Crates.io | cdumay_error_toml |
lib.rs | cdumay_error_toml |
version | 0.1.9 |
created_at | 2025-04-21 00:08:43.831444+00 |
updated_at | 2025-05-26 13:10:48.134361+00 |
description | A Rust Library for TOML error |
homepage | https://github.com/cdumay/cdumay_error_toml |
repository | https://github.com/cdumay/cdumay_error_toml |
max_upload_size | |
id | 1642114 |
size | 23,902 |
A lightweight utility crate that wraps TOML serialization and deserialization errors (toml::ser::Error
, toml::de::Error
) and converts them into structured, typed errors using the cdumay_core
framework.
This helps standardize error handling in Rust applications that process TOML configuration or data files, while enriching error details with structured context.
Serialization
and Deserialization
BTreeMap
cdumay_core::ErrorConverter
trait for easy integration[dependencies]
cdumay_core = "0.1"
serde = { version = "1.0", features = ["derive"] }
serde-value = "0.7"
toml = "0.8"
Using the TomlDeserializeErrorConverter
and TomlSerializeErrorConverter
directly:
use cdumay_core::ErrorConverter;
use std::collections::BTreeMap;
use serde::{Deserialize, Serialize};
use cdumay_error_toml::{TomlDeserializeErrorConverter, TomlSerializeErrorConverter};
#[derive(Serialize, Deserialize)]
struct Config {
name: String,
debug: bool,
}
fn serialize_config(config: &Config) -> cdumay_core::Result<String> {
toml::to_string(config).map_err(|e| {
let mut ctx = BTreeMap::new();
ctx.insert("config_name".into(), serde_value::Value::String(config.name.clone()));
TomlSerializeErrorConverter::convert(&e, "Failed to serialize TOML config".into(), ctx)
})
}
fn deserialize_config(input: &str) -> cdumay_core::Result<Config> {
toml::from_str::<Config>(input).map_err(|e| {
let mut ctx = BTreeMap::new();
ctx.insert("input".into(), serde_value::Value::String(input.to_string()));
TomlDeserializeErrorConverter::convert(&e, "Failed to deserialize TOML config".into(), ctx)
})
}
{
"code": "TOML-00001",
"status": 400,
"kind": "Invalid Toml data",
"message": "Failed to deserialize TOML config",
"context": {
"input": "[invalid toml]"
}
}
Using the macros:
use cdumay_core::ErrorConverter;
use std::collections::BTreeMap;
use serde::{Deserialize, Serialize};
use cdumay_error_toml::{convert_deserialize_result, convert_serialize_result};
#[derive(Serialize, Deserialize)]
struct Config {
name: String,
debug: bool,
}
fn serialize_config(config: &Config) -> cdumay_core::Result<String> {
let mut ctx = BTreeMap::new();
ctx.insert("config_name".into(), serde_value::Value::String(config.name.clone()));
convert_serialize_result!(toml::to_string(config), ctx, "Failed to serialize TOML config")
}
fn deserialize_config(input: &str) -> cdumay_core::Result<Config> {
let mut ctx = BTreeMap::new();
ctx.insert("input".into(), serde_value::Value::String(input.to_string()));
convert_deserialize_result!(toml::from_str::<Config>(input), ctx, "Failed to deserialize TOML config")
}