Skip to main content

foundry_config/
utils.rs

1//! Utility functions
2
3use 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
14// TODO: Why do these exist separately from `Config::load`?
15
16/// Loads the config for the current project workspace.
17pub fn load_config() -> eyre::Result<Config> {
18    load_config_with_root(None)
19}
20
21/// Loads the config for the current project workspace or the provided root path.
22pub 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
30/// Returns the path of the top-level directory of the working git tree.
31pub 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
37/// Returns the root path to set for the project root.
38///
39/// Traverse the dir tree up and look for a `foundry.toml` file starting at the given path or cwd,
40/// but only until the root dir of the current repo so that:
41///
42/// ```text
43/// -- foundry.toml
44///
45/// -- repo
46///   |__ .git
47///   |__sub
48///      |__ [given_path | cwd]
49/// ```
50///
51/// will still detect `repo` as root.
52///
53/// Returns `repo` or `cwd` if no `foundry.toml` is found in the tree.
54///
55/// Returns an error if:
56/// - `cwd` is `Some` and is not a valid directory;
57/// - `cwd` is `None` and the [`std::env::current_dir`] call fails.
58pub 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        // Don't look outside of the git repo if it exists.
67        .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
73/// Returns all [`Remapping`]s contained in the `remappings` str separated by newlines
74///
75/// # Example
76///
77/// ```
78/// use foundry_config::remappings_from_newline;
79/// let remappings: Result<Vec<_>, _> = remappings_from_newline(
80///     r#"
81///              file-ds-test/=lib/ds-test/
82///              file-other/=lib/other/
83///          "#,
84/// )
85/// .collect();
86/// ```
87pub 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
93/// Returns the remappings from the given var
94///
95/// Returns `None` if the env var is not set, otherwise all Remappings, See
96/// `remappings_from_newline`
97pub 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
102/// Converts the `val` into a `figment::Value::Array`
103///
104/// The values should be separated by commas, surrounding brackets are also supported `[a,b,c]`
105pub 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
121/// Splits a shell-like argument string into argv parts without invoking a shell.
122///
123/// This supports whitespace separation, single and double quotes, and backslash escaping. It is
124/// intentionally smaller than a shell parser: expansions, redirection, pipelines, and command
125/// separators are treated as plain argument text by callers.
126///
127/// Returns `Err(quote_char)` when the input contains an unterminated quote.
128pub fn split_quoted_args(args: &str) -> Result<Vec<String>, char> {
129    let mut parts = Vec::new();
130    let mut current = String::new();
131    let mut quote = None;
132    let mut escaped = false;
133    let mut token_started = false;
134
135    for ch in args.chars() {
136        if escaped {
137            current.push(ch);
138            escaped = false;
139            token_started = true;
140            continue;
141        }
142        if ch == '\\' {
143            escaped = true;
144            token_started = true;
145            continue;
146        }
147        if let Some(quote_ch) = quote {
148            if ch == quote_ch {
149                quote = None;
150            } else {
151                current.push(ch);
152            }
153            token_started = true;
154            continue;
155        }
156        if matches!(ch, '"' | '\'') {
157            quote = Some(ch);
158            token_started = true;
159        } else if ch.is_whitespace() {
160            if token_started {
161                parts.push(std::mem::take(&mut current));
162                token_started = false;
163            }
164        } else {
165            current.push(ch);
166            token_started = true;
167        }
168    }
169
170    if let Some(quote_ch) = quote {
171        return Err(quote_ch);
172    }
173    if escaped {
174        current.push('\\');
175        token_started = true;
176    }
177    if token_started {
178        parts.push(current);
179    }
180
181    Ok(parts)
182}
183
184/// Returns a list of _unique_ paths to all folders under `root` that contain a `foundry.toml` file
185///
186/// This will also resolve symlinks
187///
188/// # Example
189///
190/// ```no_run
191/// use foundry_config::utils;
192/// let dirs = utils::foundry_toml_dirs("./lib");
193/// ```
194///
195/// for following layout this will return
196/// `["lib/dep1", "lib/dep2"]`
197///
198/// ```text
199/// lib
200/// └── dep1
201/// │   ├── foundry.toml
202/// └── dep2
203///     ├── foundry.toml
204/// ```
205pub fn foundry_toml_dirs(root: impl AsRef<Path>) -> Vec<PathBuf> {
206    walkdir::WalkDir::new(root)
207        .max_depth(1)
208        .into_iter()
209        .filter_map(Result::ok)
210        .filter(|e| e.file_type().is_dir())
211        .filter_map(|e| dunce::canonicalize(e.path()).ok())
212        .filter(|p| p.join(Config::FILE_NAME).exists())
213        .collect()
214}
215
216/// Returns a remapping for the given dir
217pub(crate) fn get_dir_remapping(dir: impl AsRef<Path>) -> Option<Remapping> {
218    let dir = dir.as_ref();
219    if let Some(dir_name) = dir.file_name().and_then(|s| s.to_str()).filter(|s| !s.is_empty()) {
220        let mut r = Remapping {
221            context: None,
222            name: format!("{dir_name}/"),
223            path: format!("{}", dir.display()),
224        };
225        if !r.path.ends_with('/') {
226            r.path.push('/')
227        }
228        Some(r)
229    } else {
230        None
231    }
232}
233
234/// Deserialize stringified percent. The value must be between 0 and 100 inclusive.
235pub(crate) fn deserialize_stringified_percent<'de, D>(deserializer: D) -> Result<u32, D::Error>
236where
237    D: Deserializer<'de>,
238{
239    let num: U256 = Numeric::deserialize(deserializer)?.into();
240    let num: u64 = num.try_into().map_err(serde::de::Error::custom)?;
241    if num <= 100 {
242        num.try_into().map_err(serde::de::Error::custom)
243    } else {
244        Err(serde::de::Error::custom("percent must be lte 100"))
245    }
246}
247
248/// Deserialize a `u64` or "max" for `u64::MAX`.
249pub(crate) fn deserialize_u64_or_max<'de, D>(deserializer: D) -> Result<u64, D::Error>
250where
251    D: Deserializer<'de>,
252{
253    #[derive(Deserialize)]
254    #[serde(untagged)]
255    enum Val {
256        Number(u64),
257        String(String),
258    }
259
260    match Val::deserialize(deserializer)? {
261        Val::Number(num) => Ok(num),
262        Val::String(s) if s.eq_ignore_ascii_case("max") => Ok(u64::MAX),
263        Val::String(s) => s.parse::<u64>().map_err(D::Error::custom),
264    }
265}
266
267/// Deserialize a `usize` or "max" for `usize::MAX`.
268pub(crate) fn deserialize_usize_or_max<'de, D>(deserializer: D) -> Result<usize, D::Error>
269where
270    D: Deserializer<'de>,
271{
272    deserialize_u64_or_max(deserializer)?.try_into().map_err(D::Error::custom)
273}
274
275/// Serialize a `usize` as `"max"` if it equals `usize::MAX`, as a string if it exceeds
276/// `i64::MAX` (TOML integer limit), or as a plain number otherwise.
277pub(crate) fn serialize_usize_or_max<S>(value: &usize, serializer: S) -> Result<S::Ok, S::Error>
278where
279    S: Serializer,
280{
281    if *value == usize::MAX {
282        serializer.serialize_str("max")
283    } else if *value > i64::MAX as usize {
284        serializer.serialize_str(&value.to_string())
285    } else {
286        serializer.serialize_u64(*value as u64)
287    }
288}
289
290/// Deserialize into `U256` from either a `u64`, a `U256` hex string, or a decimal string.
291pub fn deserialize_u64_to_u256<'de, D>(deserializer: D) -> Result<U256, D::Error>
292where
293    D: Deserializer<'de>,
294{
295    #[derive(Deserialize)]
296    #[serde(untagged)]
297    enum NumericValue {
298        U256(U256),
299        U64(u64),
300        String(String),
301    }
302
303    match NumericValue::deserialize(deserializer)? {
304        NumericValue::U64(n) => Ok(U256::from(n)),
305        NumericValue::U256(n) => Ok(n),
306        NumericValue::String(s) => {
307            // Handle decimal strings (e.g., "18446744073709551615")
308            U256::from_str(&s).map_err(D::Error::custom)
309        }
310    }
311}
312
313/// Serialize `U256` as `u64` if it fits, otherwise as a hex string.
314/// If the number fits into a i64, serialize it as number without quotation marks.
315/// If the number fits into a u64, serialize it as a stringified number with quotation marks.
316/// Otherwise, serialize it as a hex string with quotation marks.
317pub fn serialize_u64_or_u256<S>(n: &U256, serializer: S) -> Result<S::Ok, S::Error>
318where
319    S: Serializer,
320{
321    // The TOML specification handles integers as i64 so the number representation is limited to
322    // i64. If the number is larger than `i64::MAX` and up to `u64::MAX`, we serialize it as a
323    // string to avoid losing precision.
324    if let Ok(n_i64) = i64::try_from(*n) {
325        serializer.serialize_i64(n_i64)
326    } else if let Ok(n_u64) = u64::try_from(*n) {
327        serializer.serialize_str(&n_u64.to_string())
328    } else {
329        serializer.serialize_str(&format!("{n:#x}"))
330    }
331}
332
333/// Helper type to parse both `u64` and `U256`
334#[derive(Clone, Copy, Deserialize)]
335#[serde(untagged)]
336pub enum Numeric {
337    /// A [U256] value.
338    U256(U256),
339    /// A `u64` value.
340    Num(u64),
341}
342
343impl From<Numeric> for U256 {
344    fn from(n: Numeric) -> Self {
345        match n {
346            Numeric::U256(n) => n,
347            Numeric::Num(n) => Self::from(n),
348        }
349    }
350}
351
352impl FromStr for Numeric {
353    type Err = String;
354
355    fn from_str(s: &str) -> Result<Self, Self::Err> {
356        if s.starts_with("0x") {
357            U256::from_str_radix(s, 16).map(Numeric::U256).map_err(|err| err.to_string())
358        } else {
359            U256::from_str(s).map(Numeric::U256).map_err(|err| err.to_string())
360        }
361    }
362}