foundry_config/macros.rs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240
/// A macro to implement converters from a type to [`Config`](crate::Config) and
/// [`figment::Figment`].
///
/// This can be used to remove some boilerplate code that's necessary to add additional layer(s) to
/// the `Config`'s default `Figment`.
///
/// `impl_figment` takes the default `Config` and merges additional `Provider`, therefore the
/// targeted type, requires an implementation of `figment::Profile`.
///
/// # Example
///
/// Use `impl_figment` on a type with a `root: Option<PathBuf>` field, which will be used for
/// [`Config::figment_with_root()`](crate::Config::figment_with_root).
///
/// ```rust
/// use std::path::PathBuf;
/// use serde::Serialize;
/// use foundry_config::{Config, impl_figment_convert};
/// use foundry_config::figment::*;
/// use foundry_config::figment::error::Kind::InvalidType;
/// use foundry_config::figment::value::*;
/// #[derive(Default, Serialize)]
/// struct MyArgs {
/// #[serde(skip_serializing_if = "Option::is_none")]
/// root: Option<PathBuf>,
/// }
/// impl_figment_convert!(MyArgs);
///
/// impl Provider for MyArgs {
/// fn metadata(&self) -> Metadata {
/// Metadata::default()
/// }
///
/// fn data(&self) -> std::result::Result<Map<Profile, Dict>, Error> {
/// let value = Value::serialize(self)?;
/// let error = InvalidType(value.to_actual(), "map".into());
/// let mut dict = value.into_dict().ok_or(error)?;
/// Ok(Map::from([(Config::selected_profile(), dict)]))
/// }
/// }
///
/// let figment: Figment = From::from(&MyArgs::default());
/// let config: Config = From::from(&MyArgs::default());
///
/// // Use `impl_figment` on a type that has several nested `Provider` as fields but is _not_ a `Provider` itself
///
/// #[derive(Default)]
/// struct Outer {
/// start: MyArgs,
/// second: MyArgs,
/// third: MyArgs,
/// }
/// impl_figment_convert!(Outer, start, second, third);
///
/// let figment: Figment = From::from(&Outer::default());
/// let config: Config = From::from(&Outer::default());
/// ```
#[macro_export]
macro_rules! impl_figment_convert {
($name:ty) => {
impl<'a> From<&'a $name> for $crate::figment::Figment {
fn from(args: &'a $name) -> Self {
let root = args.root.clone()
.unwrap_or_else(|| $crate::find_project_root(None));
$crate::Config::figment_with_root(&root).merge(args)
}
}
impl<'a> From<&'a $name> for $crate::Config {
fn from(args: &'a $name) -> Self {
let figment: $crate::figment::Figment = args.into();
$crate::Config::from_provider(figment).sanitized()
}
}
};
($name:ty, $start:ident $(, $more:ident)*) => {
impl<'a> From<&'a $name> for $crate::figment::Figment {
fn from(args: &'a $name) -> Self {
let mut figment: $crate::figment::Figment = From::from(&args.$start);
$(
figment = figment.merge(&args.$more);
)*
figment
}
}
impl<'a> From<&'a $name> for $crate::Config {
fn from(args: &'a $name) -> Self {
let figment: $crate::figment::Figment = args.into();
$crate::Config::from_provider(figment).sanitized()
}
}
};
($name:ty, self, $start:ident $(, $more:ident)*) => {
impl<'a> From<&'a $name> for $crate::figment::Figment {
fn from(args: &'a $name) -> Self {
let mut figment: $crate::figment::Figment = From::from(&args.$start);
$(
figment = figment.merge(&args.$more);
)*
figment = figment.merge(args);
figment
}
}
impl<'a> From<&'a $name> for $crate::Config {
fn from(args: &'a $name) -> Self {
let figment: $crate::figment::Figment = args.into();
$crate::Config::from_provider(figment).sanitized()
}
}
};
}
/// Same as `impl_figment_convert` but also merges the type itself into the figment
///
/// # Example
///
/// Merge several nested `Provider` together with the type itself
///
/// ```rust
/// use foundry_config::{
/// figment::{value::*, *},
/// impl_figment_convert, merge_impl_figment_convert, Config,
/// };
/// use std::path::PathBuf;
///
/// #[derive(Default)]
/// struct MyArgs {
/// root: Option<PathBuf>,
/// }
///
/// impl Provider for MyArgs {
/// fn metadata(&self) -> Metadata {
/// Metadata::default()
/// }
///
/// fn data(&self) -> std::result::Result<Map<Profile, Dict>, Error> {
/// todo!()
/// }
/// }
///
/// impl_figment_convert!(MyArgs);
///
/// #[derive(Default)]
/// struct OuterArgs {
/// value: u64,
/// inner: MyArgs,
/// }
///
/// impl Provider for OuterArgs {
/// fn metadata(&self) -> Metadata {
/// Metadata::default()
/// }
///
/// fn data(&self) -> std::result::Result<Map<Profile, Dict>, Error> {
/// todo!()
/// }
/// }
///
/// merge_impl_figment_convert!(OuterArgs, inner);
/// ```
#[macro_export]
macro_rules! merge_impl_figment_convert {
($name:ty, $start:ident $(, $more:ident)*) => {
impl<'a> From<&'a $name> for $crate::figment::Figment {
fn from(args: &'a $name) -> Self {
let mut figment: $crate::figment::Figment = From::from(&args.$start);
$ (
figment = figment.merge(&args.$more);
)*
figment = figment.merge(args);
figment
}
}
impl<'a> From<&'a $name> for $crate::Config {
fn from(args: &'a $name) -> Self {
let figment: $crate::figment::Figment = args.into();
$crate::Config::from_provider(figment).sanitized()
}
}
};
}
/// A macro to implement converters from a type to [`Config`](crate::Config) and
/// [`figment::Figment`].
///
/// Via [Config::to_figment](crate::Config::to_figment) and the
/// [Cast](crate::FigmentProviders::Cast) profile.
#[macro_export]
macro_rules! impl_figment_convert_cast {
($name:ty) => {
impl<'a> From<&'a $name> for $crate::figment::Figment {
fn from(args: &'a $name) -> Self {
$crate::Config::with_root(&$crate::find_project_root(None))
.to_figment($crate::FigmentProviders::Cast)
.merge(args)
}
}
impl<'a> From<&'a $name> for $crate::Config {
fn from(args: &'a $name) -> Self {
let figment: $crate::figment::Figment = args.into();
$crate::Config::from_provider(figment).sanitized()
}
}
};
}
/// Same as `impl_figment_convert` but also implies `Provider` for the given `Serialize` type for
/// convenience. The `Provider` only provides the "root" value for the current profile
#[macro_export]
macro_rules! impl_figment_convert_basic {
($name:ty) => {
$crate::impl_figment_convert!($name);
impl $crate::figment::Provider for $name {
fn metadata(&self) -> $crate::figment::Metadata {
$crate::figment::Metadata::named(stringify!($name))
}
fn data(
&self,
) -> Result<
$crate::figment::value::Map<$crate::figment::Profile, $crate::figment::value::Dict>,
$crate::figment::Error,
> {
let mut dict = $crate::figment::value::Dict::new();
if let Some(root) = self.root.as_ref() {
dict.insert(
"root".to_string(),
$crate::figment::value::Value::serialize(root)?,
);
}
Ok($crate::figment::value::Map::from([($crate::Config::selected_profile(), dict)]))
}
}
};
}