pub struct Error {
pub profile: Option<Profile>,
pub metadata: Option<Metadata>,
pub path: Vec<String>,
pub kind: Kind,
/* private fields */
}
Expand description
An error that occured while producing data or extracting a configuration.
Constructing Errors
An Error
will generally be constructed indirectly via its implementations
of serde’s de::Error
and ser::Error
, that is, as a result of
serialization or deserialization errors. When implementing Provider
,
however, it may be necessary to construct an Error
directly.
Broadly, there are two ways to construct an Error
:
-
Via an error message, since
Error
implsFrom<String>
:use figment::Error; Error::from("whoops, something went wrong!".to_string());
-
Via a
Kind
, sinceError
implsFrom<Kind>
:use figment::{error::{Error, Kind}, value::Value}; let value = Value::serialize(&100).unwrap(); if !value.as_str().is_some() { let kind = Kind::InvalidType(value.to_actual(), "string".into()); let error = Error::from(kind); }
As always, ?
can be used to automatically convert into an Error
using
the available From
implementations:
use std::fs::File;
fn try_read() -> Result<(), figment::Error> {
let x = File::open("/tmp/foo.boo").map_err(|e| e.to_string())?;
Ok(())
}
Display
By default, Error
uses all of the available information about the error,
including the Metadata
, path
, and profile
to display a message that
resembles the following, where $
is error.
for some error: Error
:
$kind: `$metadata.interpolate($path)` in $($metadata.sources())*
Concretely, such an error may look like:
invalid type: found sequence, expected u16: `staging.port` in TOML file Config.toml
Iterator
An Error
may contain more than one error. To process all errors, iterate
over an Error
:
fn with_error(error: figment::Error) {
for error in error {
println!("error: {}", error);
}
}
Fields§
§profile: Option<Profile>
The profile that was selected when the error occured, if any.
metadata: Option<Metadata>
The metadata for the provider of the value that errored, if known.
path: Vec<String>
The path to the configuration key that errored, if known.
kind: Kind
The error kind.
Implementations§
source§impl Error
impl Error
sourcepub fn missing(&self) -> bool
pub fn missing(&self) -> bool
Returns true
if the error’s kind is MissingField
.
Example
use figment::error::{Error, Kind};
let error = Error::from(Kind::MissingField("path".into()));
assert!(error.missing());
sourcepub fn with_path(self, path: &str) -> Self
pub fn with_path(self, path: &str) -> Self
Append the string path
to the error’s path.
Example
use figment::Error;
let error = Error::from("an error message".to_string())
.with_path("some_path");
sourcepub fn count(&self) -> usize
pub fn count(&self) -> usize
Returns the number of errors represented by self
.
Example
use figment::{Figment, providers::{Format, Toml}};
figment::Jail::expect_with(|jail| {
jail.create_file("Base.toml", r#"
cat = [1
"#)?;
jail.create_file("Release.toml", r#"
cat = "
"#)?;
let figment = Figment::from(Toml::file("Base.toml"))
.merge(Toml::file("Release.toml"));
let error = figment.extract_inner::<String>("cat").unwrap_err();
assert_eq!(error.count(), 2);
Ok(())
});
Trait Implementations§
source§impl Error for Error
impl Error for Error
1.30.0 · source§fn source(&self) -> Option<&(dyn Error + 'static)>
fn source(&self) -> Option<&(dyn Error + 'static)>
1.0.0 · source§fn description(&self) -> &str
fn description(&self) -> &str
source§impl Error for Error
impl Error for Error
source§fn custom<T: Display>(msg: T) -> Self
fn custom<T: Display>(msg: T) -> Self
source§fn invalid_type(unexp: Unexpected<'_>, exp: &dyn Expected) -> Self
fn invalid_type(unexp: Unexpected<'_>, exp: &dyn Expected) -> Self
Deserialize
receives a type different from what it was
expecting. Read moresource§fn invalid_value(unexp: Unexpected<'_>, exp: &dyn Expected) -> Self
fn invalid_value(unexp: Unexpected<'_>, exp: &dyn Expected) -> Self
Deserialize
receives a value of the right type but that
is wrong for some other reason. Read moresource§fn invalid_length(len: usize, exp: &dyn Expected) -> Self
fn invalid_length(len: usize, exp: &dyn Expected) -> Self
source§fn unknown_variant(variant: &str, expected: &'static [&'static str]) -> Self
fn unknown_variant(variant: &str, expected: &'static [&'static str]) -> Self
Deserialize
enum type received a variant with an
unrecognized name.source§fn unknown_field(field: &str, expected: &'static [&'static str]) -> Self
fn unknown_field(field: &str, expected: &'static [&'static str]) -> Self
Deserialize
struct type received a field with an
unrecognized name.source§fn missing_field(field: &'static str) -> Self
fn missing_field(field: &'static str) -> Self
Deserialize
struct type expected to receive a required
field with a particular name but that field was not present in the
input.source§fn duplicate_field(field: &'static str) -> Self
fn duplicate_field(field: &'static str) -> Self
Deserialize
struct type received more than one of the
same field.source§impl IntoIterator for Error
impl IntoIterator for Error
source§impl PartialEq<Error> for Error
impl PartialEq<Error> for Error
impl StructuralPartialEq for Error
Auto Trait Implementations§
impl !RefUnwindSafe for Error
impl Send for Error
impl Sync for Error
impl Unpin for Error
impl !UnwindSafe for Error
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);