solang_forge_fmt/
config.rs

1// SPDX-License-Identifier: Apache-2.0
2
3//! Configuration specific to the `forge fmt` command and the `forge_fmt` package
4
5use serde::{Deserialize, Serialize};
6
7/// Contains the config and rule set
8#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
9pub struct FormatterConfig {
10    /// Maximum line length where formatter will try to wrap the line
11    pub line_length: usize,
12    /// Number of spaces per indentation level
13    pub tab_width: usize,
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, NumberUnderscore::Preserve)
71    }
72
73    /// Returns true if the option is `Remove`
74    #[inline]
75    pub fn is_remove(self) -> bool {
76        matches!(self, NumberUnderscore::Remove)
77    }
78
79    /// Returns true if the option is `Remove`
80    #[inline]
81    pub fn is_thousands(self) -> bool {
82        matches!(self, NumberUnderscore::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, HexUnderscore::Preserve)
104    }
105
106    /// Returns true if the option is `Remove`
107    #[inline]
108    pub fn is_remove(self) -> bool {
109        matches!(self, HexUnderscore::Remove)
110    }
111
112    /// Returns true if the option is `Remove`
113    #[inline]
114    pub fn is_bytes(self) -> bool {
115        matches!(self, HexUnderscore::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            QuoteStyle::Double => Some('"'),
136            QuoteStyle::Single => Some('\''),
137            QuoteStyle::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 attributes multiline first
161    AttributesFirst,
162    /// If function params or attrs are multiline
163    /// split the rest
164    All,
165}
166
167impl Default for FormatterConfig {
168    fn default() -> Self {
169        FormatterConfig {
170            line_length: 120,
171            tab_width: 4,
172            bracket_spacing: false,
173            int_types: IntTypes::Long,
174            multiline_func_header: MultilineFuncHeaderStyle::AttributesFirst,
175            quote_style: QuoteStyle::Double,
176            number_underscore: NumberUnderscore::Preserve,
177            hex_underscore: HexUnderscore::Remove,
178            single_line_statement_blocks: SingleLineBlockStyle::Preserve,
179            override_spacing: false,
180            wrap_comments: false,
181            ignore: vec![],
182            contract_new_lines: false,
183            sort_imports: false,
184        }
185    }
186}