extern crate alloc;
use alloc::borrow::Cow;
use core::fmt;
#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub struct SecurityError {
pub kind: SecurityErrorKind,
pub detail: Cow<'static, str>,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[non_exhaustive]
pub enum SecurityErrorKind {
AuthenticationFailed,
AccessDenied,
CryptoFailed,
InvalidConfiguration,
BadArgument,
NotImplemented,
Internal,
}
impl SecurityError {
#[must_use]
pub fn new(kind: SecurityErrorKind, detail: impl Into<Cow<'static, str>>) -> Self {
Self {
kind,
detail: detail.into(),
}
}
#[must_use]
pub fn not_implemented(detail: impl Into<Cow<'static, str>>) -> Self {
Self::new(SecurityErrorKind::NotImplemented, detail)
}
#[must_use]
pub fn bad_argument(detail: impl Into<Cow<'static, str>>) -> Self {
Self::new(SecurityErrorKind::BadArgument, detail)
}
}
impl fmt::Display for SecurityError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "security error [{:?}]: {}", self.kind, self.detail)
}
}
#[cfg(feature = "std")]
impl std::error::Error for SecurityError {}
pub type SecurityResult<T> = core::result::Result<T, SecurityError>;