type BoxError = Box<dyn std::error::Error + Send + Sync + 'static>;
#[derive(thiserror::Error, Debug)]
#[error(transparent)]
pub struct Error(ErrorKind);
impl Error {
pub fn is_loading(&self) -> bool {
matches!(self.0, ErrorKind::Loading(_))
}
pub fn is_parsing(&self) -> bool {
matches!(self.0, ErrorKind::Parsing(_))
}
pub fn is_unknown_type(&self) -> bool {
matches!(self.0, ErrorKind::UnknownType(_))
}
pub fn is_missing_field(&self) -> bool {
matches!(self.0, ErrorKind::MissingField(_))
}
pub fn is_not_supported(&self) -> bool {
matches!(self.0, ErrorKind::NotSupported(_))
}
pub(crate) fn loading<T>(source: T) -> Error
where
T: Into<BoxError>,
{
Error(ErrorKind::Loading(source.into()))
}
pub(crate) fn parsing<T>(source: T) -> Error
where
T: Into<BoxError>,
{
Error(ErrorKind::Parsing(source.into()))
}
pub(crate) fn unknown_type<T>(source: T) -> Error
where
T: Into<BoxError>,
{
Error(ErrorKind::UnknownType(source.into()))
}
pub(crate) fn missing_field(field: &'static str) -> Error {
Error(ErrorKind::MissingField(field))
}
pub(crate) fn not_supported<T>(credential_type: T) -> Error
where
T: Into<BoxError>,
{
Error(ErrorKind::NotSupported(credential_type.into()))
}
}
#[derive(thiserror::Error, Debug)]
enum ErrorKind {
#[error("could not find or open the credentials file {0}")]
Loading(#[source] BoxError),
#[error("cannot parse the credentials file {0}")]
Parsing(#[source] BoxError),
#[error("unknown or invalid credentials type {0}")]
UnknownType(#[source] BoxError),
#[error("missing required field: {0}")]
MissingField(&'static str),
#[error("credentials type not supported: {0}")]
NotSupported(#[source] BoxError),
}
#[cfg(test)]
mod tests {
use super::*;
use std::error::Error as _;
#[test]
fn constructors() {
let error = Error::loading("test message");
assert!(error.is_loading(), "{error:?}");
assert!(error.source().is_some(), "{error:?}");
assert!(error.to_string().contains("test message"), "{error}");
let error = Error::parsing("test message");
assert!(error.is_parsing(), "{error:?}");
assert!(error.source().is_some(), "{error:?}");
assert!(error.to_string().contains("test message"), "{error}");
let error = Error::unknown_type("test message");
assert!(error.is_unknown_type(), "{error:?}");
assert!(error.source().is_some(), "{error:?}");
assert!(error.to_string().contains("test message"), "{error}");
let error = Error::missing_field("test field");
assert!(error.is_missing_field(), "{error:?}");
assert!(error.source().is_none(), "{error:?}");
assert!(error.to_string().contains("test field"), "{error}");
}
}