Skip to main content

wdl_format/
config.rs

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