datex_core/fmt/
options.rs

1#[derive(Clone, Debug, PartialEq, Eq)]
2pub struct FormattingOptions {
3    /// Number of spaces to use for indentation.
4    pub indent: usize,
5
6    /// Maximum line width before wrapping occurs.
7    pub max_width: usize,
8
9    /// Whether to add trailing commas in collections like lists and maps.
10    /// E.g., `[1, 2, 3,]` instead of `[1, 2, 3]`.
11    pub trailing_comma: bool,
12
13    /// Whether to add spaces inside brackets of collections like lists and maps.
14    /// E.g., `[ 1,2,3 ]` instead of `[1,2,3]`.
15    pub spaced_collections: bool,
16
17    /// Whether to add spaces inside collections like lists and maps.
18    /// E.g., `[1, 2, 3]` instead of `[1,2,3]`.
19    pub space_in_collection: bool,
20
21    /// Whether to add spaces around operators.
22    /// E.g., `1 + 2` instead of `1+2`.
23    pub spaces_around_operators: bool,
24
25    /// Formatting style for type declarations.
26    /// Determines how type annotations are spaced and aligned.
27    pub type_declaration_formatting: TypeDeclarationFormatting,
28
29    /// Whether to add newlines between statements.
30    pub statement_formatting: StatementFormatting,
31
32    /// Formatting style for type variant suffixes.
33    pub variant_formatting: VariantFormatting,
34
35    /// Bracketing style for expressions.
36    pub bracket_style: BracketStyle,
37}
38
39#[derive(Clone, Debug, PartialEq, Eq)]
40pub enum BracketStyle {
41    /// Keep original bracketing as is.
42    KeepAll,
43
44    /// Remove only redundant or duplicate outer brackets, e.g. `((42))` -> `(42)`.
45    RemoveDuplicate,
46
47    /// Remove all unnecessary brackets based purely on operator precedence.
48    Minimal,
49}
50
51/// Formatting styles for enum variants.
52#[derive(Clone, Debug, PartialEq, Eq)]
53pub enum VariantFormatting {
54    /// Keep the original formatting.
55    KeepAll,
56    /// Use variant suffixes.
57    WithSuffix,
58    /// Do not use variant suffixes.
59    WithoutSuffix,
60}
61
62/// Formatting styles for statements.
63#[derive(Clone, Debug, PartialEq, Eq)]
64pub enum StatementFormatting {
65    /// Add a newline between statements.
66    NewlineBetween,
67    /// Add a space between statements.
68    SpaceBetween,
69    /// Compact formatting without extra spaces or newlines.
70    Compact,
71}
72
73/// Formatting styles for type declarations.
74#[derive(Clone, Debug, PartialEq, Eq)]
75pub enum TypeDeclarationFormatting {
76    /// Compact formatting without extra spaces.
77    Compact,
78    /// Spaces around the colon in type declarations.
79    SpaceAroundColon,
80    /// Space after the colon in type declarations.
81    SpaceAfterColon,
82}
83
84impl Default for FormattingOptions {
85    fn default() -> Self {
86        FormattingOptions {
87            indent: 4,
88            max_width: 40,
89            variant_formatting: VariantFormatting::KeepAll,
90            trailing_comma: true,
91            spaced_collections: false,
92            space_in_collection: true,
93            spaces_around_operators: true,
94            type_declaration_formatting:
95                TypeDeclarationFormatting::SpaceAfterColon,
96            statement_formatting: StatementFormatting::NewlineBetween,
97            bracket_style: BracketStyle::Minimal,
98        }
99    }
100}
101impl FormattingOptions {
102    pub fn compact() -> Self {
103        FormattingOptions {
104            indent: 2,
105            max_width: 40,
106            variant_formatting: VariantFormatting::WithoutSuffix,
107            trailing_comma: false,
108            spaced_collections: false,
109            space_in_collection: false,
110            spaces_around_operators: false,
111            type_declaration_formatting: TypeDeclarationFormatting::Compact,
112            statement_formatting: StatementFormatting::Compact,
113            bracket_style: BracketStyle::Minimal,
114        }
115    }
116}