Disable some code (and fix others) when no features are enabled

This commit is contained in:
Lukas Kalbertodt
2021-07-28 12:38:50 +02:00
parent 1a8e9440cc
commit 4f4f42e8f3
4 changed files with 58 additions and 9 deletions

View File

@@ -1,4 +1,8 @@
use std::{fmt, path::PathBuf};
use std::fmt;
#[cfg(any(feature = "toml", feature = "yaml"))]
use std::path::PathBuf;
/// Type describing all errors that can occur in this library.
@@ -13,12 +17,14 @@ pub(crate) enum ErrorInner {
MissingValue(String),
/// An IO error occured, e.g. when reading a file.
#[cfg(any(feature = "toml", feature = "yaml"))]
Io {
path: Option<PathBuf>,
err: std::io::Error,
},
/// Returned by `Source::load` implementations when deserialization fails.
#[cfg(any(feature = "toml", feature = "yaml"))]
Deserialization {
/// A human readable description for the error message, describing from
/// what source it was attempted to deserialize. Completes the sentence
@@ -39,17 +45,20 @@ pub(crate) enum ErrorInner {
/// Returned by the [`Source`] impls for `Path` and `PathBuf` if the file
/// extension is not supported by confique or if the corresponding Cargo
/// feature of confique was not enabled.
#[cfg(any(feature = "toml", feature = "yaml"))]
UnsupportedFileFormat {
path: PathBuf,
},
/// Returned by the [`Source`] impls for `Path` and `PathBuf` if the path
/// does not contain a file extension.
#[cfg(any(feature = "toml", feature = "yaml"))]
MissingFileExtension {
path: PathBuf,
},
/// A file source was marked as required but the file does not exist.
#[cfg(any(feature = "toml", feature = "yaml"))]
MissingRequiredFile {
path: PathBuf,
}
@@ -58,13 +67,18 @@ pub(crate) enum ErrorInner {
impl std::error::Error for Error {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
match &*self.inner {
#[cfg(any(feature = "toml", feature = "yaml"))]
ErrorInner::Io { err, .. } => Some(err),
#[cfg(any(feature = "toml", feature = "yaml"))]
ErrorInner::Deserialization { err, .. } => Some(&**err),
ErrorInner::MissingValue(_)
| ErrorInner::EnvDeserialization { .. }
| ErrorInner::UnsupportedFileFormat { .. }
| ErrorInner::MissingFileExtension { .. }
| ErrorInner::MissingRequiredFile { .. } => None,
ErrorInner::MissingValue(_) => None,
ErrorInner::EnvDeserialization { .. } => None,
#[cfg(any(feature = "toml", feature = "yaml"))]
ErrorInner::UnsupportedFileFormat { .. } => None,
#[cfg(any(feature = "toml", feature = "yaml"))]
ErrorInner::MissingFileExtension { .. } => None,
#[cfg(any(feature = "toml", feature = "yaml"))]
ErrorInner::MissingRequiredFile { .. } => None,
}
}
}
@@ -75,18 +89,22 @@ impl fmt::Display for Error {
ErrorInner::MissingValue(path) => {
std::write!(f, "required configuration value is missing: '{}'", path)
}
#[cfg(any(feature = "toml", feature = "yaml"))]
ErrorInner::Io { path: Some(path), .. } => {
std::write!(f,
"IO error occured while reading configuration file '{}'",
path.display(),
)
}
#[cfg(any(feature = "toml", feature = "yaml"))]
ErrorInner::Io { path: None, .. } => {
std::write!(f, "IO error occured while loading configuration")
}
#[cfg(any(feature = "toml", feature = "yaml"))]
ErrorInner::Deserialization { source: Some(source), .. } => {
std::write!(f, "failed to deserialize configuration from {}", source)
}
#[cfg(any(feature = "toml", feature = "yaml"))]
ErrorInner::Deserialization { source: None, .. } => {
std::write!(f, "failed to deserialize configuration")
}
@@ -98,18 +116,21 @@ impl fmt::Display for Error {
msg,
)
}
#[cfg(any(feature = "toml", feature = "yaml"))]
ErrorInner::UnsupportedFileFormat { path } => {
std::write!(f,
"unknown configuration file format/extension: '{}'",
path.display(),
)
}
#[cfg(any(feature = "toml", feature = "yaml"))]
ErrorInner::MissingFileExtension { path } => {
std::write!(f,
"cannot guess configuration file format due to missing file extension in '{}'",
path.display(),
)
}
#[cfg(any(feature = "toml", feature = "yaml"))]
ErrorInner::MissingRequiredFile { path } => {
std::write!(f,
"required configuration file does not exist: '{}'",