#[cfg(not(feature = "std"))]
use alloc::boxed::Box;
#[cfg(feature = "std")]
use std::boxed::Box;
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[non_exhaustive]
pub enum VerificationErrorKind {
UnsupportedAlgorithm,
UnsupportedKey,
MalformedKey,
InvalidSignature,
}
impl core::fmt::Display for VerificationErrorKind {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
f.write_str(match self {
Self::UnsupportedAlgorithm => "unsupported signature algorithm",
Self::UnsupportedKey => "issuer key not usable with this signature algorithm",
Self::MalformedKey => "malformed issuer public key",
Self::InvalidSignature => "invalid signature",
})
}
}
#[derive(Debug)]
pub struct VerificationError {
kind: VerificationErrorKind,
source: Option<Box<dyn core::error::Error + Send + Sync + 'static>>,
}
impl VerificationError {
#[must_use]
pub const fn new(kind: VerificationErrorKind) -> Self {
Self { kind, source: None }
}
#[must_use]
pub fn with_source(
kind: VerificationErrorKind,
source: impl core::error::Error + Send + Sync + 'static,
) -> Self {
Self {
kind,
source: Some(Box::new(source)),
}
}
#[must_use]
pub const fn unsupported_algorithm() -> Self {
Self::new(VerificationErrorKind::UnsupportedAlgorithm)
}
#[must_use]
pub const fn unsupported_key() -> Self {
Self::new(VerificationErrorKind::UnsupportedKey)
}
#[must_use]
pub const fn malformed_key() -> Self {
Self::new(VerificationErrorKind::MalformedKey)
}
#[must_use]
pub const fn invalid_signature() -> Self {
Self::new(VerificationErrorKind::InvalidSignature)
}
#[must_use]
pub const fn kind(&self) -> VerificationErrorKind {
self.kind
}
}
impl From<VerificationErrorKind> for VerificationError {
fn from(kind: VerificationErrorKind) -> Self {
Self::new(kind)
}
}
impl core::fmt::Display for VerificationError {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
core::fmt::Display::fmt(&self.kind, f)
}
}
impl core::error::Error for VerificationError {
fn source(&self) -> Option<&(dyn core::error::Error + 'static)> {
self.source
.as_deref()
.map(|e| e as &(dyn core::error::Error + 'static))
}
}