1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
//! [](./LICENSE)
//! [](https://crates.io/crates/cdumay_error_toml)
//! [](https://docs.rs/cdumay_error_toml)
//! [](https://github.com/cdumay/cdumay_error_toml)
//!
//! 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`](https://!docs.rs/cdumay-error/) framework.
//!
//! This helps standardize error handling in Rust applications that process TOML configuration or data files, while enriching error details with structured context.
//!
//! ## Features
//!
//! - Categorizes TOML-related errors into `Serialization` and `Deserialization`
//! - Provides unique error codes, HTTP status codes, and descriptions
//! - Supports rich contextual error metadata via `BTreeMap`
//! - Uses the `cdumay_core::ErrorConverter` trait for easy integration
//! - Provides a convenient macros for error conversion
//!
//! ## Usage Example
//!
//! ### Dependencies
//!
//! ```toml
//! [dependencies]
//! cdumay_core = "0.1"
//! serde = { version = "1.0", features = ["derive"] }
//! serde-value = "0.7"
//! toml = "0.8"
//! ```
//!
//! ### Code sample
//!
//! Using the `TomlDeserializeErrorConverter` and `TomlSerializeErrorConverter` directly:
//! ```rust
//! 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) -> Result<String, cdumay_core::Error> {
//! 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) -> Result<Config, cdumay_core::Error> {
//! 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)
//! })
//! }
//! ```
//!
//! ### Example Output
//!
//! ```json
//! {
//! "code": "TOML-00001",
//! "status": 400,
//! "kind": "Invalid Toml data",
//! "message": "Failed to deserialize TOML config",
//! "context": {
//! "input": "[invalid toml]"
//! }
//! }
//! ```
//!
//! Using the macros:
//! ```rust
//! 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) -> Result<String, cdumay_core::Error> {
//! 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) -> Result<Config, cdumay_core::Error> {
//! 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")
//! }
//! ```
use ;
use BTreeMap;
define_kinds!
define_errors!
/// Wrapper type for converting [`toml::ser::Error`]
/// into a structured [`cdumay_core::Error`].
;
/// Wrapper type for converting [`toml::de::Error`]
/// into a structured [`cdumay_core::Error`].
;