Skip to main content

foundry_config/providers/
ext.rs

1use crate::{Config, extend, utils};
2use figment::{
3    Error, Figment, Metadata, Profile, Provider,
4    providers::{Env, Format, Toml},
5    value::{Dict, Map, Value},
6};
7use foundry_compilers::ProjectPathsConfig;
8use heck::ToSnakeCase;
9use std::{
10    cell::OnceCell,
11    path::{Path, PathBuf},
12};
13
14pub(crate) trait ProviderExt: Provider + Sized {
15    fn rename(
16        self,
17        from: impl Into<Profile>,
18        to: impl Into<Profile>,
19    ) -> RenameProfileProvider<Self> {
20        RenameProfileProvider::new(self, from, to)
21    }
22
23    fn wrap(
24        self,
25        wrapping_key: impl Into<Profile>,
26        profile: impl Into<Profile>,
27    ) -> WrapProfileProvider<Self> {
28        WrapProfileProvider::new(self, wrapping_key, profile)
29    }
30
31    fn strict_select(
32        self,
33        profiles: impl IntoIterator<Item = impl Into<Profile>>,
34    ) -> OptionalStrictProfileProvider<Self> {
35        OptionalStrictProfileProvider::new(self, profiles)
36    }
37
38    fn fallback(
39        self,
40        profile: impl Into<Profile>,
41        fallback: impl Into<Profile>,
42    ) -> FallbackProfileProvider<Self> {
43        FallbackProfileProvider::new(self, profile, fallback)
44    }
45
46    fn legacy_labels(self) -> LegacyLabelsProvider<Self> {
47        LegacyLabelsProvider(self)
48    }
49}
50
51impl<P: Provider> ProviderExt for P {}
52
53/// A convenience provider to retrieve a toml file.
54/// This will return an error if the env var is set but the file does not exist
55pub(crate) struct TomlFileProvider {
56    env_var: Option<&'static str>,
57    env_val: OnceCell<Option<String>>,
58    default: PathBuf,
59    cache: OnceCell<Result<Map<Profile, Dict>, Error>>,
60}
61
62impl TomlFileProvider {
63    pub(crate) const fn new(env_var: Option<&'static str>, default: PathBuf) -> Self {
64        Self { env_var, env_val: OnceCell::new(), default, cache: OnceCell::new() }
65    }
66
67    fn env_val(&self) -> Option<&str> {
68        self.env_val.get_or_init(|| self.env_var.and_then(Env::var)).as_deref()
69    }
70
71    fn file(&self) -> PathBuf {
72        self.env_val().map(PathBuf::from).unwrap_or_else(|| self.default.clone())
73    }
74
75    fn is_missing(&self) -> bool {
76        if let Some(file) = self.env_val() {
77            let path = Path::new(&file);
78            if !path.exists() {
79                return true;
80            }
81        }
82        false
83    }
84
85    /// Reads and processes the TOML configuration file, handling inheritance if configured.
86    fn read(&self) -> Result<Map<Profile, Dict>, Error> {
87        use serde::de::Error as _;
88
89        // Get the config file path and validate it exists
90        let local_path = self.file();
91        if !local_path.exists() {
92            if let Some(file) = self.env_val() {
93                return Err(Error::custom(format!(
94                    "Config file `{}` set in env var `{}` does not exist",
95                    file,
96                    self.env_var.unwrap()
97                )));
98            }
99            return Ok(Map::new());
100        }
101
102        // Create a provider for the local config file
103        let local_provider = Toml::file(local_path.clone()).nested();
104
105        // Parse the local config to check for extends field
106        let local_path_str = local_path.to_string_lossy();
107        let local_content = std::fs::read_to_string(&local_path)
108            .map_err(|e| Error::custom(e.to_string()).with_path(&local_path_str))?;
109        let partial_config: extend::ExtendsPartialConfig = toml::from_str(&local_content)
110            .map_err(|e| Error::custom(e.to_string()).with_path(&local_path_str))?;
111
112        // Check if the currently active profile has an 'extends' field
113        let selected_profile = Config::selected_profile();
114        let extends_config = partial_config.profile.as_ref().and_then(|profiles| {
115            let profile_str = selected_profile.to_string();
116            profiles.get(&profile_str).and_then(|cfg| cfg.extends.as_ref())
117        });
118
119        // If inheritance is configured, load and merge the base config
120        if let Some(extends_config) = extends_config {
121            let extends_path = extends_config.path();
122            let extends_strategy = extends_config.strategy();
123            let relative_base_path = PathBuf::from(extends_path);
124            let local_dir = local_path.parent().ok_or_else(|| {
125                Error::custom(format!(
126                    "Could not determine parent directory of config file: {}",
127                    local_path.display()
128                ))
129            })?;
130
131            let base_path =
132                foundry_compilers::utils::canonicalize(local_dir.join(&relative_base_path))
133                    .map_err(|e| {
134                        Error::custom(format!(
135                            "Failed to resolve inherited config path: {}: {e}",
136                            relative_base_path.display()
137                        ))
138                    })?;
139
140            // Validate the base config file exists
141            if !base_path.is_file() {
142                return Err(Error::custom(format!(
143                    "Inherited config file does not exist or is not a file: {}",
144                    base_path.display()
145                )));
146            }
147
148            // Prevent self-inheritance which would cause infinite recursion
149            if foundry_compilers::utils::canonicalize(&local_path).ok().as_ref() == Some(&base_path)
150            {
151                return Err(Error::custom(format!(
152                    "Config file {} cannot inherit from itself.",
153                    local_path.display()
154                )));
155            }
156
157            // Parse the base config to check for nested inheritance
158            let base_path_str = base_path.to_string_lossy();
159            let base_content = std::fs::read_to_string(&base_path)
160                .map_err(|e| Error::custom(e.to_string()).with_path(&base_path_str))?;
161            let base_partial: extend::ExtendsPartialConfig = toml::from_str(&base_content)
162                .map_err(|e| Error::custom(e.to_string()).with_path(&base_path_str))?;
163
164            // Check if the base file's same profile also has extends (nested inheritance)
165            let base_extends = base_partial
166                .profile
167                .as_ref()
168                .and_then(|profiles| {
169                    let profile_str = selected_profile.to_string();
170                    profiles.get(&profile_str)
171                })
172                .and_then(|profile| profile.extends.as_ref());
173
174            // Prevent nested inheritance to avoid complexity and potential cycles
175            if base_extends.is_some() {
176                return Err(Error::custom(format!(
177                    "Nested inheritance is not allowed. Base file '{}' cannot have an 'extends' field in profile '{selected_profile}'.",
178                    base_path.display()
179                )));
180            }
181
182            // Normalize standalone sections before merging so equivalent profile-qualified values
183            // have the same shape across inherited files.
184            let base_provider = NormalizeSymbolicProvider::new(
185                NormalizeTracingProvider::new(
186                    Toml::file(base_path).nested().legacy_labels(),
187                    selected_profile.clone(),
188                ),
189                selected_profile.clone(),
190            );
191            let local_provider = NormalizeSymbolicProvider::new(
192                NormalizeTracingProvider::new(
193                    local_provider.legacy_labels(),
194                    selected_profile.clone(),
195                ),
196                selected_profile.clone(),
197            );
198
199            // Apply the selected merge strategy
200            match extends_strategy {
201                extend::ExtendStrategy::ExtendArrays => {
202                    // Using 'admerge' strategy:
203                    // - Arrays are concatenated (base elements + local elements)
204                    // - Other values are replaced (local values override base values)
205                    // - The extends field is preserved in the final configuration
206                    Figment::new().merge(base_provider).admerge(local_provider).data()
207                }
208                extend::ExtendStrategy::ReplaceArrays => {
209                    // Using 'merge' strategy:
210                    // - Arrays are replaced entirely (local arrays replace base arrays)
211                    // - Other values are replaced (local values override base values)
212                    Figment::new().merge(base_provider).merge(local_provider).data()
213                }
214                extend::ExtendStrategy::NoCollision => {
215                    // Check for key collisions between base and local configs
216                    let base_data = base_provider.data()?;
217                    let local_data = local_provider.data()?;
218
219                    let profile_key = Profile::new("profile");
220                    if let (Some(local_profiles), Some(base_profiles)) =
221                        (local_data.get(&profile_key), base_data.get(&profile_key))
222                    {
223                        // Extract dicts for the selected profile
224                        let profile_str = selected_profile.to_string();
225                        let base_dict = base_profiles.get(&profile_str).and_then(|v| v.as_dict());
226                        let local_dict = local_profiles.get(&profile_str).and_then(|v| v.as_dict());
227
228                        // Find colliding keys
229                        if let (Some(local_dict), Some(base_dict)) = (local_dict, base_dict) {
230                            let collisions: Vec<&String> = local_dict
231                                .keys()
232                                .filter(|key| {
233                                    // Ignore the "extends" key as it's expected
234                                    *key != "extends" && base_dict.contains_key(*key)
235                                })
236                                .collect();
237
238                            if !collisions.is_empty() {
239                                return Err(Error::custom(format!(
240                                    "Key collision detected in profile '{profile_str}' when extending '{extends_path}'. \
241                                    Conflicting keys: {collisions:?}. Use 'extends.strategy' or 'extends_strategy' to specify how to handle conflicts."
242                                )));
243                            }
244                        }
245                    }
246
247                    // Safe to merge the configs without collisions
248                    Figment::new().merge(base_provider).merge(local_provider).data()
249                }
250            }
251        } else {
252            // No inheritance - return the local config as-is
253            local_provider.data()
254        }
255    }
256}
257
258struct NormalizeSymbolicProvider<P> {
259    provider: P,
260    selected_profile: Profile,
261}
262
263impl<P> NormalizeSymbolicProvider<P> {
264    const fn new(provider: P, selected_profile: Profile) -> Self {
265        Self { provider, selected_profile }
266    }
267}
268
269struct NormalizeTracingProvider<P> {
270    provider: P,
271    selected_profile: Profile,
272}
273
274impl<P> NormalizeTracingProvider<P> {
275    const fn new(provider: P, selected_profile: Profile) -> Self {
276        Self { provider, selected_profile }
277    }
278}
279
280impl<P: Provider> Provider for NormalizeTracingProvider<P> {
281    fn metadata(&self) -> Metadata {
282        self.provider.metadata()
283    }
284
285    fn data(&self) -> Result<Map<Profile, Dict>, Error> {
286        let mut data = self.provider.data()?;
287        normalize_tracing_section(&mut data, &self.selected_profile);
288        Ok(data)
289    }
290
291    fn profile(&self) -> Option<Profile> {
292        self.provider.profile()
293    }
294}
295
296/// Moves the standalone tracing section into the selected profile before inherited configs are
297/// merged. The deprecated standalone labels section remains in place for warning generation and
298/// is normalized again after inheritance is resolved.
299fn normalize_tracing_section(data: &mut Map<Profile, Dict>, selected_profile: &Profile) {
300    let Some(tracing) = data.remove(&Profile::new("tracing")) else { return };
301
302    let profiles = data.entry(Profile::new(Config::PROFILE_SECTION)).or_default();
303    let profile =
304        profiles.entry(selected_profile.to_string()).or_insert_with(|| Value::from(Dict::new()));
305    let Value::Dict(_, profile) = profile else { return };
306
307    match (profile.get_mut("tracing"), tracing) {
308        (Some(Value::Dict(_, profile_tracing)), tracing) => {
309            merge_missing(profile_tracing, tracing);
310        }
311        (None, tracing) => {
312            profile.insert("tracing".to_string(), Value::from(tracing));
313        }
314        _ => {}
315    }
316}
317
318impl<P: Provider> Provider for NormalizeSymbolicProvider<P> {
319    fn metadata(&self) -> Metadata {
320        self.provider.metadata()
321    }
322
323    fn data(&self) -> Result<Map<Profile, Dict>, Error> {
324        let mut data = self.provider.data()?;
325        normalize_symbolic_section(&mut data, &self.selected_profile);
326        Ok(data)
327    }
328
329    fn profile(&self) -> Option<Profile> {
330        self.provider.profile()
331    }
332}
333
334/// Moves the standalone symbolic section into the selected profile before inherited configs are
335/// merged.
336///
337/// This gives equivalent standalone and profile-qualified keys the same shape, so source
338/// precedence and collision detection apply consistently across inherited files.
339fn normalize_symbolic_section(data: &mut Map<Profile, Dict>, selected_profile: &Profile) {
340    let Some(symbolic) = data.remove(&Profile::new("symbolic")) else { return };
341
342    let profiles = data.entry(Profile::new(Config::PROFILE_SECTION)).or_default();
343    let profile =
344        profiles.entry(selected_profile.to_string()).or_insert_with(|| Value::from(Dict::new()));
345    let Value::Dict(_, profile) = profile else { return };
346
347    match (profile.get_mut("symbolic"), symbolic) {
348        (Some(Value::Dict(_, profile_symbolic)), symbolic) => {
349            merge_missing(profile_symbolic, symbolic);
350        }
351        (None, symbolic) => {
352            profile.insert("symbolic".to_string(), Value::from(symbolic));
353        }
354        _ => {}
355    }
356}
357
358/// Recursively fills missing values while preserving values from the higher-precedence source.
359fn merge_missing(target: &mut Dict, fallback: Dict) {
360    for (key, value) in fallback {
361        match (target.get_mut(&key), value) {
362            (Some(Value::Dict(_, target)), Value::Dict(_, fallback)) => {
363                merge_missing(target, fallback);
364            }
365            (None, value) => {
366                target.insert(key, value);
367            }
368            _ => {}
369        }
370    }
371}
372
373impl Provider for TomlFileProvider {
374    fn metadata(&self) -> Metadata {
375        if self.is_missing() {
376            Metadata::named("TOML file provider")
377        } else {
378            Toml::file(self.file()).nested().metadata()
379        }
380    }
381
382    fn data(&self) -> Result<Map<Profile, Dict>, Error> {
383        self.cache.get_or_init(|| self.read()).clone()
384    }
385}
386
387/// A Provider that ensures all keys are snake case if they're not standalone sections. See
388/// `Config::STANDALONE_SECTIONS`
389///
390/// For the `[profile]` section, profile names (like `ci-venom`) are preserved as-is,
391/// but the top-level config keys within each profile are still converted to snake_case.
392pub(crate) struct ForcedSnakeCaseData<P>(pub(crate) P);
393
394impl<P: Provider> Provider for ForcedSnakeCaseData<P> {
395    fn metadata(&self) -> Metadata {
396        self.0.metadata()
397    }
398
399    fn data(&self) -> Result<Map<Profile, Dict>, Error> {
400        let mut map = self.0.data()?;
401        for (profile, dict) in &mut map {
402            if Config::STANDALONE_SECTIONS.contains(&profile.as_ref()) {
403                // don't force snake case for keys in standalone sections
404                continue;
405            }
406
407            if profile.as_str().as_str() == Config::PROFILE_SECTION {
408                // For the `[profile]` section, we need to preserve profile names (the keys)
409                // but snake_case the top-level config keys within each profile's dict.
410                let dict2 = std::mem::take(dict);
411                *dict = dict2
412                    .into_iter()
413                    .map(|(profile_name, v)| {
414                        // Keep the profile name exactly as-is (e.g., "ci-venom" stays "ci-venom")
415                        let v = snake_case_profile_keys(v);
416                        (profile_name, v)
417                    })
418                    .collect();
419                continue;
420            }
421
422            let dict2 = std::mem::take(dict);
423            *dict = dict2.into_iter().map(|(k, v)| (k.to_snake_case(), v)).collect();
424        }
425        Ok(map)
426    }
427
428    fn profile(&self) -> Option<Profile> {
429        self.0.profile()
430    }
431}
432
433/// Converts the top-level config keys in a profile value to snake_case.
434fn snake_case_profile_keys(value: Value) -> Value {
435    match value {
436        Value::Dict(tag, dict) => {
437            let new_dict = dict.into_iter().map(|(k, v)| (k.to_snake_case(), v)).collect();
438            Value::Dict(tag, new_dict)
439        }
440        other => other,
441    }
442}
443
444/// A Provider that handles breaking changes in toml files
445pub(crate) struct BackwardsCompatTomlProvider<P>(pub(crate) P);
446
447impl<P: Provider> Provider for BackwardsCompatTomlProvider<P> {
448    fn metadata(&self) -> Metadata {
449        self.0.metadata()
450    }
451
452    fn data(&self) -> Result<Map<Profile, Dict>, Error> {
453        let mut map = Map::new();
454        let solc_env = std::env::var("FOUNDRY_SOLC_VERSION")
455            .or_else(|_| std::env::var("DAPP_SOLC_VERSION"))
456            .map(Value::from)
457            .ok();
458        for (profile, mut dict) in self.0.data()? {
459            if let Some(v) = solc_env.clone() {
460                // ENV var takes precedence over config file
461                dict.insert("solc".to_string(), v);
462            } else if let Some(v) = dict.remove("solc_version") {
463                // only insert older variant if not already included
464                if !dict.contains_key("solc") {
465                    dict.insert("solc".to_string(), v);
466                }
467            }
468            if let Some(v) = dict.remove("deny_warnings")
469                && !dict.contains_key("deny")
470            {
471                dict.insert("deny".to_string(), v);
472            }
473
474            map.insert(profile, dict);
475        }
476        normalize_legacy_labels(&mut map);
477        Ok(map)
478    }
479
480    fn profile(&self) -> Option<Profile> {
481        self.0.profile()
482    }
483}
484
485/// Adapts deprecated labels from arbitrary external providers.
486pub(crate) struct LegacyLabelsProvider<P>(pub(crate) P);
487
488impl<P: Provider> Provider for LegacyLabelsProvider<P> {
489    fn metadata(&self) -> Metadata {
490        self.0.metadata()
491    }
492
493    fn data(&self) -> Result<Map<Profile, Dict>, Error> {
494        let mut map = self.0.data()?;
495        normalize_legacy_labels(&mut map);
496        Ok(map)
497    }
498
499    fn profile(&self) -> Option<Profile> {
500        self.0.profile()
501    }
502}
503
504pub(crate) fn normalize_legacy_labels(map: &mut Map<Profile, Dict>) {
505    if let Some(labels) = map.get(&Profile::new("labels")).cloned() {
506        merge_tracing_labels(&labels, map.entry(Profile::new("tracing")).or_default());
507    }
508
509    for (profile, dict) in map {
510        if profile.as_str().as_str() == Config::PROFILE_SECTION {
511            for value in dict.values_mut() {
512                if let Value::Dict(_, profile) = value {
513                    normalize_legacy_labels_in_profile(profile);
514                }
515            }
516        } else if !Config::STANDALONE_SECTIONS.contains(&profile.as_ref()) {
517            normalize_legacy_labels_in_profile(dict);
518        }
519    }
520}
521
522pub(crate) fn normalize_legacy_labels_in_profile(dict: &mut Dict) {
523    let Some(Value::Dict(_, labels)) = dict.get("labels").cloned() else { return };
524    let tracing = dict.entry("tracing".to_string()).or_insert_with(|| Dict::new().into());
525    if let Value::Dict(_, tracing) = tracing {
526        merge_tracing_labels(&labels, tracing);
527    }
528}
529
530fn merge_tracing_labels(legacy: &Dict, tracing: &mut Dict) {
531    match tracing.get("labels") {
532        Some(Value::Dict(_, configured)) => {
533            let mut labels = legacy.clone();
534            labels.extend(configured.clone());
535            tracing.insert("labels".to_string(), labels.into());
536        }
537        Some(_) => {}
538        None => {
539            tracing.insert("labels".to_string(), legacy.clone().into());
540        }
541    }
542}
543
544/// A provider that sets the `src` and `output` path depending on their existence.
545pub(crate) struct DappHardhatDirProvider<'a> {
546    pub(crate) root: &'a Path,
547    pub(crate) detect_src: bool,
548}
549
550impl Provider for DappHardhatDirProvider<'_> {
551    fn metadata(&self) -> Metadata {
552        Metadata::named("Dapp Hardhat dir compat")
553    }
554
555    fn data(&self) -> Result<Map<Profile, Dict>, Error> {
556        let mut dict = Dict::new();
557        if self.detect_src {
558            dict.insert(
559                "src".to_string(),
560                ProjectPathsConfig::find_source_dir(self.root)
561                    .file_name()
562                    .unwrap()
563                    .to_string_lossy()
564                    .to_string()
565                    .into(),
566            );
567        }
568        dict.insert(
569            "out".to_string(),
570            ProjectPathsConfig::find_artifacts_dir(self.root)
571                .file_name()
572                .unwrap()
573                .to_string_lossy()
574                .to_string()
575                .into(),
576        );
577
578        // detect libs folders:
579        //   if `lib` _and_ `node_modules` exists: include both
580        //   if only `node_modules` exists: include `node_modules`
581        //   include `lib` otherwise
582        let mut libs = vec![];
583        let node_modules = self.root.join("node_modules");
584        let lib = self.root.join("lib");
585        if node_modules.exists() {
586            if lib.exists() {
587                libs.push(lib.file_name().unwrap().to_string_lossy().to_string());
588            }
589            libs.push(node_modules.file_name().unwrap().to_string_lossy().to_string());
590        } else {
591            libs.push(lib.file_name().unwrap().to_string_lossy().to_string());
592        }
593
594        dict.insert("libs".to_string(), libs.into());
595
596        Ok(Map::from([(Config::selected_profile(), dict)]))
597    }
598}
599
600/// A provider that checks for DAPP_ env vars that are named differently than FOUNDRY_
601pub(crate) struct DappEnvCompatProvider;
602
603impl Provider for DappEnvCompatProvider {
604    fn metadata(&self) -> Metadata {
605        Metadata::named("Dapp env compat")
606    }
607
608    fn data(&self) -> Result<Map<Profile, Dict>, Error> {
609        use serde::de::Error as _;
610        use std::env;
611
612        let mut dict = Dict::new();
613        if let Ok(val) = env::var("DAPP_TEST_NUMBER") {
614            dict.insert(
615                "block_number".to_string(),
616                val.parse::<u64>().map_err(figment::Error::custom)?.into(),
617            );
618        }
619        if let Ok(val) = env::var("DAPP_TEST_ADDRESS") {
620            dict.insert("sender".to_string(), val.into());
621        }
622        if let Ok(val) = env::var("DAPP_FORK_BLOCK") {
623            dict.insert(
624                "fork_block_number".to_string(),
625                val.parse::<u64>().map_err(figment::Error::custom)?.into(),
626            );
627        } else if let Ok(val) = env::var("DAPP_TEST_NUMBER") {
628            dict.insert(
629                "fork_block_number".to_string(),
630                val.parse::<u64>().map_err(figment::Error::custom)?.into(),
631            );
632        }
633        if let Ok(val) = env::var("DAPP_TEST_TIMESTAMP") {
634            dict.insert(
635                "block_timestamp".to_string(),
636                val.parse::<u64>().map_err(figment::Error::custom)?.into(),
637            );
638        }
639        if let Ok(val) = env::var("DAPP_BUILD_OPTIMIZE_RUNS") {
640            dict.insert(
641                "optimizer_runs".to_string(),
642                val.parse::<u64>().map_err(figment::Error::custom)?.into(),
643            );
644        }
645        if let Ok(val) = env::var("DAPP_BUILD_OPTIMIZE") {
646            // Activate Solidity optimizer (0 or 1)
647            let val = val.parse::<u8>().map_err(figment::Error::custom)?;
648            if val > 1 {
649                return Err(
650                    format!("Invalid $DAPP_BUILD_OPTIMIZE value `{val}`, expected 0 or 1").into()
651                );
652            }
653            dict.insert("optimizer".to_string(), (val == 1).into());
654        }
655
656        // libraries in env vars either as `[..]` or single string separated by comma
657        if let Ok(val) = env::var("DAPP_LIBRARIES").or_else(|_| env::var("FOUNDRY_LIBRARIES")) {
658            dict.insert("libraries".to_string(), utils::to_array_value(&val)?);
659        }
660
661        let mut fuzz_dict = Dict::new();
662        if let Ok(val) = env::var("DAPP_TEST_FUZZ_RUNS") {
663            fuzz_dict.insert(
664                "runs".to_string(),
665                val.parse::<u32>().map_err(figment::Error::custom)?.into(),
666            );
667        }
668        dict.insert("fuzz".to_string(), fuzz_dict.into());
669
670        let mut invariant_dict = Dict::new();
671        if let Ok(val) = env::var("DAPP_TEST_DEPTH") {
672            invariant_dict.insert(
673                "depth".to_string(),
674                val.parse::<u32>().map_err(figment::Error::custom)?.into(),
675            );
676        }
677        dict.insert("invariant".to_string(), invariant_dict.into());
678
679        Ok(Map::from([(Config::selected_profile(), dict)]))
680    }
681}
682
683/// Renames a profile from `from` to `to`.
684///
685/// For example given:
686///
687/// ```toml
688/// [from]
689/// key = "value"
690/// ```
691///
692/// RenameProfileProvider will output
693///
694/// ```toml
695/// [to]
696/// key = "value"
697/// ```
698pub(crate) struct RenameProfileProvider<P> {
699    provider: P,
700    from: Profile,
701    to: Profile,
702}
703
704impl<P> RenameProfileProvider<P> {
705    pub(crate) fn new(provider: P, from: impl Into<Profile>, to: impl Into<Profile>) -> Self {
706        Self { provider, from: from.into(), to: to.into() }
707    }
708}
709
710impl<P: Provider> Provider for RenameProfileProvider<P> {
711    fn metadata(&self) -> Metadata {
712        self.provider.metadata()
713    }
714
715    fn data(&self) -> Result<Map<Profile, Dict>, Error> {
716        let mut data = self.provider.data()?;
717        if let Some(data) = data.remove(&self.from) {
718            return Ok(Map::from([(self.to.clone(), data)]));
719        }
720        Ok(Default::default())
721    }
722
723    fn profile(&self) -> Option<Profile> {
724        Some(self.to.clone())
725    }
726}
727
728/// Unwraps a profile reducing the key depth
729///
730/// For example given:
731///
732/// ```toml
733/// [wrapping_key.profile]
734/// key = "value"
735/// ```
736///
737/// UnwrapProfileProvider will output:
738///
739/// ```toml
740/// [profile]
741/// key = "value"
742/// ```
743struct UnwrapProfileProvider<P> {
744    provider: P,
745    wrapping_key: Profile,
746    profile: Profile,
747}
748
749impl<P> UnwrapProfileProvider<P> {
750    pub fn new(provider: P, wrapping_key: impl Into<Profile>, profile: impl Into<Profile>) -> Self {
751        Self { provider, wrapping_key: wrapping_key.into(), profile: profile.into() }
752    }
753}
754
755impl<P: Provider> Provider for UnwrapProfileProvider<P> {
756    fn metadata(&self) -> Metadata {
757        self.provider.metadata()
758    }
759
760    fn data(&self) -> Result<Map<Profile, Dict>, Error> {
761        let mut data = self.provider.data()?;
762        if let Some(profiles) = data.remove(&self.wrapping_key) {
763            for (profile_str, profile_val) in profiles {
764                let profile = Profile::new(&profile_str);
765                if profile != self.profile {
766                    continue;
767                }
768                match profile_val {
769                    Value::Dict(_, dict) => return Ok(profile.collect(dict)),
770                    bad_val => {
771                        let mut err = Error::from(figment::error::Kind::InvalidType(
772                            bad_val.to_actual(),
773                            "dict".into(),
774                        ));
775                        err.metadata = Some(self.provider.metadata());
776                        err.profile = Some(self.profile.clone());
777                        return Err(err);
778                    }
779                }
780            }
781        }
782        Ok(Default::default())
783    }
784
785    fn profile(&self) -> Option<Profile> {
786        Some(self.profile.clone())
787    }
788}
789
790/// Wraps a profile in another profile
791///
792/// For example given:
793///
794/// ```toml
795/// [profile]
796/// key = "value"
797/// ```
798///
799/// WrapProfileProvider will output:
800///
801/// ```toml
802/// [wrapping_key.profile]
803/// key = "value"
804/// ```
805pub(crate) struct WrapProfileProvider<P> {
806    provider: P,
807    wrapping_key: Profile,
808    profile: Profile,
809}
810
811impl<P> WrapProfileProvider<P> {
812    pub fn new(provider: P, wrapping_key: impl Into<Profile>, profile: impl Into<Profile>) -> Self {
813        Self { provider, wrapping_key: wrapping_key.into(), profile: profile.into() }
814    }
815}
816
817impl<P: Provider> Provider for WrapProfileProvider<P> {
818    fn metadata(&self) -> Metadata {
819        self.provider.metadata()
820    }
821
822    fn data(&self) -> Result<Map<Profile, Dict>, Error> {
823        if let Some(inner) = self.provider.data()?.remove(&self.profile) {
824            let value = Value::from(inner);
825            let mut dict = Dict::new();
826            dict.insert(self.profile.as_str().as_str().to_snake_case(), value);
827            Ok(self.wrapping_key.collect(dict))
828        } else {
829            Ok(Default::default())
830        }
831    }
832
833    fn profile(&self) -> Option<Profile> {
834        Some(self.profile.clone())
835    }
836}
837
838/// Extracts the profile from the `profile` key and using the original key as backup, merging
839/// values where necessary
840///
841/// For example given:
842///
843/// ```toml
844/// [profile.cool]
845/// key = "value"
846///
847/// [cool]
848/// key2 = "value2"
849/// ```
850///
851/// OptionalStrictProfileProvider will output:
852///
853/// ```toml
854/// [cool]
855/// key = "value"
856/// key2 = "value2"
857/// ```
858///
859/// And emit a deprecation warning
860pub(crate) struct OptionalStrictProfileProvider<P> {
861    provider: P,
862    profiles: Vec<Profile>,
863}
864
865impl<P> OptionalStrictProfileProvider<P> {
866    pub const PROFILE_PROFILE: Profile = Profile::const_new("profile");
867
868    pub fn new(provider: P, profiles: impl IntoIterator<Item = impl Into<Profile>>) -> Self {
869        Self { provider, profiles: profiles.into_iter().map(|profile| profile.into()).collect() }
870    }
871}
872
873impl<P: Provider> Provider for OptionalStrictProfileProvider<P> {
874    fn metadata(&self) -> Metadata {
875        self.provider.metadata()
876    }
877
878    fn data(&self) -> Result<Map<Profile, Dict>, Error> {
879        let mut figment = Figment::from(&self.provider);
880        for profile in &self.profiles {
881            figment = figment.merge(UnwrapProfileProvider::new(
882                &self.provider,
883                Self::PROFILE_PROFILE,
884                profile.clone(),
885            ));
886        }
887        figment.data().map_err(|err| {
888            // figment does tag metadata and tries to map metadata to an error, since we use a new
889            // figment in this provider this new figment does not know about the metadata of the
890            // provider and can't map the metadata to the error. Therefore we return the root error
891            // if this error originated in the provider's data.
892            if let Err(root_err) = self.provider.data() {
893                return root_err;
894            }
895            err
896        })
897    }
898
899    fn profile(&self) -> Option<Profile> {
900        self.profiles.last().cloned()
901    }
902}
903
904/// Extracts the profile from the `profile` key and sets unset values according to the fallback
905/// provider
906pub struct FallbackProfileProvider<P> {
907    provider: P,
908    profile: Profile,
909    fallback: Profile,
910}
911
912impl<P> FallbackProfileProvider<P> {
913    /// Creates a new fallback profile provider.
914    pub fn new(provider: P, profile: impl Into<Profile>, fallback: impl Into<Profile>) -> Self {
915        Self { provider, profile: profile.into(), fallback: fallback.into() }
916    }
917}
918
919impl<P: Provider> Provider for FallbackProfileProvider<P> {
920    fn metadata(&self) -> Metadata {
921        self.provider.metadata()
922    }
923
924    fn data(&self) -> Result<Map<Profile, Dict>, Error> {
925        let mut data = self.provider.data()?;
926        let invariant_corpus_random_sequence_weight_configured = self.profile == "invariant"
927            && data
928                .get(&self.profile)
929                .is_some_and(|inner| inner.contains_key("corpus_random_sequence_weight"));
930        let mark_invariant_corpus_random_sequence_weight_configured =
931            |inner: &mut Dict| -> Result<(), Error> {
932                if invariant_corpus_random_sequence_weight_configured {
933                    inner.insert(
934                        "corpus_random_sequence_weight_configured".to_string(),
935                        Value::serialize(true)?,
936                    );
937                }
938                Ok(())
939            };
940
941        if let Some(fallback) = data.remove(&self.fallback) {
942            let mut inner = data.remove(&self.profile).unwrap_or_default();
943            for (k, v) in fallback {
944                inner.entry(k).or_insert(v);
945            }
946            mark_invariant_corpus_random_sequence_weight_configured(&mut inner)?;
947            Ok(self.profile.collect(inner))
948        } else {
949            if let Some(inner) = data.get_mut(&self.profile) {
950                mark_invariant_corpus_random_sequence_weight_configured(inner)?;
951            }
952            Ok(data)
953        }
954    }
955
956    fn profile(&self) -> Option<Profile> {
957        Some(self.profile.clone())
958    }
959}