use std::{fmt, io, path::PathBuf};
pub type Result<T> = std::result::Result<T, Error>;
#[derive(Debug)]
pub enum Error {
Io {
path: PathBuf,
operation: &'static str,
source: io::Error,
},
Config {
message: String,
line: Option<usize>,
},
Crypto(String),
Deploy { entry: String, message: String },
Vault(String),
User(String),
}
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Io {
path,
operation,
source,
} => {
write!(f, "{operation} `{}`: {source}", path.display())
}
Self::Config {
message,
line: Some(n),
} => {
write!(f, "config error (line {n}): {message}")
}
Self::Config {
message,
line: None,
} => {
write!(f, "config error: {message}")
}
Self::Crypto(msg) => write!(f, "crypto error: {msg}"),
Self::Deploy { entry, message } => {
write!(f, "deploy `{entry}`: {message}")
}
Self::Vault(msg) => write!(f, "vault error: {msg}"),
Self::User(msg) => write!(f, "{msg}"),
}
}
}
impl std::error::Error for Error {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
match self {
Self::Io { source, .. } => Some(source),
_ => None,
}
}
}
impl Error {
pub fn io(path: impl Into<PathBuf>, operation: &'static str, source: io::Error) -> Self {
Self::Io {
path: path.into(),
operation,
source,
}
}
}