use thiserror::Error;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum ErrorCode {
NonCanonical,
DuplicateMapKey,
ForbiddenCborConstruct,
IdMismatch,
SignatureInvalid,
UnknownAlgorithm,
DeprecatedAlgorithm,
UnsupportedVersion,
LimitExceeded,
Malformed,
InvalidBase64Url,
UnexpectedHeaderParam,
AlgorithmConfusion,
SchemaViolation,
RelationshipUngrounded,
DanglingReference,
CycleDetected,
PolicyInvalid,
Expired,
Revoked,
RevocationUnknown,
UnauthorizedStatus,
}
impl ErrorCode {
pub fn as_str(self) -> &'static str {
match self {
Self::NonCanonical => "NON_CANONICAL",
Self::DuplicateMapKey => "DUPLICATE_MAP_KEY",
Self::ForbiddenCborConstruct => "FORBIDDEN_CBOR_CONSTRUCT",
Self::IdMismatch => "ID_MISMATCH",
Self::SignatureInvalid => "SIGNATURE_INVALID",
Self::UnknownAlgorithm => "UNKNOWN_ALGORITHM",
Self::DeprecatedAlgorithm => "DEPRECATED_ALGORITHM",
Self::UnsupportedVersion => "UNSUPPORTED_VERSION",
Self::LimitExceeded => "LIMIT_EXCEEDED",
Self::Malformed => "MALFORMED",
Self::InvalidBase64Url => "INVALID_BASE64URL",
Self::UnexpectedHeaderParam => "UNEXPECTED_HEADER_PARAM",
Self::AlgorithmConfusion => "ALGORITHM_CONFUSION",
Self::SchemaViolation => "SCHEMA_VIOLATION",
Self::RelationshipUngrounded => "RELATIONSHIP_UNGROUNDED",
Self::DanglingReference => "DANGLING_REFERENCE",
Self::CycleDetected => "CYCLE_DETECTED",
Self::PolicyInvalid => "POLICY_INVALID",
Self::Expired => "EXPIRED",
Self::Revoked => "REVOKED",
Self::RevocationUnknown => "REVOCATION_UNKNOWN",
Self::UnauthorizedStatus => "UNAUTHORIZED_STATUS",
}
}
}
impl std::fmt::Display for ErrorCode {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_str(self.as_str())
}
}
#[derive(Debug, Error)]
#[error("[{code}] {message}")]
pub struct ProofError {
pub code: ErrorCode,
pub message: String,
}
impl ProofError {
pub fn new(code: ErrorCode, message: impl Into<String>) -> Self {
Self {
code,
message: message.into(),
}
}
pub fn code_str(&self) -> &'static str {
self.code.as_str()
}
}
impl ErrorCode {
pub fn err(self, message: impl Into<String>) -> ProofError {
ProofError::new(self, message)
}
}