use std::fmt;
pub type Result<T> = std::result::Result<T, Error>;
#[derive(Debug, Clone)]
pub struct Error {
message: String,
}
impl Error {
pub fn new(message: impl Into<String>) -> Self {
Self {
message: message.into(),
}
}
}
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(&self.message)
}
}
impl std::error::Error for Error {}
impl From<std::io::Error> for Error {
fn from(value: std::io::Error) -> Self {
Self::new(value.to_string())
}
}
impl From<std::num::ParseIntError> for Error {
fn from(value: std::num::ParseIntError) -> Self {
Self::new(value.to_string())
}
}
impl From<std::str::Utf8Error> for Error {
fn from(value: std::str::Utf8Error) -> Self {
Self::new(value.to_string())
}
}
impl From<base64::DecodeError> for Error {
fn from(value: base64::DecodeError) -> Self {
Self::new(format!("base64 decode failed: {value}"))
}
}
impl From<rustls::Error> for Error {
fn from(value: rustls::Error) -> Self {
Self::new(value.to_string())
}
}
impl From<chrono::ParseError> for Error {
fn from(value: chrono::ParseError) -> Self {
Self::new(value.to_string())
}
}
pub fn err<T>(message: impl Into<String>) -> Result<T> {
Err(Error::new(message))
}
pub fn ensure(ok: bool, message: impl Into<String>) -> Result<()> {
if ok {
Ok(())
} else {
Err(Error::new(message))
}
}