use core::{array::TryFromSliceError, num::TryFromIntError};
use crate::client::{Algorithm, Mode};
#[allow(unused)]
use alloc::string::{String, ToString};
#[cfg(feature = "stream")]
use futures::io::Error as FuturesIOError;
#[cfg(feature = "web")]
use wasm_bindgen::JsValue;
#[derive(Debug)]
pub enum Error {
NotPostGuard,
IncorrectVersion {
expected: u16,
found: u16,
},
Json(serde_json::Error),
Bincode(bincode::Error),
UnknownIdentifier(String),
IncorrectSchemeVersion,
ConstraintViolation,
FormatViolation(String),
Symmetric,
AlgorithmNotSupported(Algorithm),
ModeNotSupported(Mode),
KEM,
IncorrectSignature,
#[cfg(feature = "stream")]
FuturesIO(FuturesIOError),
#[cfg(feature = "web")]
JavaScript(JsValue),
}
impl core::fmt::Display for Error {
fn fmt(&self, f: &mut alloc::fmt::Formatter<'_>) -> alloc::fmt::Result {
match self {
Self::NotPostGuard => {
write!(f, "the bytestream does not start with the expected prelude")
}
Self::IncorrectVersion { expected, found } => {
write!(f, "wrong version, expected: {expected}, found: {found}")
}
Self::UnknownIdentifier(ident) => write!(f, "recipient unknown: {ident}"),
Self::FormatViolation(s) => write!(f, "{s} not (correctly) found in format"),
Self::Bincode(e) => {
write!(f, "Bincode error: {e}")
}
Self::Json(e) => write!(f, "JSON error: {e}"),
Self::IncorrectSchemeVersion => write!(f, "incorrect scheme version"),
Self::ConstraintViolation => write!(f, "constraint violation"),
Self::Symmetric => write!(f, "symmetric encryption operation error"),
Self::AlgorithmNotSupported(a) => write!(f, "algorithm is not supported: {a:?}"),
Self::ModeNotSupported(m) => write!(f, "mode is not supported: {m:?}"),
Self::KEM => write!(f, "KEM error"),
Self::IncorrectSignature => write!(f, "incorrect signature"),
#[cfg(feature = "stream")]
Self::FuturesIO(e) => write!(f, "futures IO error: {e}"),
#[cfg(feature = "web")]
Self::JavaScript(e) => write!(
f,
"JavaScript error: {}",
e.as_string().unwrap_or("no further details".to_string())
),
}
}
}
impl From<bincode::Error> for Error {
fn from(e: bincode::Error) -> Self {
Self::Bincode(e)
}
}
impl From<TryFromIntError> for Error {
fn from(_: TryFromIntError) -> Self {
Self::ConstraintViolation
}
}
impl From<TryFromSliceError> for Error {
fn from(_: TryFromSliceError) -> Self {
Self::ConstraintViolation
}
}