use thiserror::Error;
#[derive(Debug, Error)]
pub enum Error {
#[error("i/o error: {0}")]
Io(#[from] std::io::Error),
#[error("botan: {0}")]
Botan(String),
#[error("hex: {0}")]
Hex(String),
#[error("base64: {0}")]
Base64(String),
#[error("cipher: {0}")]
Cipher(String),
#[error("pbkdf: {0}")]
Pbkdf(String),
#[error("policy violation: {0}")]
Policy(String),
#[error("capability policy '{rule}' violated: {context}")]
PolicyViolation { rule: String, context: String },
#[error("parse error in {file}:{lineno}: {msg}")]
Parse {
file: String,
lineno: i32,
msg: String,
},
#[error("CAS: {0}")]
Cas(String),
#[error("PHC: {0}")]
Phc(String),
#[error("JSON: {0}")]
Json(String),
#[error("{0}")]
Msg(String),
}
impl Error {
pub fn botan(e: impl std::fmt::Display) -> Self {
Error::Botan(e.to_string())
}
pub fn msg(s: impl Into<String>) -> Self {
Error::Msg(s.into())
}
pub fn json(e: impl std::fmt::Display) -> Self {
Error::Json(e.to_string())
}
pub fn with_context(self, ctx: impl std::fmt::Display) -> Self {
Error::Msg(format!("{}: {}", ctx, self))
}
}
impl From<botan::Error> for Error {
fn from(e: botan::Error) -> Self {
Error::Botan(e.to_string())
}
}
impl From<hex::FromHexError> for Error {
fn from(e: hex::FromHexError) -> Self {
Error::Hex(e.to_string())
}
}
pub type Result<T> = std::result::Result<T, Error>;