Skip to main content

wdl_format/
config.rs

1//! Formatting configuration.
2
3mod indent;
4mod max_line_length;
5mod newline;
6mod quotes;
7
8pub use indent::Indent;
9pub use max_line_length::MaxLineLength;
10pub use newline::NewlineStyle;
11pub use quotes::QuoteStyle;
12use schemars::JsonSchema;
13use toml_spanner::Toml;
14use toml_spanner::helper::display;
15use toml_spanner::helper::parse_string;
16
17/// Default for whether import sorting is enabled.
18fn sort_imports_default() -> bool {
19    true
20}
21
22/// Default for whether input sorting is enabled.
23fn sort_inputs_default() -> bool {
24    false
25}
26
27/// Default for whether trailing commas are enabled.
28fn trailing_commas_default() -> bool {
29    true
30}
31
32/// Default for whether task and workflow sections should be reordered.
33fn reorder_sections_default() -> bool {
34    false
35}
36
37/// Default for whether to upgrade deprecations.
38fn upgrade_deprecations_default() -> bool {
39    false
40}
41
42/// Configuration for formatting.
43#[derive(Clone, Copy, Debug, PartialEq, Eq, Toml, JsonSchema)]
44#[toml(Toml, deny_unknown_fields)]
45pub struct Config {
46    /// The indentation configuration.
47    #[toml(default)]
48    #[schemars(default)]
49    pub indent: Indent,
50    /// The maximum line length.
51    #[toml(default)]
52    #[schemars(default)]
53    pub max_line_length: MaxLineLength,
54    /// Whether to sort import statements alphabetically.
55    #[toml(default = sort_imports_default())]
56    #[schemars(default = "sort_imports_default")]
57    pub sort_imports: bool,
58    /// Whether to sort input sections.
59    #[toml(default = sort_inputs_default())]
60    #[schemars(default = "sort_inputs_default")]
61    pub sort_inputs: bool,
62    /// Whether to add trailing commas to multiline lists.
63    #[toml(default = trailing_commas_default())]
64    #[schemars(default = "trailing_commas_default")]
65    pub trailing_commas: bool,
66    /// Whether to reorder task and workflow sections to Sprocket's opinionated
67    /// order.
68    #[toml(default = reorder_sections_default())]
69    #[schemars(default = "reorder_sections_default")]
70    pub reorder_sections: bool,
71    /// Whether to eagerly upgrade deprecated WDL constructs.
72    ///
73    /// Currently this includes changing curly brace command sections (`{}`)
74    /// into heredoc command sections (`<<<>>>`) and changing dollar-style
75    /// placeholders (`${}`) into tilde-style placeholders (`~{}`).
76    #[toml(default = upgrade_deprecations_default())]
77    #[schemars(default = "upgrade_deprecations_default")]
78    pub upgrade_deprecations: bool,
79    /// The newline style.
80    #[toml(default, FromToml with = parse_string, ToToml with = display)]
81    #[schemars(default)]
82    pub newline_style: NewlineStyle,
83    /// The quote style.
84    #[toml(default, FromToml with = parse_string, ToToml with = display)]
85    #[schemars(default)]
86    pub quote_style: QuoteStyle,
87}
88
89impl Default for Config {
90    fn default() -> Self {
91        Self {
92            indent: Indent::default(),
93            max_line_length: MaxLineLength::default(),
94            sort_imports: sort_imports_default(),
95            sort_inputs: sort_inputs_default(),
96            trailing_commas: trailing_commas_default(),
97            reorder_sections: reorder_sections_default(),
98            upgrade_deprecations: upgrade_deprecations_default(),
99            newline_style: NewlineStyle::default(),
100            quote_style: QuoteStyle::default(),
101        }
102    }
103}
104
105impl Config {
106    /// Overwrite the indentation configuration.
107    pub fn indent(mut self, indent: Indent) -> Self {
108        self.indent = indent;
109        self
110    }
111
112    /// Set the newline style.
113    pub fn newline_style(mut self, newline_style: NewlineStyle) -> Self {
114        self.newline_style = newline_style;
115        self
116    }
117
118    /// Set the quote style.
119    pub fn quote_style(mut self, quote_style: QuoteStyle) -> Self {
120        self.quote_style = quote_style;
121        self
122    }
123
124    /// Overwrite the maximum line length configuration.
125    pub fn max_line_length(mut self, max_line_length: MaxLineLength) -> Self {
126        self.max_line_length = max_line_length;
127        self
128    }
129
130    /// Set whether import sorting is enabled.
131    pub fn sort_imports(mut self, sort_imports: bool) -> Self {
132        self.sort_imports = sort_imports;
133        self
134    }
135
136    /// Set whether input sorting is enabled.
137    pub fn sort_inputs(mut self, sort_inputs: bool) -> Self {
138        self.sort_inputs = sort_inputs;
139        self
140    }
141
142    /// Set whether trailing commas are enabled.
143    pub fn trailing_commas(mut self, trailing_commas: bool) -> Self {
144        self.trailing_commas = trailing_commas;
145        self
146    }
147
148    /// Set whether section reordering is enabled.
149    pub fn reorder_sections(mut self, reorder_sections: bool) -> Self {
150        self.reorder_sections = reorder_sections;
151        self
152    }
153
154    /// Set whether to upgrade deprecations.
155    pub fn upgrade_deprecations(mut self, upgrade_deprecations: bool) -> Self {
156        self.upgrade_deprecations = upgrade_deprecations;
157        self
158    }
159}