1use crate::MAX_NESTING_DEPTH;
4
5#[derive(Debug)]
7#[non_exhaustive]
8pub enum JcsError {
9 Json(serde_json::Error),
11 InvalidString(String),
13 InvalidNumber(String),
15 NestingDepthExceeded,
17 UnsupportedAlgorithm(String),
21}
22
23impl std::fmt::Display for JcsError {
24 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
25 match self {
26 Self::Json(e) => write!(f, "JCS JSON processing failed: {e}"),
27 Self::InvalidString(msg) => write!(f, "JCS string validation failed: {msg}"),
28 Self::InvalidNumber(msg) => write!(f, "JCS number validation failed: {msg}"),
29 Self::NestingDepthExceeded => write!(
30 f,
31 "JCS nesting depth exceeded maximum of {MAX_NESTING_DEPTH}"
32 ),
33 Self::UnsupportedAlgorithm(name) => {
34 write!(f, "digest algorithm not wired in this build: {name}")
35 }
36 }
37 }
38}
39
40impl std::error::Error for JcsError {
41 fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
42 match self {
43 Self::Json(e) => Some(e),
44 Self::InvalidString(_)
45 | Self::InvalidNumber(_)
46 | Self::NestingDepthExceeded
47 | Self::UnsupportedAlgorithm(_) => None,
48 }
49 }
50}
51
52impl From<serde_json::Error> for JcsError {
53 fn from(error: serde_json::Error) -> Self {
54 Self::Json(error)
55 }
56}
57
58#[derive(Debug)]
66pub enum JcsErrorInfo {
67 Json(serde_json::Error),
69 Validation(String),
71}
72
73impl JcsError {
74 #[must_use]
76 pub fn into_info(self) -> JcsErrorInfo {
77 match self {
78 Self::Json(err) => JcsErrorInfo::Json(err),
79 Self::InvalidString(msg)
80 | Self::InvalidNumber(msg)
81 | Self::UnsupportedAlgorithm(msg) => JcsErrorInfo::Validation(msg),
82 other => JcsErrorInfo::Validation(other.to_string()),
83 }
84 }
85}