Skip to main content

wdl_format/
config.rs

1//! Formatting configuration.
2
3mod indent;
4mod max_line_length;
5mod newline;
6
7pub use indent::Indent;
8pub use max_line_length::MaxLineLength;
9pub use newline::NewlineStyle;
10use toml_spanner::Toml;
11use toml_spanner::helper::display;
12use toml_spanner::helper::parse_string;
13
14/// Default for whether import sorting is enabled.
15const SORT_IMPORTS_DEFAULT: bool = true;
16/// Default for whether input sorting is enabled.
17const SORT_INPUTS_DEFAULT: bool = false;
18/// Default for whether trailing commas are enabled.
19const TRAILING_COMMAS_DEFAULT: bool = true;
20
21/// Configuration for formatting.
22#[derive(Clone, Copy, Debug, PartialEq, Eq, Toml)]
23#[toml(Toml, deny_unknown_fields)]
24pub struct Config {
25    /// The indentation configuration.
26    #[toml(default)]
27    pub indent: Indent,
28    /// The maximum line length.
29    #[toml(default)]
30    pub max_line_length: MaxLineLength,
31    /// Whether to sort import statements alphabetically.
32    #[toml(default = SORT_IMPORTS_DEFAULT)]
33    pub sort_imports: bool,
34    /// Whether to sort input sections.
35    #[toml(default = SORT_INPUTS_DEFAULT)]
36    pub sort_inputs: bool,
37    /// Whether to add trailing commas to multiline lists.
38    #[toml(default = TRAILING_COMMAS_DEFAULT)]
39    pub trailing_commas: bool,
40    /// The newline style.
41    #[toml(default, FromToml with = parse_string, ToToml with = display)]
42    pub newline_style: NewlineStyle,
43}
44
45impl Default for Config {
46    fn default() -> Self {
47        Self {
48            indent: Indent::default(),
49            max_line_length: MaxLineLength::default(),
50            sort_imports: SORT_IMPORTS_DEFAULT,
51            sort_inputs: SORT_INPUTS_DEFAULT,
52            trailing_commas: TRAILING_COMMAS_DEFAULT,
53            newline_style: NewlineStyle::default(),
54        }
55    }
56}
57
58impl Config {
59    /// Overwrite the indentation configuration.
60    pub fn indent(mut self, indent: Indent) -> Self {
61        self.indent = indent;
62        self
63    }
64
65    /// Set the newline style.
66    pub fn newline_style(mut self, newline_style: NewlineStyle) -> Self {
67        self.newline_style = newline_style;
68        self
69    }
70
71    /// Overwrite the maximum line length configuration.
72    pub fn max_line_length(mut self, max_line_length: MaxLineLength) -> Self {
73        self.max_line_length = max_line_length;
74        self
75    }
76
77    /// Set whether import sorting is enabled.
78    pub fn sort_imports(mut self, sort_imports: bool) -> Self {
79        self.sort_imports = sort_imports;
80        self
81    }
82
83    /// Set whether input sorting is enabled.
84    pub fn sort_inputs(mut self, sort_inputs: bool) -> Self {
85        self.sort_inputs = sort_inputs;
86        self
87    }
88
89    /// Set whether trailing commas are enabled.
90    pub fn trailing_commas(mut self, trailing_commas: bool) -> Self {
91        self.trailing_commas = trailing_commas;
92        self
93    }
94}