1use crate::Config;
4use alloy_primitives::U256;
5use figment::value::Value;
6use foundry_compilers::artifacts::remappings::{Remapping, RemappingError};
7use serde::{Deserialize, Deserializer, Serializer, de::Error};
8use std::{
9 io,
10 path::{Path, PathBuf},
11 str::FromStr,
12};
13
14pub fn load_config() -> eyre::Result<Config> {
18 load_config_with_root(None)
19}
20
21pub fn load_config_with_root(root: Option<&Path>) -> eyre::Result<Config> {
23 let root = match root {
24 Some(root) => root,
25 None => &find_project_root(None)?,
26 };
27 Ok(Config::load_with_root(root)?.sanitized())
28}
29
30pub fn find_git_root(relative_to: &Path) -> io::Result<Option<PathBuf>> {
32 let root =
33 if relative_to.is_absolute() { relative_to } else { &dunce::canonicalize(relative_to)? };
34 Ok(root.ancestors().find(|p| p.join(".git").exists()).map(Path::to_path_buf))
35}
36
37pub fn find_project_root(cwd: Option<&Path>) -> io::Result<PathBuf> {
59 let cwd = match cwd {
60 Some(path) => path,
61 None => &std::env::current_dir()?,
62 };
63 let boundary = find_git_root(cwd)?;
64 let found = cwd
65 .ancestors()
66 .take_while(|p| if let Some(boundary) = &boundary { p.starts_with(boundary) } else { true })
68 .find(|p| p.join(Config::FILE_NAME).is_file())
69 .map(Path::to_path_buf);
70 Ok(found.or(boundary).unwrap_or_else(|| cwd.to_path_buf()))
71}
72
73pub fn remappings_from_newline(
88 remappings: &str,
89) -> impl Iterator<Item = Result<Remapping, RemappingError>> + '_ {
90 remappings.lines().map(|x| x.trim()).filter(|x| !x.is_empty()).map(Remapping::from_str)
91}
92
93pub fn remappings_from_env_var(env_var: &str) -> Option<Result<Vec<Remapping>, RemappingError>> {
98 let val = std::env::var(env_var).ok()?;
99 Some(remappings_from_newline(&val).collect())
100}
101
102pub fn to_array_value(val: &str) -> Result<Value, figment::Error> {
106 let value: Value = match Value::from(val) {
107 Value::String(_, val) => val
108 .trim_start_matches('[')
109 .trim_end_matches(']')
110 .split(',')
111 .map(|s| s.to_string())
112 .collect::<Vec<_>>()
113 .into(),
114 Value::Empty(_, _) => Vec::<Value>::new().into(),
115 val @ Value::Array(_, _) => val,
116 _ => return Err(format!("Invalid value `{val}`, expected an array").into()),
117 };
118 Ok(value)
119}
120
121pub fn foundry_toml_dirs(root: impl AsRef<Path>) -> Vec<PathBuf> {
143 walkdir::WalkDir::new(root)
144 .max_depth(1)
145 .into_iter()
146 .filter_map(Result::ok)
147 .filter(|e| e.file_type().is_dir())
148 .filter_map(|e| dunce::canonicalize(e.path()).ok())
149 .filter(|p| p.join(Config::FILE_NAME).exists())
150 .collect()
151}
152
153pub(crate) fn get_dir_remapping(dir: impl AsRef<Path>) -> Option<Remapping> {
155 let dir = dir.as_ref();
156 if let Some(dir_name) = dir.file_name().and_then(|s| s.to_str()).filter(|s| !s.is_empty()) {
157 let mut r = Remapping {
158 context: None,
159 name: format!("{dir_name}/"),
160 path: format!("{}", dir.display()),
161 };
162 if !r.path.ends_with('/') {
163 r.path.push('/')
164 }
165 Some(r)
166 } else {
167 None
168 }
169}
170
171pub(crate) fn deserialize_stringified_percent<'de, D>(deserializer: D) -> Result<u32, D::Error>
173where
174 D: Deserializer<'de>,
175{
176 let num: U256 = Numeric::deserialize(deserializer)?.into();
177 let num: u64 = num.try_into().map_err(serde::de::Error::custom)?;
178 if num <= 100 {
179 num.try_into().map_err(serde::de::Error::custom)
180 } else {
181 Err(serde::de::Error::custom("percent must be lte 100"))
182 }
183}
184
185pub(crate) fn deserialize_u64_or_max<'de, D>(deserializer: D) -> Result<u64, D::Error>
187where
188 D: Deserializer<'de>,
189{
190 #[derive(Deserialize)]
191 #[serde(untagged)]
192 enum Val {
193 Number(u64),
194 String(String),
195 }
196
197 match Val::deserialize(deserializer)? {
198 Val::Number(num) => Ok(num),
199 Val::String(s) if s.eq_ignore_ascii_case("max") => Ok(u64::MAX),
200 Val::String(s) => s.parse::<u64>().map_err(D::Error::custom),
201 }
202}
203
204pub(crate) fn deserialize_usize_or_max<'de, D>(deserializer: D) -> Result<usize, D::Error>
206where
207 D: Deserializer<'de>,
208{
209 deserialize_u64_or_max(deserializer)?.try_into().map_err(D::Error::custom)
210}
211
212pub(crate) fn serialize_usize_or_max<S>(value: &usize, serializer: S) -> Result<S::Ok, S::Error>
215where
216 S: Serializer,
217{
218 if *value == usize::MAX {
219 serializer.serialize_str("max")
220 } else if *value > i64::MAX as usize {
221 serializer.serialize_str(&value.to_string())
222 } else {
223 serializer.serialize_u64(*value as u64)
224 }
225}
226
227pub fn deserialize_u64_to_u256<'de, D>(deserializer: D) -> Result<U256, D::Error>
229where
230 D: Deserializer<'de>,
231{
232 #[derive(Deserialize)]
233 #[serde(untagged)]
234 enum NumericValue {
235 U256(U256),
236 U64(u64),
237 String(String),
238 }
239
240 match NumericValue::deserialize(deserializer)? {
241 NumericValue::U64(n) => Ok(U256::from(n)),
242 NumericValue::U256(n) => Ok(n),
243 NumericValue::String(s) => {
244 U256::from_str(&s).map_err(D::Error::custom)
246 }
247 }
248}
249
250pub fn serialize_u64_or_u256<S>(n: &U256, serializer: S) -> Result<S::Ok, S::Error>
255where
256 S: Serializer,
257{
258 if let Ok(n_i64) = i64::try_from(*n) {
262 serializer.serialize_i64(n_i64)
263 } else if let Ok(n_u64) = u64::try_from(*n) {
264 serializer.serialize_str(&n_u64.to_string())
265 } else {
266 serializer.serialize_str(&format!("{n:#x}"))
267 }
268}
269
270#[derive(Clone, Copy, Deserialize)]
272#[serde(untagged)]
273pub enum Numeric {
274 U256(U256),
276 Num(u64),
278}
279
280impl From<Numeric> for U256 {
281 fn from(n: Numeric) -> Self {
282 match n {
283 Numeric::U256(n) => n,
284 Numeric::Num(n) => Self::from(n),
285 }
286 }
287}
288
289impl FromStr for Numeric {
290 type Err = String;
291
292 fn from_str(s: &str) -> Result<Self, Self::Err> {
293 if s.starts_with("0x") {
294 U256::from_str_radix(s, 16).map(Numeric::U256).map_err(|err| err.to_string())
295 } else {
296 U256::from_str(s).map(Numeric::U256).map_err(|err| err.to_string())
297 }
298 }
299}