pub struct Source<'a> { /* private fields */ }Expand description
One layer of configuration.
Constructors are const, so a &'static [Source<'static>] can live in a
static — which is how the macro emits it. The lifetime is there for
everything else: configuration assembled at runtime, fetched over the
network, or read from a pipe borrows just as happily.
Implementations§
Source§impl<'a> Source<'a>
impl<'a> Source<'a>
Sourcepub const fn file(path: &'a str, format: Format) -> Self
pub const fn file(path: &'a str, format: Format) -> Self
A file on disk. A file that does not exist is skipped, not an error —
listing an optional secrets.toml is the whole point of layering.
Sourcepub const fn inline(text: &'a str, format: Format) -> Self
pub const fn inline(text: &'a str, format: Format) -> Self
Configuration already in memory: a compiled-in default, a fixture, or something just read off a socket.
figment parses from a string, so anything implementing Read arrives
here through std::io::read_to_string rather than through a variant
of its own — a reader source would only hide that one line.
use dynamic_config::{load, Format, LoadSpec, Source};
use serde::Deserialize;
#[derive(Deserialize)]
struct Db { host: String }
let mut pipe = std::io::Cursor::new(r#"{"db": {"host": "localhost"}}"#);
let text = std::io::read_to_string(&mut pipe).unwrap();
let sources = [Source::inline(&text, Format::Json)];
let db: Db = load(&LoadSpec::new("db", &sources)).unwrap();
assert_eq!(db.host, "localhost");Sourcepub const fn provider(provider: &'a (dyn Provider + Send + Sync)) -> Self
Available on crate feature figment only.
pub const fn provider(provider: &'a (dyn Provider + Send + Sync)) -> Self
figment only.Configuration from a figment provider of your own.
The three built-in kinds — a file, an encrypted file, inline text — are
the ones this crate can describe. A provider is anything figment can
read: Serialized::defaults(T), an Env with a filter this crate does
not model, a provider you wrote, one from another crate.
use dynamic_config::{load, LoadSpec, Source};
use figment::providers::{Format as _, Json};
// `.nested()` because this crate reads a top-level key as a section.
let provider = Json::string(r#"{"db": {"host": "localhost"}}"#).nested();
let sources = [Source::provider(&provider)];
let db: Db = load(&LoadSpec::new("db", &sources)).unwrap();
assert_eq!(db.host, "localhost");§Two things it is on you to get right
Sections. Every other source here goes through this crate’s own
mapping of top-level keys to sections. A provider does not: what it
yields is merged as figment sees it, so it has to produce the section as
a profile — .nested() on a figment Data provider does exactly that.
Provenance. source_of and every error report the provider’s own
metadata name. A provider that describes itself badly produces a
diagnostic that describes it badly.
The Send + Sync bound is not decoration: a LoadSpec is moved to
another thread by load_async and by the file watcher, so a provider
that cannot cross one would take those with it. Every provider figment
ships already satisfies it.
Sourcepub const fn format(&self) -> Option<Format>
pub const fn format(&self) -> Option<Format>
This source’s format, for the kinds that parse text.
None for a provider, which hands over values that
are already figment’s and never sees a byte of text.
Sourcepub const fn encrypted(path: &'a str, format: Format) -> Self
pub const fn encrypted(path: &'a str, format: Format) -> Self
Configuration in a file that is encrypted on disk.
format is what the plaintext is, so secrets.json.age is
Format::Json. Reading it needs a decryptor installed with
set_decryptor.
In every other respect it is a file: same precedence, same profile variants, watched the same way, skipped if it is not there.
Sourcepub const fn path(&self) -> Option<&'a str>
pub const fn path(&self) -> Option<&'a str>
The file path, if this source is a file — encrypted or not.
Encrypted files are included because everything that asks this question — profile variants, the directories to watch — wants the same answer for both.
Sourcepub const fn is_encrypted(&self) -> bool
pub const fn is_encrypted(&self) -> bool
Whether this source has to be decrypted before it can be parsed.
Trait Implementations§
Auto Trait Implementations§
impl<'a> !RefUnwindSafe for Source<'a>
impl<'a> !UnwindSafe for Source<'a>
impl<'a> Freeze for Source<'a>
impl<'a> Send for Source<'a>
impl<'a> Sync for Source<'a>
impl<'a> Unpin for Source<'a>
impl<'a> UnsafeUnpin for Source<'a>
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
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
Source§fn in_current_span(self) -> Instrumented<Self> ⓘ
fn in_current_span(self) -> Instrumented<Self> ⓘ
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>
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>
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>
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>
Source§fn on_bright_yellow(&self) -> Painted<&T>
fn on_bright_yellow(&self) -> Painted<&T>
Source§fn on_bright_blue(&self) -> Painted<&T>
fn on_bright_blue(&self) -> Painted<&T>
Source§fn on_bright_magenta(&self) -> Painted<&T>
fn on_bright_magenta(&self) -> Painted<&T>
Source§fn on_bright_cyan(&self) -> Painted<&T>
fn on_bright_cyan(&self) -> Painted<&T>
Source§fn on_bright_white(&self) -> Painted<&T>
fn on_bright_white(&self) -> Painted<&T>
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 rapid_blink(&self) -> Painted<&T>
fn rapid_blink(&self) -> Painted<&T>
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 clear(&self) -> Painted<&T>
👎Deprecated since 1.0.1: renamed to resetting() due to conflicts with Vec::clear().
The clear() method will be removed in a future release.
fn clear(&self) -> Painted<&T>
renamed to resetting() due to conflicts with Vec::clear().
The clear() method will be removed in a future release.
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);