Crates.io | cdumay_error_yaml |
lib.rs | cdumay_error_yaml |
version | 0.1.8 |
created_at | 2025-04-20 22:02:56.786291+00 |
updated_at | 2025-05-26 13:11:47.273262+00 |
description | A Rust Library for YAML error |
homepage | https://github.com/cdumay/cdumay_error_yaml |
repository | https://github.com/cdumay/cdumay_error_yaml |
max_upload_size | |
id | 1642050 |
size | 21,776 |
Here's the documentation for your code in a README.md
format:
A lightweight utility crate that converts YAML serialization and deserialization errors (serde_yaml::Error
) into structured, typed errors using the cdumay_core
framework.
This helps standardize error handling for Rust applications that deal with YAML configuration or data files, while enriching error details with structured context.
BTreeMap
cdumay_core::ErrorConverter
traitconvert_result!
macro for error conversion[dependencies]
cdumay_core = "1.0"
serde = { version = "1.0", features = ["derive"] }
serde-value = "0.7"
serde_yaml = "0.8"
Using the YamlErrorConverter
directly:
use cdumay_core::ErrorConverter;
use std::collections::BTreeMap;
use serde::{Deserialize, Serialize};
use cdumay_error_yaml::YamlErrorConverter;
#[derive(Serialize, Deserialize)]
struct Config {
name: String,
debug: bool,
}
fn serialize_config(config: &Config) -> cdumay_core::Result<String> {
serde_yaml::to_string(config).map_err(|e| {
let mut ctx = BTreeMap::new();
ctx.insert("config_name".into(), serde_value::Value::String(config.name.clone()));
YamlErrorConverter::convert(&e, "Failed to serialize YAML config".into(), ctx)
})
}
fn deserialize_config(input: &str) -> cdumay_core::Result<Config> {
serde_yaml::from_str::<Config>(input).map_err(|e| {
let mut ctx = BTreeMap::new();
ctx.insert("input".into(), serde_value::Value::String(input.to_string()));
YamlErrorConverter::convert(&e, "Failed to deserialize YAML config".into(), ctx)
})
}
{
"code": "YAML-00001",
"status": 400,
"kind": "Invalid YAML data",
"message": "Failed to deserialize YAML config",
"context": {
"input": "invalid: yaml"
}
}
Using the convert_result!
macro:
use cdumay_core::ErrorConverter;
use std::collections::BTreeMap;
use serde::{Deserialize, Serialize};
use cdumay_error_yaml::convert_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_result!(serde_yaml::to_string(config), ctx, "Failed to serialize YAML config")
}
fn deserialize_config(input: &str) -> cdumay_core::Result<Config> {
convert_result!(serde_yaml::from_str::<Config>(input))
}