Skip to main content

serde_toon/
options.rs

1#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
2pub enum Delimiter {
3    #[default]
4    Comma,
5    Tab,
6    Pipe,
7}
8
9impl Delimiter {
10    pub fn as_char(self) -> char {
11        match self {
12            Delimiter::Comma => ',',
13            Delimiter::Tab => '\t',
14            Delimiter::Pipe => '|',
15        }
16    }
17}
18
19#[derive(Debug, Clone, Copy, PartialEq, Eq)]
20pub enum Indent {
21    Spaces(usize),
22}
23
24impl Indent {
25    pub fn spaces(count: usize) -> Self {
26        Indent::Spaces(count)
27    }
28}
29
30impl Default for Indent {
31    fn default() -> Self {
32        Indent::Spaces(2)
33    }
34}
35
36#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
37pub enum KeyFolding {
38    #[default]
39    Off,
40    Safe,
41}
42
43#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
44pub enum ExpandPaths {
45    #[default]
46    Off,
47    Safe,
48}
49
50#[derive(Debug, Clone, Default)]
51pub struct EncodeOptions {
52    pub indent: Indent,
53    pub delimiter: Delimiter,
54    pub key_folding: KeyFolding,
55    pub flatten_depth: Option<usize>,
56}
57
58impl EncodeOptions {
59    pub fn new() -> Self {
60        Self::default()
61    }
62
63    pub fn with_indent(mut self, indent: Indent) -> Self {
64        self.indent = indent;
65        self
66    }
67
68    pub fn with_delimiter(mut self, delimiter: Delimiter) -> Self {
69        self.delimiter = delimiter;
70        self
71    }
72
73    pub fn with_key_folding(mut self, key_folding: KeyFolding) -> Self {
74        self.key_folding = key_folding;
75        self
76    }
77
78    pub fn with_flatten_depth(mut self, flatten_depth: Option<usize>) -> Self {
79        self.flatten_depth = flatten_depth;
80        self
81    }
82}
83
84#[derive(Debug, Clone)]
85pub struct DecodeOptions {
86    pub indent: Indent,
87    pub strict: bool,
88    pub expand_paths: ExpandPaths,
89}
90
91impl DecodeOptions {
92    pub fn new() -> Self {
93        Self::default()
94    }
95
96    pub fn with_indent(mut self, indent: Indent) -> Self {
97        self.indent = indent;
98        self
99    }
100
101    pub fn with_strict(mut self, strict: bool) -> Self {
102        self.strict = strict;
103        self
104    }
105
106    pub fn with_expand_paths(mut self, expand_paths: ExpandPaths) -> Self {
107        self.expand_paths = expand_paths;
108        self
109    }
110}
111
112impl Default for DecodeOptions {
113    fn default() -> Self {
114        Self {
115            indent: Indent::default(),
116            strict: true,
117            expand_paths: ExpandPaths::default(),
118        }
119    }
120}