foundry_config/
fmt.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
//! Configuration specific to the `forge fmt` command and the `forge_fmt` package

use serde::{Deserialize, Serialize};

/// Contains the config and rule set
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub struct FormatterConfig {
    /// Maximum line length where formatter will try to wrap the line
    pub line_length: usize,
    /// Number of spaces per indentation level
    pub tab_width: usize,
    /// Print spaces between brackets
    pub bracket_spacing: bool,
    /// Style of uint/int256 types
    pub int_types: IntTypes,
    /// Style of multiline function header in case it doesn't fit
    pub multiline_func_header: MultilineFuncHeaderStyle,
    /// Style of quotation marks
    pub quote_style: QuoteStyle,
    /// Style of underscores in number literals
    pub number_underscore: NumberUnderscore,
    /// Style of underscores in hex literals
    pub hex_underscore: HexUnderscore,
    /// Style of single line blocks in statements
    pub single_line_statement_blocks: SingleLineBlockStyle,
    /// Print space in state variable, function and modifier `override` attribute
    pub override_spacing: bool,
    /// Wrap comments on `line_length` reached
    pub wrap_comments: bool,
    /// Globs to ignore
    pub ignore: Vec<String>,
    /// Add new line at start and end of contract declarations
    pub contract_new_lines: bool,
    /// Sort import statements alphabetically in groups (a group is separated by a newline).
    pub sort_imports: bool,
}

/// Style of uint/int256 types
#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum IntTypes {
    /// Print the explicit uint256 or int256
    Long,
    /// Print the implicit uint or int
    Short,
    /// Use the type defined in the source code
    Preserve,
}

/// Style of underscores in number literals
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum NumberUnderscore {
    /// Use the underscores defined in the source code
    Preserve,
    /// Remove all underscores
    #[default]
    Remove,
    /// Add an underscore every thousand, if greater than 9999
    /// e.g. 1000 -> 1000 and 10000 -> 10_000
    Thousands,
}

impl NumberUnderscore {
    /// Returns true if the option is `Preserve`
    #[inline]
    pub fn is_preserve(self) -> bool {
        matches!(self, Self::Preserve)
    }

    /// Returns true if the option is `Remove`
    #[inline]
    pub fn is_remove(self) -> bool {
        matches!(self, Self::Remove)
    }

    /// Returns true if the option is `Remove`
    #[inline]
    pub fn is_thousands(self) -> bool {
        matches!(self, Self::Thousands)
    }
}

/// Style of underscores in hex literals
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum HexUnderscore {
    /// Use the underscores defined in the source code
    Preserve,
    /// Remove all underscores
    #[default]
    Remove,
    /// Add underscore as separator between byte boundaries
    Bytes,
}

impl HexUnderscore {
    /// Returns true if the option is `Preserve`
    #[inline]
    pub fn is_preserve(self) -> bool {
        matches!(self, Self::Preserve)
    }

    /// Returns true if the option is `Remove`
    #[inline]
    pub fn is_remove(self) -> bool {
        matches!(self, Self::Remove)
    }

    /// Returns true if the option is `Remove`
    #[inline]
    pub fn is_bytes(self) -> bool {
        matches!(self, Self::Bytes)
    }
}

/// Style of string quotes
#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum QuoteStyle {
    /// Use double quotes where possible
    Double,
    /// Use single quotes where possible
    Single,
    /// Use quotation mark defined in the source code
    Preserve,
}

impl QuoteStyle {
    /// Get associated quotation mark with option
    pub fn quote(self) -> Option<char> {
        match self {
            Self::Double => Some('"'),
            Self::Single => Some('\''),
            Self::Preserve => None,
        }
    }
}

/// Style of single line blocks in statements
#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum SingleLineBlockStyle {
    /// Prefer single line block when possible
    Single,
    /// Always use multiline block
    Multi,
    /// Preserve the original style
    Preserve,
}

/// Style of function header in case it doesn't fit
#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum MultilineFuncHeaderStyle {
    /// Write function parameters multiline first.
    ParamsFirst,
    /// Write function parameters multiline first when there is more than one param.
    ParamsFirstMulti,
    /// Write function attributes multiline first.
    AttributesFirst,
    /// If function params or attrs are multiline.
    /// split the rest
    All,
    /// Same as `All` but writes function params multiline even when there is a single param.
    AllParams,
}

impl Default for FormatterConfig {
    fn default() -> Self {
        Self {
            line_length: 120,
            tab_width: 4,
            bracket_spacing: false,
            int_types: IntTypes::Long,
            multiline_func_header: MultilineFuncHeaderStyle::AttributesFirst,
            quote_style: QuoteStyle::Double,
            number_underscore: NumberUnderscore::Preserve,
            hex_underscore: HexUnderscore::Remove,
            single_line_statement_blocks: SingleLineBlockStyle::Preserve,
            override_spacing: false,
            wrap_comments: false,
            ignore: vec![],
            contract_new_lines: false,
            sort_imports: false,
        }
    }
}