serde_saphyr/serializer_options.rs
1//! Serializer options for YAML emission.
2//!
3//! Controls indentation and optional anchor name generation for the serializer.
4//!
5//! Example: use 4-space indentation and a custom anchor naming scheme.
6//!
7//! ```rust
8//! use serde::Serialize;
9//!
10//! #[derive(Serialize)]
11//! struct Item { a: i32, b: bool }
12//!
13//! let mut buf = String::new();
14//! let opts = serde_saphyr::SerializerOptions {
15//! indent_step: 4,
16//! anchor_generator: Some(|id| format!("id{}/", id)),
17//! };
18//! serde_saphyr::to_fmt_writer_with_options(&mut buf, &Item { a: 1, b: true }, opts).unwrap();
19//! assert!(buf.contains("a: 1"));
20//! ```
21#[derive(Clone, Copy)]
22pub struct SerializerOptions {
23 /// Number of spaces to indent per nesting level when emitting block-style collections.
24 pub indent_step: usize,
25 /// Optional custom anchor-name generator.
26 ///
27 /// Receives a monotonically increasing `usize` id (starting at 1) and returns the
28 /// anchor name to emit. If `None`, the built-in generator yields names like `a1`, `a2`, ...
29 pub anchor_generator: Option<fn(usize) -> String>,
30}
31
32impl Default for SerializerOptions {
33 fn default() -> Self { Self { indent_step: 2, anchor_generator: None } }
34}