Skip to main content

serde_saphyr/ser/
mod.rs

1//! Single-pass YAML serializer with optional anchors for Rc/Arc/Weak,
2//! order preservation (uses the iterator order of your types), simple
3//! style controls (block strings & flow containers), and special
4//! float handling for NaN/±Inf. No intermediate YAML DOM is built.
5//!
6//! Usage example:
7//!
8//! ```
9//! use serde::Serialize;
10//! use std::rc::Rc;
11//! use serde_saphyr::{to_string, RcAnchor, LitStr, FlowSeq};
12//!
13//! #[derive(Serialize)]
14//! struct Cfg {
15//!     name: String,
16//!     ports: FlowSeq<Vec<u16>>,   // render `[8080, 8081]`
17//!     note: LitStr<'static>,      // render as `|` block
18//!     data: RcAnchor<Vec<i32>>,   // first sight => &a1
19//!     alias: RcAnchor<Vec<i32>>,  // later sight => *a1
20//! }
21//!
22//! fn main() {
23//!     let shared = Rc::new(vec![1,2,3]);
24//!     let cfg = Cfg {
25//!         name: "demo".into(),
26//!         ports: FlowSeq(vec![8080, 8081]),
27//!         note: LitStr("line 1\nline 2"),
28//!         data: RcAnchor(shared.clone()),
29//!         alias: RcAnchor(shared),
30//!     };
31//!     println!("{}", to_string(&cfg).unwrap());
32//! }
33//! ```
34
35pub(crate) mod api;
36pub mod error;
37pub mod options;
38pub(crate) mod quoting;
39mod serializer;
40mod wrapper_impls;
41mod wrapping;
42mod zmij_format;
43
44pub use self::error::Error;
45pub use self::serializer::{
46    MapSer, SeqSer, StructVariantSer, TupleSer, TupleVariantSer, YamlSerializer,
47};
48
49/// Result alias.
50pub type Result<T> = std::result::Result<T, Error>;
51
52pub use crate::long_strings::{FoldStr, FoldString, LitStr, LitString};
53
54// Flow hints and block-string hints: we use newtype-struct names.
55const NAME_TUPLE_ANCHOR: &str = "__yaml_anchor";
56const NAME_TUPLE_WEAK: &str = "__yaml_weak_anchor";
57const NAME_FLOW_SEQ: &str = "__yaml_flow_seq";
58const NAME_FLOW_MAP: &str = "__yaml_flow_map";
59const NAME_TUPLE_COMMENTED: &str = "__yaml_commented";
60const NAME_SPACE_AFTER: &str = "__yaml_space_after";