Expand description
A flexible context management library that provides a trait-based approach for handling key-value data with support for multiple serialization formats.
§Features
- Generic context management through the
Contextualizetrait and aContextstruct - Support for multiple serialization formats (with feature flags):
- JSON (feature: “json”)
- TOML (feature: “toml”)
- YAML (feature: “yaml”)
- Type-safe error handling with the
cdumay_core::Errorstruct
§Example Usage
use std::collections::BTreeMap;
use serde::{Serialize, Deserialize};
use cdumay_context::{Contextualize, Context};
// Basic usage
let mut ctx = Context::new();
ctx.insert("name".to_string(), serde_value::Value::String("Alice".to_string()));
§Error Handling
The library provides a comprehensive error handling system through the Error enum:
use cdumay_context::{Context, ContextDump, Contextualize, UnExpectedError};
use rand::Rng;
use serde::{Serialize, Deserialize};
use std::collections::BTreeMap;
fn example_error_handling() -> cdumay_core::Result<()> {
let mut rng = rand::rng();
let dice_roll: u8 = rng.random_range(1..=6);
let mut ctx = Context::new();
ctx.insert("env".to_string(), serde_value::Value::String("prod".to_string()));
// Generic error
if dice_roll == 7 {
return Err(UnExpectedError::new().with_message("Something went wrong".to_string()).with_details(ctx.dump()).into());
}
Ok(())
}Structs§
- Context
- A dynamic key-value context container that can store heterogeneous data.
- UnExpected
Error - Error : UnExpectedError (Kind:
GenericContextError)
Constants§
- Generic
Context Error - ErrorKind : GenericContextError (500) - Generic context error
Traits§
- Context
Dump - A trait for types that can be converted into a serializable context map.
- Contextualize
- A trait for managing key-value context data with serialization support.