Skip to main content

eure_fmt/
config.rs

1//! Formatter configuration.
2
3/// Configuration options for the Eure formatter.
4#[derive(Debug, Clone, PartialEq, Eq)]
5pub struct FormatConfig {
6    /// Maximum line width (guideline, not strict limit).
7    /// Default: 100
8    pub max_width: usize,
9
10    /// Number of spaces per indentation level.
11    /// Default: 2
12    pub indent_width: usize,
13
14    /// Use tabs instead of spaces for indentation.
15    /// Default: false
16    pub use_tabs: bool,
17
18    /// Trailing comma policy for multiline arrays/objects.
19    /// Default: TrailingComma::Always
20    pub trailing_comma: TrailingComma,
21
22    /// Newline style.
23    /// Default: NewlineStyle::Lf
24    pub newline: NewlineStyle,
25}
26
27impl Default for FormatConfig {
28    fn default() -> Self {
29        Self {
30            max_width: 100,
31            indent_width: 2,
32            use_tabs: false,
33            trailing_comma: TrailingComma::Always,
34            newline: NewlineStyle::Lf,
35        }
36    }
37}
38
39impl FormatConfig {
40    /// Create a new config with default values.
41    pub fn new() -> Self {
42        Self::default()
43    }
44
45    /// Set max width.
46    pub fn with_max_width(mut self, width: usize) -> Self {
47        self.max_width = width;
48        self
49    }
50
51    /// Set indent width.
52    pub fn with_indent_width(mut self, width: usize) -> Self {
53        self.indent_width = width;
54        self
55    }
56
57    /// Set tab usage.
58    pub fn with_tabs(mut self, use_tabs: bool) -> Self {
59        self.use_tabs = use_tabs;
60        self
61    }
62
63    /// Set trailing comma policy.
64    pub fn with_trailing_comma(mut self, policy: TrailingComma) -> Self {
65        self.trailing_comma = policy;
66        self
67    }
68
69    /// Set newline style.
70    pub fn with_newline(mut self, style: NewlineStyle) -> Self {
71        self.newline = style;
72        self
73    }
74}
75
76/// Trailing comma policy.
77#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
78pub enum TrailingComma {
79    /// Always add trailing commas in multiline contexts.
80    #[default]
81    Always,
82    /// Never add trailing commas.
83    Never,
84}
85
86/// Newline style.
87#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
88pub enum NewlineStyle {
89    /// Unix-style line endings (LF).
90    #[default]
91    Lf,
92    /// Windows-style line endings (CRLF).
93    Crlf,
94}
95
96impl NewlineStyle {
97    /// Get the newline string.
98    pub fn as_str(self) -> &'static str {
99        match self {
100            NewlineStyle::Lf => "\n",
101            NewlineStyle::Crlf => "\r\n",
102        }
103    }
104}