toon_rust/
serde_api.rs

1//! Serde-compatible API for TOON encoding and decoding
2
3use crate::decode::decode;
4use crate::encode::encode;
5use crate::error::Error;
6use crate::options::{DecodeOptions, EncodeOptions};
7use serde::{de::DeserializeOwned, Serialize};
8use std::io::{Read, Write};
9
10/// Serialize a value to a TOON-formatted string
11///
12/// # Arguments
13///
14/// * `value` - The value to serialize (must implement `Serialize`)
15///
16/// # Returns
17///
18/// A `Result` containing the TOON-formatted string or an error
19///
20/// # Example
21///
22/// ```rust,no_run
23/// use serde::Serialize;
24/// use toon_rust::to_string;
25///
26/// #[derive(Serialize)]
27/// struct Product {
28///     sku: String,
29///     qty: u32,
30/// }
31///
32/// let product = Product { sku: "A1".to_string(), qty: 2 };
33/// let toon = to_string(&product).unwrap();
34/// ```
35pub fn to_string<T: Serialize>(value: &T) -> Result<String, Error> {
36    let json_value =
37        serde_json::to_value(value).map_err(|e| Error::Serialization(e.to_string()))?;
38    encode(&json_value, None)
39}
40
41/// Serialize a value to a TOON-formatted string with options
42///
43/// # Arguments
44///
45/// * `value` - The value to serialize (must implement `Serialize`)
46/// * `options` - Encoding options
47///
48/// # Returns
49///
50/// A `Result` containing the TOON-formatted string or an error
51pub fn to_string_with_options<T: Serialize>(
52    value: &T,
53    options: &EncodeOptions,
54) -> Result<String, Error> {
55    let json_value =
56        serde_json::to_value(value).map_err(|e| Error::Serialization(e.to_string()))?;
57    encode(&json_value, Some(options))
58}
59
60/// Serialize a value to a writer in TOON format
61///
62/// # Arguments
63///
64/// * `value` - The value to serialize (must implement `Serialize`)
65/// * `writer` - The writer to write to
66///
67/// # Returns
68///
69/// A `Result` indicating success or failure
70pub fn to_writer<T: Serialize, W: Write>(value: &T, writer: &mut W) -> Result<(), Error> {
71    let toon = to_string(value)?;
72    writer
73        .write_all(toon.as_bytes())
74        .map_err(|e| Error::Io(e.to_string()))?;
75    Ok(())
76}
77
78/// Serialize a value to a writer in TOON format with options
79///
80/// # Arguments
81///
82/// * `value` - The value to serialize (must implement `Serialize`)
83/// * `writer` - The writer to write to
84/// * `options` - Encoding options
85///
86/// # Returns
87///
88/// A `Result` indicating success or failure
89pub fn to_writer_with_options<T: Serialize, W: Write>(
90    value: &T,
91    writer: &mut W,
92    options: &EncodeOptions,
93) -> Result<(), Error> {
94    let toon = to_string_with_options(value, options)?;
95    writer
96        .write_all(toon.as_bytes())
97        .map_err(|e| Error::Io(e.to_string()))?;
98    Ok(())
99}
100
101/// Deserialize a TOON-formatted string to a value
102///
103/// # Arguments
104///
105/// * `s` - The TOON-formatted string to deserialize
106///
107/// # Returns
108///
109/// A `Result` containing the deserialized value or an error
110///
111/// # Example
112///
113/// ```rust,no_run
114/// use serde::Deserialize;
115/// use toon_rust::from_str;
116///
117/// #[derive(Deserialize)]
118/// struct Product {
119///     sku: String,
120///     qty: u32,
121/// }
122///
123/// let toon = "sku: A1\nqty: 2";
124/// let product: Product = from_str(toon).unwrap();
125/// ```
126pub fn from_str<T: DeserializeOwned>(s: &str) -> Result<T, Error> {
127    from_str_with_options(s, None)
128}
129
130/// Deserialize a TOON-formatted string to a value with options
131///
132/// # Arguments
133///
134/// * `s` - The TOON-formatted string to deserialize
135/// * `options` - Decoding options
136///
137/// # Returns
138///
139/// A `Result` containing the deserialized value or an error
140pub fn from_str_with_options<T: DeserializeOwned>(
141    s: &str,
142    options: Option<&DecodeOptions>,
143) -> Result<T, Error> {
144    let json_value = decode(s, options)?;
145    serde_json::from_value(json_value).map_err(|e| Error::Deserialization(e.to_string()))
146}
147
148/// Deserialize a TOON-formatted reader to a value
149///
150/// # Arguments
151///
152/// * `reader` - The reader to read from
153///
154/// # Returns
155///
156/// A `Result` containing the deserialized value or an error
157pub fn from_reader<T: DeserializeOwned, R: Read>(reader: &mut R) -> Result<T, Error> {
158    let mut s = String::new();
159    reader
160        .read_to_string(&mut s)
161        .map_err(|e| Error::Io(e.to_string()))?;
162    from_str(&s)
163}
164
165/// Deserialize a TOON-formatted reader to a value with options
166///
167/// # Arguments
168///
169/// * `reader` - The reader to read from
170/// * `options` - Decoding options
171///
172/// # Returns
173///
174/// A `Result` containing the deserialized value or an error
175pub fn from_reader_with_options<T: DeserializeOwned, R: Read>(
176    reader: &mut R,
177    options: &DecodeOptions,
178) -> Result<T, Error> {
179    let mut s = String::new();
180    reader
181        .read_to_string(&mut s)
182        .map_err(|e| Error::Io(e.to_string()))?;
183    from_str_with_options(&s, Some(options))
184}