toon_rust/
options.rs

1//! Options for encoding and decoding TOON format
2
3/// Delimiter character for tabular arrays
4#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
5pub enum Delimiter {
6    /// Comma delimiter (default)
7    #[default]
8    Comma,
9    /// Tab delimiter
10    Tab,
11    /// Pipe delimiter
12    Pipe,
13}
14
15impl Delimiter {
16    /// Get the delimiter character
17    pub fn as_char(self) -> char {
18        match self {
19            Delimiter::Comma => ',',
20            Delimiter::Tab => '\t',
21            Delimiter::Pipe => '|',
22        }
23    }
24}
25
26/// Options for encoding TOON format
27#[derive(Debug, Clone, Default)]
28pub struct EncodeOptions {
29    /// Delimiter for tabular arrays (default: comma)
30    pub delimiter: Option<Delimiter>,
31    /// Optional hash prefix for array lengths (e.g., `[#3]` instead of `[3]`)
32    pub length_marker: Option<char>,
33    /// Number of spaces per indentation level (default: 2)
34    pub indent: Option<usize>,
35}
36
37impl EncodeOptions {
38    /// Create new default options
39    pub fn new() -> Self {
40        Self::default()
41    }
42
43    /// Set the delimiter
44    pub fn delimiter(mut self, delimiter: Delimiter) -> Self {
45        self.delimiter = Some(delimiter);
46        self
47    }
48
49    /// Set the length marker (typically `'#'`)
50    pub fn length_marker(mut self, marker: char) -> Self {
51        self.length_marker = Some(marker);
52        self
53    }
54
55    /// Set the indentation level
56    pub fn indent(mut self, indent: usize) -> Self {
57        self.indent = Some(indent);
58        self
59    }
60
61    /// Get the delimiter, defaulting to comma
62    pub fn get_delimiter(&self) -> char {
63        self.delimiter.unwrap_or_default().as_char()
64    }
65
66    /// Get the indentation, defaulting to 2
67    pub fn get_indent(&self) -> usize {
68        self.indent.unwrap_or(2)
69    }
70}
71
72/// Options for decoding TOON format
73#[derive(Debug, Clone, Default)]
74pub struct DecodeOptions {
75    /// Expected number of spaces per indentation level (default: 2)
76    pub indent: Option<usize>,
77    /// Enable strict validation (default: true)
78    pub strict: Option<bool>,
79}
80
81impl DecodeOptions {
82    /// Create new default options
83    pub fn new() -> Self {
84        Self::default()
85    }
86
87    /// Set the expected indentation level
88    pub fn indent(mut self, indent: usize) -> Self {
89        self.indent = Some(indent);
90        self
91    }
92
93    /// Set strict mode
94    pub fn strict(mut self, strict: bool) -> Self {
95        self.strict = Some(strict);
96        self
97    }
98
99    /// Get the indentation, defaulting to 2
100    pub fn get_indent(&self) -> usize {
101        self.indent.unwrap_or(2)
102    }
103
104    /// Get strict mode, defaulting to true
105    pub fn get_strict(&self) -> bool {
106        self.strict.unwrap_or(true)
107    }
108}