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