Struct rocket_db_pools::figment::Metadata
[−]pub struct Metadata {
pub name: Cow<'static, str>,
pub source: Option<Source>,
pub provide_location: Option<&'static Location<'static>>,
/* private fields */
}
Expand description
Metadata about a configuration value: its source’s name and location.
Overview
Every Value
produced by a Figment
is Tag
ed with Metadata
by its producing Provider
. The metadata consists of:
- A name for the source, e.g. “TOML File”.
- The
Source
itself, if it is known. - A default or custom interpolater.
- A source
Location
where a value’s provider was added to the containing figment, if it is known.
This information is used to produce insightful error messages as well as to
generate values like RelativePathBuf
that know about their configuration
source.
Errors
Error
s produced by Figment
s contain the Metadata
for the value
that caused the error. The Display
implementation for Error
uses the
metadata’s interpolater to display the path to the key for the value that
caused the error.
Interpolation
Interpolation takes a figment profile and key path (a.b.c
) and turns it
into a source-native path. The default interpolater returns a figment key
path prefixed with the profile if the profile is custom:
${profile}.${a}.${b}.${c}
Providers are free to implement any interpolater for their metadata. For
example, the interpolater for Env
uppercases each path key:
use figment::Metadata;
let metadata = Metadata::named("environment variable(s)")
.interpolater(|profile, path| {
let keys: Vec<_> = path.iter()
.map(|k| k.to_ascii_uppercase())
.collect();
format!("{}", keys.join("."))
});
let profile = figment::Profile::Default;
let interpolated = metadata.interpolate(&profile, &["key", "path"]);
assert_eq!(interpolated, "KEY.PATH");
Fields
name: Cow<'static, str>
The name of the configuration source for a given value.
source: Option<Source>
The source of the configuration value, if it is known.
provide_location: Option<&'static Location<'static>>
The source location where this value’s provider was added to the containing figment, if it is known.
Implementations
impl Metadata
impl Metadata
pub fn from<N, S>(name: N, source: S) -> Metadata where
N: Into<Cow<'static, str>>,
S: Into<Source>,
pub fn from<N, S>(name: N, source: S) -> Metadata where
N: Into<Cow<'static, str>>,
S: Into<Source>,
Creates a new Metadata
with the given name
and source
.
Example
use figment::Metadata;
let metadata = Metadata::from("AWS Config Store", "path/to/value");
assert_eq!(metadata.name, "AWS Config Store");
assert_eq!(metadata.source.unwrap().custom(), Some("path/to/value"));
pub fn named<T>(name: T) -> Metadata where
T: Into<Cow<'static, str>>,
pub fn named<T>(name: T) -> Metadata where
T: Into<Cow<'static, str>>,
Creates a new Metadata
with the given name
and no source.
Example
use figment::Metadata;
let metadata = Metadata::named("AWS Config Store");
assert_eq!(metadata.name, "AWS Config Store");
assert!(metadata.source.is_none());
pub fn source<S>(self, source: S) -> Metadata where
S: Into<Source>,
pub fn source<S>(self, source: S) -> Metadata where
S: Into<Source>,
Sets the source
of self
to Some(source)
.
Example
use figment::Metadata;
let metadata = Metadata::named("AWS Config Store").source("config/path");
assert_eq!(metadata.name, "AWS Config Store");
assert_eq!(metadata.source.unwrap().custom(), Some("config/path"));
pub fn interpolater<I>(self, f: I) -> Metadata where
I: 'static + Clone + Send + Sync + Fn(&Profile, &[&str]) -> String,
pub fn interpolater<I>(self, f: I) -> Metadata where
I: 'static + Clone + Send + Sync + Fn(&Profile, &[&str]) -> String,
Sets the interpolater
of self
to the function f
. The interpolater
can be invoked via Metadata::interpolate()
.
Example
use figment::Metadata;
let metadata = Metadata::named("environment variable(s)")
.interpolater(|profile, path| {
let keys: Vec<_> = path.iter()
.map(|k| k.to_ascii_uppercase())
.collect();
format!("{}", keys.join("."))
});
let profile = figment::Profile::Default;
let interpolated = metadata.interpolate(&profile, &["key", "path"]);
assert_eq!(interpolated, "KEY.PATH");
pub fn interpolate<K>(&self, profile: &Profile, keys: &[K]) -> String where
K: AsRef<str>,
pub fn interpolate<K>(&self, profile: &Profile, keys: &[K]) -> String where
K: AsRef<str>,
Runs the interpolater in self
on profile
and keys
.
Example
use figment::{Metadata, Profile};
let url = "ftp://config.dev";
let md = Metadata::named("Network").source(url)
.interpolater(move |profile, keys| match profile.is_custom() {
true => format!("{}/{}/{}", url, profile, keys.join("/")),
false => format!("{}/{}", url, keys.join("/")),
});
let interpolated = md.interpolate(&Profile::Default, &["key", "path"]);
assert_eq!(interpolated, "ftp://config.dev/key/path");
let profile = Profile::new("static");
let interpolated = md.interpolate(&profile, &["key", "path"]);
assert_eq!(interpolated, "ftp://config.dev/static/key/path");
Trait Implementations
Auto Trait Implementations
impl !RefUnwindSafe for Metadata
impl Send for Metadata
impl Sync for Metadata
impl Unpin for Metadata
impl !UnwindSafe for Metadata
Blanket Implementations
sourceimpl<T> BorrowMut<T> for T where
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
const: unstable · sourcefn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
sourceimpl<T> Instrument for T
impl<T> Instrument for T
sourcefn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
sourcefn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
impl<T> IntoCollection<T> for T
impl<T> IntoCollection<T> for T
fn into_collection<A>(self) -> SmallVec<A> where
A: Array<Item = T>,
fn into_collection<A>(self) -> SmallVec<A> where
A: Array<Item = T>,
Converts self
into a collection.
fn mapped<U, F, A>(self, f: F) -> SmallVec<A> where
F: FnMut(T) -> U,
A: Array<Item = U>,
sourceimpl<T> ToOwned for T where
T: Clone,
impl<T> ToOwned for T where
T: Clone,
type Owned = T
type Owned = T
The resulting type after obtaining ownership.
sourcefn clone_into(&self, target: &mut T)
fn clone_into(&self, target: &mut T)
toowned_clone_into
)Uses borrowed data to replace owned data, usually by cloning. Read more
impl<V, T> VZip<V> for T where
V: MultiLane<T>,
impl<V, T> VZip<V> for T where
V: MultiLane<T>,
fn vzip(self) -> V
sourceimpl<T> WithSubscriber for T
impl<T> WithSubscriber for T
sourcefn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self> where
S: Into<Dispatch>,
fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self> where
S: Into<Dispatch>,
Attaches the provided Subscriber
to this type, returning a
WithDispatch
wrapper. Read more
sourcefn with_current_subscriber(self) -> WithDispatch<Self>
fn with_current_subscriber(self) -> WithDispatch<Self>
Attaches the current default Subscriber
to this type, returning a
WithDispatch
wrapper. Read more