Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ name = "confique"
version = "0.4.0"
authors = ["Lukas Kalbertodt <lukas.kalbertodt@gmail.com>"]
edition = "2021"
rust-version = "1.68.2"
rust-version = "1.70"

description = "Type-safe, layered, light-weight, `serde`-based configuration library"
documentation = "https://docs.rs/confique/"
Expand Down Expand Up @@ -41,12 +41,15 @@ required-features = ["toml"]
[features]
default = []
yaml = ["dep:serde_yaml"]
json5 = ["dep:json5"]
serialize = []


[dependencies]
confique-macro = { version = "=0.0.13", path = "macro" }
json5 = { version = "0.4.1", optional = true }
serde = { version = "1.0.145", features = ["derive"] }
serde_json = { version = "1.0", optional = true }
serde_yaml = { version = "0.9.0", optional = true }
toml = { version = "0.9.0", optional = true }

Expand Down
83 changes: 67 additions & 16 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ use std::fmt;

use std::path::PathBuf;



/// Type describing all errors that can occur in this library.
///
/// *Note*: the `Display` and `Debug` impls of this type do not include
Expand All @@ -19,7 +17,21 @@ pub struct Error {

impl Error {
pub(crate) fn field_validation(msg: impl fmt::Display) -> Self {
ErrorInner::FieldValidation { msg: msg.to_string() }.into()
ErrorInner::FieldValidation {
msg: msg.to_string(),
}
.into()
}

#[cfg(all(
feature = "serialize",
any(feature = "toml", feature = "yaml", feature = "json5")
))]
pub(crate) fn serialization(msg: impl fmt::Display) -> Self {
ErrorInner::Serialization {
msg: msg.to_string(),
}
.into()
}
}

Expand Down Expand Up @@ -88,6 +100,13 @@ pub(crate) enum ErrorInner {

/// When a struct validation function fails.
StructValidation { name: String, msg: String },

/// When serialization of a config fails.
#[cfg(all(
feature = "serialize",
any(feature = "toml", feature = "yaml", feature = "json5")
))]
Serialization { msg: String },
}

impl std::error::Error for Error {
Expand All @@ -104,6 +123,11 @@ impl std::error::Error for Error {
ErrorInner::MissingRequiredFile { .. } => None,
ErrorInner::FieldValidation { .. } => None,
ErrorInner::StructValidation { .. } => None,
#[cfg(all(
feature = "serialize",
any(feature = "toml", feature = "yaml", feature = "json5")
))]
ErrorInner::Serialization { .. } => None,
}
}
}
Expand All @@ -114,16 +138,22 @@ impl fmt::Display for Error {
ErrorInner::MissingValue(path) => {
std::write!(f, "required configuration value is missing: '{path}'")
}
ErrorInner::Io { path: Some(path), .. } => {
std::write!(f,
ErrorInner::Io {
path: Some(path), ..
} => {
std::write!(
f,
"IO error occured while reading configuration file '{}'",
path.display(),
)
}
ErrorInner::Io { path: None, .. } => {
std::write!(f, "IO error occured while loading configuration")
}
ErrorInner::Deserialization { source: Some(source), err } => {
ErrorInner::Deserialization {
source: Some(source),
err,
} => {
std::write!(f, "failed to deserialize configuration from {source}")?;
if f.alternate() {
f.write_str(": ")?;
Expand All @@ -140,36 +170,48 @@ impl fmt::Display for Error {
Ok(())
}
ErrorInner::EnvNotUnicode { field, key } => {
std::write!(f, "failed to load value `{field}` from \
environment variable `{key}`: value is not valid unicode")
std::write!(
f,
"failed to load value `{field}` from \
environment variable `{key}`: value is not valid unicode"
)
}
ErrorInner::EnvDeserialization { field, key, msg } => {
std::write!(f, "failed to deserialize value `{field}` from \
environment variable `{key}`: {msg}")
std::write!(
f,
"failed to deserialize value `{field}` from \
environment variable `{key}`: {msg}"
)
}
ErrorInner::EnvParseError { field, key, err } => {
std::write!(f, "failed to parse environment variable `{key}` into \
field `{field}`")?;
std::write!(
f,
"failed to parse environment variable `{key}` into \
field `{field}`"
)?;
if f.alternate() {
f.write_str(": ")?;
fmt::Display::fmt(&err, f)?;
}
Ok(())
}
ErrorInner::UnsupportedFileFormat { path } => {
std::write!(f,
std::write!(
f,
"unknown configuration file format/extension: '{}'",
path.display(),
)
}
ErrorInner::MissingFileExtension { path } => {
std::write!(f,
std::write!(
f,
"cannot guess configuration file format due to missing file extension in '{}'",
path.display(),
)
}
ErrorInner::MissingRequiredFile { path } => {
std::write!(f,
std::write!(
f,
"required configuration file does not exist: '{}'",
path.display(),
)
Expand All @@ -180,6 +222,13 @@ impl fmt::Display for Error {
ErrorInner::StructValidation { name, msg } => {
std::write!(f, "config validation of `{name}` failed: {msg}")
}
#[cfg(all(
feature = "serialize",
any(feature = "toml", feature = "yaml", feature = "json5")
))]
ErrorInner::Serialization { msg } => {
std::write!(f, "failed to serialize configuration: {msg}")
}
}
}
}
Expand All @@ -192,6 +241,8 @@ impl fmt::Debug for Error {

impl From<ErrorInner> for Error {
fn from(inner: ErrorInner) -> Self {
Self { inner: Box::new(inner) }
Self {
inner: Box::new(inner),
}
}
}
Loading