pub type Result<T> = std::result::Result<T, Error>;
#[derive(Debug)]
pub enum Error {
InvalidTokenLength,
InvalidTokenSignature,
SerdeJsonError(serde_json::Error),
SignError(hmac::digest::InvalidLength),
B64DecodeError(base64::DecodeError),
FromUtf8Error(std::string::FromUtf8Error),
}
impl std::error::Error for Error {}
impl std::fmt::Display for Error {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let message = match self {
Error::InvalidTokenLength => "Invalid token, not enough parts",
Error::InvalidTokenSignature => "Calculated token signature difference from provided signature",
Error::SerdeJsonError(_) => "Serde_json error",
Error::SignError(_) => "HMAC error",
Error::B64DecodeError(_) => "Base64 error",
Error::FromUtf8Error(_) => "UTF-8 convertion error",
};
write!(f, "{message}")
}
}
impl From<serde_json::Error> for Error {
fn from(error: serde_json::Error) -> Self {
Self::SerdeJsonError(error)
}
}
impl From<hmac::digest::InvalidLength> for Error {
fn from(error: hmac::digest::InvalidLength) -> Self {
Self::SignError(error)
}
}
impl From<base64::DecodeError> for Error {
fn from(error: base64::DecodeError) -> Self {
Self::B64DecodeError(error)
}
}
impl From<std::string::FromUtf8Error> for Error {
fn from(error: std::string::FromUtf8Error) -> Self {
Self::FromUtf8Error(error)
}
}