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