cdumay_context/
lib.rs

1//! [![License: BSD-3-Clause](https://img.shields.io/badge/license-BSD--3--Clause-blue)](./LICENSE)
2//! [![cdumay_context on crates.io](https://img.shields.io/crates/v/cdumay_context)](https://crates.io/crates/cdumay_context)
3//! [![cdumay_context on docs.rs](https://docs.rs/cdumay_context/badge.svg)](https://docs.rs/cdumay_context)
4//! [![Source Code Repository](https://img.shields.io/badge/Code-On%20GitHub-blue?logo=GitHub)](https://github.com/cdumay/cdumay_context)
5//!
6//! A flexible context management library that provides a trait-based approach for handling
7//! key-value data with support for multiple serialization formats.
8//!
9//! # Features
10//!
11//! - Generic context management through the `Contextualize` trait and a `Context` struct
12//! - Support for multiple serialization formats (with feature flags):
13//!   - JSON (feature: "json")
14//!   - TOML (feature: "toml")
15//!   - YAML (feature: "yaml")
16//! - Type-safe error handling with the `cdumay_core::Error` struct
17//!
18//! # Example Usage
19//!
20//! ```rust
21//! use std::collections::BTreeMap;
22//! use serde::{Serialize, Deserialize};
23//! use cdumay_context::{Contextualize, Context};
24//!
25//! // Basic usage
26//! let mut ctx = Context::new();
27//! ctx.insert("name".to_string(), serde_value::Value::String("Alice".to_string()));
28//!
29//! ```
30//!
31//! # Error Handling
32//!
33//! The library provides a comprehensive error handling system through the `Error` enum:
34//!
35//! ```rust
36//! use cdumay_context::{Context, ContextDump, Contextualize, UnExpectedError};
37//! use rand::Rng;
38//! use serde::{Serialize, Deserialize};
39//! use std::collections::BTreeMap;
40//!
41//! fn example_error_handling() -> cdumay_core::Result<()> {
42//!     let mut rng = rand::rng();
43//!     let dice_roll: u8 = rng.random_range(1..=6);
44//!     let mut ctx = Context::new();
45//!     ctx.insert("env".to_string(), serde_value::Value::String("prod".to_string()));
46//!
47//!     // Generic error
48//!     if dice_roll == 7 {
49//!         return Err(UnExpectedError::new().with_message("Something went wrong".to_string()).with_details(ctx.dump()).into());
50//!     }
51//!
52//!     Ok(())
53//! }
54//! ```
55
56mod error;
57pub use error::{GenericContextError, UnExpectedError};
58
59mod context;
60pub use context::{ContextDump, Context, Contextualize};