use crate::MAX_NESTING_DEPTH;
#[derive(Debug)]
#[non_exhaustive]
pub enum JcsError {
Json(serde_json::Error),
InvalidString(String),
InvalidNumber(String),
NestingDepthExceeded,
UnsupportedAlgorithm(String),
}
impl std::fmt::Display for JcsError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::Json(e) => write!(f, "JCS JSON processing failed: {e}"),
Self::InvalidString(msg) => write!(f, "JCS string validation failed: {msg}"),
Self::InvalidNumber(msg) => write!(f, "JCS number validation failed: {msg}"),
Self::NestingDepthExceeded => write!(
f,
"JCS nesting depth exceeded maximum of {MAX_NESTING_DEPTH}"
),
Self::UnsupportedAlgorithm(name) => {
write!(f, "digest algorithm not wired in this build: {name}")
}
}
}
}
impl std::error::Error for JcsError {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
match self {
Self::Json(e) => Some(e),
Self::InvalidString(_)
| Self::InvalidNumber(_)
| Self::NestingDepthExceeded
| Self::UnsupportedAlgorithm(_) => None,
}
}
}
impl From<serde_json::Error> for JcsError {
fn from(error: serde_json::Error) -> Self {
Self::Json(error)
}
}
#[derive(Debug)]
pub enum JcsErrorInfo {
Json(serde_json::Error),
Validation(String),
}
impl JcsError {
#[must_use]
pub fn into_info(self) -> JcsErrorInfo {
match self {
Self::Json(err) => JcsErrorInfo::Json(err),
Self::InvalidString(msg)
| Self::InvalidNumber(msg)
| Self::UnsupportedAlgorithm(msg) => JcsErrorInfo::Validation(msg),
other => JcsErrorInfo::Validation(other.to_string()),
}
}
}