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§
source§impl Metadata
impl Metadata
sourcepub fn from<N, S>(name: N, source: S) -> Selfwhere
N: Into<Cow<'static, str>>,
S: Into<Source>,
pub fn from<N, S>(name: N, source: S) -> Selfwhere 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"));
sourcepub fn named<T: Into<Cow<'static, str>>>(name: T) -> Self
pub fn named<T: Into<Cow<'static, str>>>(name: T) -> Self
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());
sourcepub fn source<S: Into<Source>>(self, source: S) -> Self
pub fn source<S: Into<Source>>(self, source: S) -> Self
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"));
sourcepub fn interpolater<I>(self, f: I) -> Selfwhere
I: Fn(&Profile, &[&str]) -> String + Clone + Send + Sync + 'static,
pub fn interpolater<I>(self, f: I) -> Selfwhere I: Fn(&Profile, &[&str]) -> String + Clone + Send + Sync + 'static,
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");
sourcepub fn interpolate<K: AsRef<str>>(
&self,
profile: &Profile,
keys: &[K]
) -> String
pub fn interpolate<K: AsRef<str>>( &self, profile: &Profile, keys: &[K] ) -> String
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§
source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
source§impl<T> Paint for Twhere
T: ?Sized,
impl<T> Paint for Twhere T: ?Sized,
source§fn fg(&self, value: Color) -> Painted<&T>
fn fg(&self, value: Color) -> Painted<&T>
Returns a styled value derived from self
with the foreground set to
value
.
This method should be used rarely. Instead, prefer to use color-specific
builder methods like red()
and
green()
, which have the same functionality but are
pithier.
Example
Set foreground color to white using fg()
:
use yansi::{Paint, Color};
painted.fg(Color::White);
Set foreground color to white using white()
.
use yansi::Paint;
painted.white();
source§fn bright_black(&self) -> Painted<&T>
fn bright_black(&self) -> Painted<&T>
source§fn bright_red(&self) -> Painted<&T>
fn bright_red(&self) -> Painted<&T>
source§fn bright_green(&self) -> Painted<&T>
fn bright_green(&self) -> Painted<&T>
source§fn bright_yellow(&self) -> Painted<&T>
fn bright_yellow(&self) -> Painted<&T>
Returns self
with the
fg()
set to
Color::BrightYellow
.
Example
println!("{}", value.bright_yellow());
source§fn bright_blue(&self) -> Painted<&T>
fn bright_blue(&self) -> Painted<&T>
source§fn bright_magenta(&self) -> Painted<&T>
fn bright_magenta(&self) -> Painted<&T>
Returns self
with the
fg()
set to
Color::BrightMagenta
.
Example
println!("{}", value.bright_magenta());
source§fn bright_cyan(&self) -> Painted<&T>
fn bright_cyan(&self) -> Painted<&T>
source§fn bright_white(&self) -> Painted<&T>
fn bright_white(&self) -> Painted<&T>
source§fn bg(&self, value: Color) -> Painted<&T>
fn bg(&self, value: Color) -> Painted<&T>
Returns a styled value derived from self
with the background set to
value
.
This method should be used rarely. Instead, prefer to use color-specific
builder methods like on_red()
and
on_green()
, which have the same functionality but
are pithier.
Example
Set background color to red using fg()
:
use yansi::{Paint, Color};
painted.bg(Color::Red);
Set background color to red using on_red()
.
use yansi::Paint;
painted.on_red();
source§fn on_primary(&self) -> Painted<&T>
fn on_primary(&self) -> Painted<&T>
source§fn on_magenta(&self) -> Painted<&T>
fn on_magenta(&self) -> Painted<&T>
source§fn on_bright_black(&self) -> Painted<&T>
fn on_bright_black(&self) -> Painted<&T>
Returns self
with the
bg()
set to
Color::BrightBlack
.
Example
println!("{}", value.on_bright_black());
source§fn on_bright_red(&self) -> Painted<&T>
fn on_bright_red(&self) -> Painted<&T>
source§fn on_bright_green(&self) -> Painted<&T>
fn on_bright_green(&self) -> Painted<&T>
Returns self
with the
bg()
set to
Color::BrightGreen
.
Example
println!("{}", value.on_bright_green());
source§fn on_bright_yellow(&self) -> Painted<&T>
fn on_bright_yellow(&self) -> Painted<&T>
Returns self
with the
bg()
set to
Color::BrightYellow
.
Example
println!("{}", value.on_bright_yellow());
source§fn on_bright_blue(&self) -> Painted<&T>
fn on_bright_blue(&self) -> Painted<&T>
Returns self
with the
bg()
set to
Color::BrightBlue
.
Example
println!("{}", value.on_bright_blue());
source§fn on_bright_magenta(&self) -> Painted<&T>
fn on_bright_magenta(&self) -> Painted<&T>
Returns self
with the
bg()
set to
Color::BrightMagenta
.
Example
println!("{}", value.on_bright_magenta());
source§fn on_bright_cyan(&self) -> Painted<&T>
fn on_bright_cyan(&self) -> Painted<&T>
Returns self
with the
bg()
set to
Color::BrightCyan
.
Example
println!("{}", value.on_bright_cyan());
source§fn on_bright_white(&self) -> Painted<&T>
fn on_bright_white(&self) -> Painted<&T>
Returns self
with the
bg()
set to
Color::BrightWhite
.
Example
println!("{}", value.on_bright_white());
source§fn attr(&self, value: Attribute) -> Painted<&T>
fn attr(&self, value: Attribute) -> Painted<&T>
Enables the styling Attribute
value
.
This method should be used rarely. Instead, prefer to use
attribute-specific builder methods like bold()
and
underline()
, which have the same functionality
but are pithier.
Example
Make text bold using attr()
:
use yansi::{Paint, Attribute};
painted.attr(Attribute::Bold);
Make text bold using using bold()
.
use yansi::Paint;
painted.bold();
source§fn underline(&self) -> Painted<&T>
fn underline(&self) -> Painted<&T>
Returns self
with the
attr()
set to
Attribute::Underline
.
Example
println!("{}", value.underline());
source§fn rapid_blink(&self) -> Painted<&T>
fn rapid_blink(&self) -> Painted<&T>
Returns self
with the
attr()
set to
Attribute::RapidBlink
.
Example
println!("{}", value.rapid_blink());
source§fn quirk(&self, value: Quirk) -> Painted<&T>
fn quirk(&self, value: Quirk) -> Painted<&T>
Enables the yansi
Quirk
value
.
This method should be used rarely. Instead, prefer to use quirk-specific
builder methods like mask()
and
wrap()
, which have the same functionality but are
pithier.
Example
Enable wrapping using .quirk()
:
use yansi::{Paint, Quirk};
painted.quirk(Quirk::Wrap);
Enable wrapping using wrap()
.
use yansi::Paint;
painted.wrap();
source§fn whenever(&self, value: Condition) -> Painted<&T>
fn whenever(&self, value: Condition) -> Painted<&T>
Conditionally enable styling based on whether the Condition
value
applies. Replaces any previous condition.
See the crate level docs for more details.
Example
Enable styling painted
only when both stdout
and stderr
are TTYs:
use yansi::{Paint, Condition};
painted.red().on_yellow().whenever(Condition::STDOUTERR_ARE_TTY);