foundry_config/
fmt.rs

1//! Configuration specific to the `forge fmt` command and the `forge_fmt` package
2
3use serde::{Deserialize, Serialize};
4
5/// Contains the config and rule set
6#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
7pub struct FormatterConfig {
8    /// Maximum line length where formatter will try to wrap the line
9    pub line_length: usize,
10    /// Number of spaces per indentation level. Ignored if style is Tab
11    pub tab_width: usize,
12    /// Style of indent
13    pub style: IndentStyle,
14    /// Print spaces between brackets
15    pub bracket_spacing: bool,
16    /// Style of uint/int256 types
17    pub int_types: IntTypes,
18    /// Style of multiline function header in case it doesn't fit
19    pub multiline_func_header: MultilineFuncHeaderStyle,
20    /// Style of quotation marks
21    pub quote_style: QuoteStyle,
22    /// Style of underscores in number literals
23    pub number_underscore: NumberUnderscore,
24    /// Style of underscores in hex literals
25    pub hex_underscore: HexUnderscore,
26    /// Style of single line blocks in statements
27    pub single_line_statement_blocks: SingleLineBlockStyle,
28    /// Print space in state variable, function and modifier `override` attribute
29    pub override_spacing: bool,
30    /// Wrap comments on `line_length` reached
31    pub wrap_comments: bool,
32    /// Globs to ignore
33    pub ignore: Vec<String>,
34    /// Add new line at start and end of contract declarations
35    pub contract_new_lines: bool,
36    /// Sort import statements alphabetically in groups (a group is separated by a newline).
37    pub sort_imports: bool,
38}
39
40/// Style of uint/int256 types
41#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)]
42#[serde(rename_all = "snake_case")]
43pub enum IntTypes {
44    /// Print the explicit uint256 or int256
45    Long,
46    /// Print the implicit uint or int
47    Short,
48    /// Use the type defined in the source code
49    Preserve,
50}
51
52/// Style of underscores in number literals
53#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
54#[serde(rename_all = "snake_case")]
55pub enum NumberUnderscore {
56    /// Use the underscores defined in the source code
57    Preserve,
58    /// Remove all underscores
59    #[default]
60    Remove,
61    /// Add an underscore every thousand, if greater than 9999
62    /// e.g. 1000 -> 1000 and 10000 -> 10_000
63    Thousands,
64}
65
66impl NumberUnderscore {
67    /// Returns true if the option is `Preserve`
68    #[inline]
69    pub fn is_preserve(self) -> bool {
70        matches!(self, Self::Preserve)
71    }
72
73    /// Returns true if the option is `Remove`
74    #[inline]
75    pub fn is_remove(self) -> bool {
76        matches!(self, Self::Remove)
77    }
78
79    /// Returns true if the option is `Remove`
80    #[inline]
81    pub fn is_thousands(self) -> bool {
82        matches!(self, Self::Thousands)
83    }
84}
85
86/// Style of underscores in hex literals
87#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
88#[serde(rename_all = "snake_case")]
89pub enum HexUnderscore {
90    /// Use the underscores defined in the source code
91    Preserve,
92    /// Remove all underscores
93    #[default]
94    Remove,
95    /// Add underscore as separator between byte boundaries
96    Bytes,
97}
98
99impl HexUnderscore {
100    /// Returns true if the option is `Preserve`
101    #[inline]
102    pub fn is_preserve(self) -> bool {
103        matches!(self, Self::Preserve)
104    }
105
106    /// Returns true if the option is `Remove`
107    #[inline]
108    pub fn is_remove(self) -> bool {
109        matches!(self, Self::Remove)
110    }
111
112    /// Returns true if the option is `Remove`
113    #[inline]
114    pub fn is_bytes(self) -> bool {
115        matches!(self, Self::Bytes)
116    }
117}
118
119/// Style of string quotes
120#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)]
121#[serde(rename_all = "snake_case")]
122pub enum QuoteStyle {
123    /// Use double quotes where possible
124    Double,
125    /// Use single quotes where possible
126    Single,
127    /// Use quotation mark defined in the source code
128    Preserve,
129}
130
131impl QuoteStyle {
132    /// Get associated quotation mark with option
133    pub fn quote(self) -> Option<char> {
134        match self {
135            Self::Double => Some('"'),
136            Self::Single => Some('\''),
137            Self::Preserve => None,
138        }
139    }
140}
141
142/// Style of single line blocks in statements
143#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)]
144#[serde(rename_all = "snake_case")]
145pub enum SingleLineBlockStyle {
146    /// Prefer single line block when possible
147    Single,
148    /// Always use multiline block
149    Multi,
150    /// Preserve the original style
151    Preserve,
152}
153
154/// Style of function header in case it doesn't fit
155#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)]
156#[serde(rename_all = "snake_case")]
157pub enum MultilineFuncHeaderStyle {
158    /// Write function parameters multiline first.
159    ParamsFirst,
160    /// Write function parameters multiline first when there is more than one param.
161    ParamsFirstMulti,
162    /// Write function attributes multiline first.
163    AttributesFirst,
164    /// If function params or attrs are multiline.
165    /// split the rest
166    All,
167    /// Same as `All` but writes function params multiline even when there is a single param.
168    AllParams,
169}
170
171/// Style of indent
172#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
173#[serde(rename_all = "snake_case")]
174pub enum IndentStyle {
175    #[default]
176    Space,
177    Tab,
178}
179
180impl Default for FormatterConfig {
181    fn default() -> Self {
182        Self {
183            line_length: 120,
184            tab_width: 4,
185            style: IndentStyle::Space,
186            bracket_spacing: false,
187            int_types: IntTypes::Long,
188            multiline_func_header: MultilineFuncHeaderStyle::AttributesFirst,
189            quote_style: QuoteStyle::Double,
190            number_underscore: NumberUnderscore::Preserve,
191            hex_underscore: HexUnderscore::Remove,
192            single_line_statement_blocks: SingleLineBlockStyle::Preserve,
193            override_spacing: false,
194            wrap_comments: false,
195            ignore: vec![],
196            contract_new_lines: false,
197            sort_imports: false,
198        }
199    }
200}