foundry_config/
warning.rs

1use figment::Profile;
2use serde::{Deserialize, Serialize};
3use std::{fmt, path::PathBuf};
4
5/// Warnings emitted during loading or managing Configuration
6#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
7#[serde(tag = "type")]
8pub enum Warning {
9    /// An unknown section was encountered in a TOML file
10    UnknownSection {
11        /// The unknown key
12        unknown_section: Profile,
13        /// The source where the key was found
14        source: Option<String>,
15    },
16    /// No local TOML file found, with location tried
17    NoLocalToml(PathBuf),
18    /// Could not read TOML
19    CouldNotReadToml {
20        /// The path of the TOML file
21        path: PathBuf,
22        /// The error message that occurred
23        err: String,
24    },
25    /// Could not write TOML
26    CouldNotWriteToml {
27        /// The path of the TOML file
28        path: PathBuf,
29        /// The error message that occurred
30        err: String,
31    },
32    /// Invalid profile. Profile should be a table
33    CouldNotFixProfile {
34        /// The path of the TOML file
35        path: PathBuf,
36        /// The profile to be fixed
37        profile: String,
38        /// The error message that occurred
39        err: String,
40    },
41    /// Deprecated key.
42    DeprecatedKey {
43        /// The key being deprecated
44        old: String,
45        /// The new key replacing the deprecated one if not empty, otherwise, meaning the old one
46        /// is being removed completely without replacement
47        new: String,
48    },
49    /// An unknown key was encountered in a profile in a TOML file
50    UnknownKey {
51        /// The unknown key name
52        key: String,
53        /// The profile where the key was found, if applicable
54        profile: String,
55        /// The config file where the key was found
56        source: String,
57    },
58    /// An unknown key was encountered in a section in a TOML file
59    UnknownSectionKey {
60        /// The unknown key name
61        key: String,
62        /// The section where the key was found
63        section: String,
64        /// The config file where the key was found
65        source: String,
66    },
67}
68
69impl fmt::Display for Warning {
70    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
71        match self {
72            Self::UnknownSection { unknown_section, source } => {
73                let source = source.as_ref().map(|src| format!(" in {src}")).unwrap_or_default();
74                write!(
75                    f,
76                    "Found unknown config section{source}: [{unknown_section}]\n\
77                     This notation for profiles has been deprecated and may result in the profile \
78                     not being registered in future versions.\n\
79                     Please use [profile.{unknown_section}] instead or run `forge config --fix`."
80                )
81            }
82            Self::NoLocalToml(path) => write!(
83                f,
84                "No local TOML found to fix at {}.\n\
85                 Change the current directory to a project path or set the foundry.toml path with \
86                 the `FOUNDRY_CONFIG` environment variable",
87                path.display()
88            ),
89
90            Self::CouldNotReadToml { path, err } => {
91                write!(f, "Could not read TOML at {}: {err}", path.display())
92            }
93            Self::CouldNotWriteToml { path, err } => {
94                write!(f, "Could not write TOML to {}: {err}", path.display())
95            }
96            Self::CouldNotFixProfile { path, profile, err } => {
97                write!(f, "Could not fix [{profile}] in TOML at {}: {err}", path.display())
98            }
99            Self::DeprecatedKey { old, new } if new.is_empty() => {
100                write!(f, "Key `{old}` is being deprecated and will be removed in future versions.")
101            }
102            Self::DeprecatedKey { old, new } => {
103                write!(
104                    f,
105                    "Key `{old}` is being deprecated in favor of `{new}`. It will be removed in future versions."
106                )
107            }
108            Self::UnknownKey { key, profile, source } => {
109                write!(
110                    f,
111                    "Found unknown `{key}` config for profile `{profile}` defined in {source}."
112                )
113            }
114            Self::UnknownSectionKey { key, section, source } => {
115                write!(
116                    f,
117                    "Found unknown `{key}` config key in section `{section}` defined in {source}."
118                )
119            }
120        }
121    }
122}
123
124impl std::error::Error for Warning {}