1use serde_core::ser::Error as _;
36
37pub(crate) mod api;
38pub mod error;
39pub mod options;
40pub(crate) mod quoting;
41mod serializer;
42mod wrapper_impls;
43mod wrapping;
44mod zmij_format;
45
46pub use self::error::Error;
47pub use self::serializer::YamlSerializer;
48#[doc(hidden)]
49pub use self::serializer::{MapSer, SeqSer, StructVariantSer, TupleSer};
50
51pub type Result<T> = std::result::Result<T, Error>;
53
54#[inline]
55fn checked_indentation(indent_step: usize, depth: usize) -> Result<usize> {
56 indent_step
57 .checked_mul(depth)
58 .ok_or_else(|| Error::custom("serializer indentation exceeds usize"))
59}
60
61#[inline]
62fn checked_depth_add(depth: usize, additional_levels: usize) -> Result<usize> {
63 depth
64 .checked_add(additional_levels)
65 .ok_or_else(|| Error::custom("serializer indentation depth exceeds usize"))
66}
67
68pub use crate::long_strings::{FoldStr, FoldString, LitStr, LitString};
69
70const NAME_TUPLE_ANCHOR: &str = "__yaml_anchor";
72const NAME_TUPLE_WEAK: &str = "__yaml_weak_anchor";
73const NAME_FLOW_SEQ: &str = "__yaml_flow_seq";
74const NAME_FLOW_MAP: &str = "__yaml_flow_map";
75#[cfg(feature = "comments")]
76const NAME_TUPLE_COMMENTED: &str = "__yaml_commented";
77const NAME_TUPLE_TAGGED: &str = "__yaml_tagged";
78const NAME_SPACE_AFTER: &str = "__yaml_space_after";
79const NAME_DOUBLE_QUOTED: &str = "__yaml_double_quoted";
80const NAME_SINGLE_QUOTED: &str = "__yaml_single_quoted";
81const NAME_NULLABLE_TILDE: &str = "__yaml_nullable_tilde";
82
83#[cfg(test)]
84mod tests {
85 use super::{checked_depth_add, checked_indentation};
86
87 #[test]
88 fn indentation_arithmetic_is_checked() {
89 assert_eq!(checked_indentation(64, 2).unwrap(), 128);
90 assert_eq!(
91 checked_indentation(2, usize::MAX).unwrap_err().to_string(),
92 "serializer indentation exceeds usize"
93 );
94 assert_eq!(
95 checked_depth_add(usize::MAX, 1).unwrap_err().to_string(),
96 "serializer indentation depth exceeds usize"
97 );
98 assert_eq!(
99 checked_depth_add(usize::MAX - 1, 2)
100 .unwrap_err()
101 .to_string(),
102 "serializer indentation depth exceeds usize"
103 );
104 }
105}