jaml/ser.rs
1//! Serialization of Rust values to JAML text.
2
3use jasn_core::ser;
4use serde::Serialize;
5
6use crate::{Value, formatter};
7
8/// Error type for serialization.
9pub type Error = ser::Error;
10
11/// Result type for serialization.
12pub type Result<T> = std::result::Result<T, Error>;
13
14/// Serialize a Rust value to a JAML string.
15///
16/// JAML is inherently indentation-based, so output is always formatted
17/// with proper indentation (similar to YAML).
18pub fn to_string<T>(value: &T) -> Result<String>
19where
20 T: Serialize,
21{
22 let jaml_value = ser::to_value(value)?;
23 Ok(formatter::format(&jaml_value))
24}
25
26/// Serialize a Rust value to a JAML string with pretty formatting.
27///
28/// **Note:** JAML is inherently indentation-based (like YAML), so this function
29/// produces the same output as [`to_string`]. It exists for API consistency
30/// with other serialization formats.
31pub fn to_string_pretty<T>(value: &T) -> Result<String>
32where
33 T: Serialize,
34{
35 to_string(value)
36}
37
38/// Serialize a Rust value to a JAML string with custom formatting options.
39pub fn to_string_opts<T>(value: &T, options: &formatter::Options) -> Result<String>
40where
41 T: Serialize,
42{
43 // TODO: optimize by directly serializing to string instead of going through Value
44 let jaml_value = ser::to_value(value)?;
45 Ok(formatter::format_with_opts(&jaml_value, options))
46}
47
48/// Serialize a Rust value to a JAML [`Value`].
49pub fn to_value<T>(value: &T) -> Result<Value>
50where
51 T: Serialize + ?Sized,
52{
53 ser::to_value(value)
54}