1use alloy_primitives::map::HashSet;
3use figment::providers::{Format, Toml};
4use serde::{Deserialize, Deserializer, Serialize, Serializer};
5use std::{error::Error, fmt, str::FromStr};
6
7#[derive(Clone, PartialEq)]
9pub struct ExtractConfigError {
10 pub(crate) error: figment::Error,
12}
13
14impl ExtractConfigError {
15 pub const fn new(error: figment::Error) -> Self {
17 Self { error }
18 }
19}
20
21impl fmt::Display for ExtractConfigError {
22 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
23 let mut unique_errors = Vec::with_capacity(self.error.count());
24 let mut unique = HashSet::with_capacity(self.error.count());
25 for err in self.error.clone() {
26 let err = if err
27 .metadata
28 .as_ref()
29 .map(|meta| meta.name.contains(Toml::NAME))
30 .unwrap_or_default()
31 {
32 FoundryConfigError::Toml(err)
33 } else {
34 FoundryConfigError::Other(err)
35 };
36
37 if unique.insert(err.to_string()) {
38 unique_errors.push(err);
39 }
40 }
41 writeln!(f, "failed to extract foundry config:")?;
42 for err in unique_errors {
43 writeln!(f, "{err}")?;
44 }
45 Ok(())
46 }
47}
48
49impl fmt::Debug for ExtractConfigError {
50 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
51 fmt::Display::fmt(self, f)
52 }
53}
54
55impl Error for ExtractConfigError {
56 fn source(&self) -> Option<&(dyn Error + 'static)> {
57 Error::source(&self.error)
58 }
59}
60
61#[derive(Clone, Debug, PartialEq)]
63pub enum FoundryConfigError {
64 Toml(figment::Error),
66 Other(figment::Error),
68}
69
70impl fmt::Display for FoundryConfigError {
71 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
72 let fmt_err = |err: &figment::Error, f: &mut fmt::Formatter<'_>| {
73 write!(f, "{err}")?;
74 if !err.path.is_empty() {
75 write!(f, " for setting `{}`", err.path.join("."))?;
77 }
78 Ok(())
79 };
80
81 match self {
82 Self::Toml(err) => {
83 f.write_str("foundry.toml error: ")?;
84 fmt_err(err, f)
85 }
86 Self::Other(err) => {
87 f.write_str("foundry config error: ")?;
88 fmt_err(err, f)
89 }
90 }
91 }
92}
93
94impl Error for FoundryConfigError {
95 fn source(&self) -> Option<&(dyn Error + 'static)> {
96 match self {
97 Self::Other(error) | Self::Toml(error) => Error::source(error),
98 }
99 }
100}
101
102#[derive(Clone, Copy, Debug, PartialEq, Eq)]
104pub enum SolidityErrorCode {
105 SpdxLicenseNotProvided,
107 VisibilityForConstructorIsIgnored,
110 ContractExceeds24576Bytes,
113 ContractInitCodeSizeExceeds49152Bytes,
115 FunctionStateMutabilityCanBeRestricted,
117 UnusedLocalVariable,
119 UnusedFunctionParameter,
122 ReturnValueOfCallsNotUsed,
124 InterfacesExplicitlyVirtual,
126 PayableNoReceiveEther,
129 ShadowsExistingDeclaration,
131 DeclarationSameNameAsAnother,
133 UnnamedReturnVariable,
135 Unreachable,
137 PragmaSolidity,
139 TransientStorageUsed,
141 TooManyWarnings,
143 TransferDeprecated,
145 NatspecMemorySafeAssemblyDeprecated,
147 ContractExceeds65536Bytes,
149 ContractInitCodeSizeExceeds131072Bytes,
152 Other(u64),
154}
155
156impl SolidityErrorCode {
157 pub const fn as_str(&self) -> Result<&'static str, u64> {
161 let s = match self {
162 Self::SpdxLicenseNotProvided => "license",
163 Self::VisibilityForConstructorIsIgnored => "constructor-visibility",
164 Self::ContractExceeds24576Bytes => "code-size",
165 Self::ContractInitCodeSizeExceeds49152Bytes => "init-code-size",
166 Self::FunctionStateMutabilityCanBeRestricted => "func-mutability",
167 Self::UnusedLocalVariable => "unused-var",
168 Self::UnusedFunctionParameter => "unused-param",
169 Self::ReturnValueOfCallsNotUsed => "unused-return",
170 Self::InterfacesExplicitlyVirtual => "virtual-interfaces",
171 Self::PayableNoReceiveEther => "missing-receive-ether",
172 Self::ShadowsExistingDeclaration => "shadowing",
173 Self::DeclarationSameNameAsAnother => "same-varname",
174 Self::UnnamedReturnVariable => "unnamed-return",
175 Self::Unreachable => "unreachable",
176 Self::PragmaSolidity => "pragma-solidity",
177 Self::TransientStorageUsed => "transient-storage",
178 Self::TooManyWarnings => "too-many-warnings",
179 Self::TransferDeprecated => "transfer-deprecated",
180 Self::NatspecMemorySafeAssemblyDeprecated => "natspec-memory-safe-assembly-deprecated",
181 Self::ContractExceeds65536Bytes => "code-size-amsterdam",
182 Self::ContractInitCodeSizeExceeds131072Bytes => "init-code-size-amsterdam",
183 Self::Other(code) => return Err(*code),
184 };
185 Ok(s)
186 }
187}
188
189impl From<SolidityErrorCode> for u64 {
190 fn from(code: SolidityErrorCode) -> Self {
191 match code {
192 SolidityErrorCode::SpdxLicenseNotProvided => 1878,
193 SolidityErrorCode::VisibilityForConstructorIsIgnored => 2462,
194 SolidityErrorCode::ContractExceeds24576Bytes => 5574,
195 SolidityErrorCode::ContractInitCodeSizeExceeds49152Bytes => 3860,
196 SolidityErrorCode::FunctionStateMutabilityCanBeRestricted => 2018,
197 SolidityErrorCode::UnusedLocalVariable => 2072,
198 SolidityErrorCode::UnusedFunctionParameter => 5667,
199 SolidityErrorCode::ReturnValueOfCallsNotUsed => 9302,
200 SolidityErrorCode::InterfacesExplicitlyVirtual => 5815,
201 SolidityErrorCode::PayableNoReceiveEther => 3628,
202 SolidityErrorCode::ShadowsExistingDeclaration => 2519,
203 SolidityErrorCode::DeclarationSameNameAsAnother => 8760,
204 SolidityErrorCode::UnnamedReturnVariable => 6321,
205 SolidityErrorCode::Unreachable => 5740,
206 SolidityErrorCode::PragmaSolidity => 3420,
207 SolidityErrorCode::TransientStorageUsed => 2394,
208 SolidityErrorCode::TooManyWarnings => 4591,
209 SolidityErrorCode::TransferDeprecated => 9207,
210 SolidityErrorCode::NatspecMemorySafeAssemblyDeprecated => 2424,
211 SolidityErrorCode::ContractExceeds65536Bytes => 5776,
212 SolidityErrorCode::ContractInitCodeSizeExceeds131072Bytes => 7868,
213 SolidityErrorCode::Other(code) => code,
214 }
215 }
216}
217
218impl fmt::Display for SolidityErrorCode {
219 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
220 match self.as_str() {
221 Ok(name) => name.fmt(f),
222 Err(code) => code.fmt(f),
223 }
224 }
225}
226
227impl FromStr for SolidityErrorCode {
228 type Err = String;
229
230 fn from_str(s: &str) -> Result<Self, Self::Err> {
231 let code = match s {
232 "license" => Self::SpdxLicenseNotProvided,
233 "constructor-visibility" => Self::VisibilityForConstructorIsIgnored,
234 "code-size" => Self::ContractExceeds24576Bytes,
235 "init-code-size" => Self::ContractInitCodeSizeExceeds49152Bytes,
236 "func-mutability" => Self::FunctionStateMutabilityCanBeRestricted,
237 "unused-var" => Self::UnusedLocalVariable,
238 "unused-param" => Self::UnusedFunctionParameter,
239 "unused-return" => Self::ReturnValueOfCallsNotUsed,
240 "virtual-interfaces" => Self::InterfacesExplicitlyVirtual,
241 "missing-receive-ether" => Self::PayableNoReceiveEther,
242 "shadowing" => Self::ShadowsExistingDeclaration,
243 "same-varname" => Self::DeclarationSameNameAsAnother,
244 "unnamed-return" => Self::UnnamedReturnVariable,
245 "unreachable" => Self::Unreachable,
246 "pragma-solidity" => Self::PragmaSolidity,
247 "transient-storage" => Self::TransientStorageUsed,
248 "too-many-warnings" => Self::TooManyWarnings,
249 "transfer-deprecated" => Self::TransferDeprecated,
250 "natspec-memory-safe-assembly-deprecated" => Self::NatspecMemorySafeAssemblyDeprecated,
251 "code-size-amsterdam" => Self::ContractExceeds65536Bytes,
252 "init-code-size-amsterdam" => Self::ContractInitCodeSizeExceeds131072Bytes,
253 _ => return Err(format!("Unknown variant {s}")),
254 };
255
256 Ok(code)
257 }
258}
259
260impl From<u64> for SolidityErrorCode {
261 fn from(code: u64) -> Self {
262 match code {
263 1878 => Self::SpdxLicenseNotProvided,
264 2462 => Self::VisibilityForConstructorIsIgnored,
265 5574 => Self::ContractExceeds24576Bytes,
266 3860 => Self::ContractInitCodeSizeExceeds49152Bytes,
267 2018 => Self::FunctionStateMutabilityCanBeRestricted,
268 2072 => Self::UnusedLocalVariable,
269 5667 => Self::UnusedFunctionParameter,
270 9302 => Self::ReturnValueOfCallsNotUsed,
271 5815 => Self::InterfacesExplicitlyVirtual,
272 3628 => Self::PayableNoReceiveEther,
273 2519 => Self::ShadowsExistingDeclaration,
274 8760 => Self::DeclarationSameNameAsAnother,
275 6321 => Self::UnnamedReturnVariable,
276 5740 => Self::Unreachable,
277 3420 => Self::PragmaSolidity,
278 2394 => Self::TransientStorageUsed,
279 4591 => Self::TooManyWarnings,
280 9207 => Self::TransferDeprecated,
281 2424 => Self::NatspecMemorySafeAssemblyDeprecated,
282 5776 => Self::ContractExceeds65536Bytes,
283 7868 => Self::ContractInitCodeSizeExceeds131072Bytes,
284 other => Self::Other(other),
285 }
286 }
287}
288
289impl Serialize for SolidityErrorCode {
290 fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
291 where
292 S: Serializer,
293 {
294 match self.as_str() {
295 Ok(alias) => serializer.serialize_str(alias),
296 Err(code) => serializer.serialize_u64(code),
297 }
298 }
299}
300
301impl<'de> Deserialize<'de> for SolidityErrorCode {
302 fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
303 where
304 D: Deserializer<'de>,
305 {
306 #[derive(Deserialize)]
308 #[serde(untagged)]
309 enum SolCode {
310 Name(String),
311 Code(u64),
312 }
313
314 match SolCode::deserialize(deserializer)? {
315 SolCode::Code(code) => Ok(code.into()),
316 SolCode::Name(name) => name.parse().map_err(serde::de::Error::custom),
317 }
318 }
319}