1use core::{array::TryFromSliceError, num::TryFromIntError};
4
5use crate::client::{Algorithm, Mode};
6
7#[allow(unused)]
8use alloc::string::{String, ToString};
9
10#[cfg(feature = "stream")]
11use futures::io::Error as FuturesIOError;
12
13#[cfg(feature = "web")]
14use wasm_bindgen::JsValue;
15
16#[derive(Debug)]
18pub enum Error {
19 NotPostGuard,
21 IncorrectVersion {
23 expected: u16,
25 found: u16,
27 },
28 Json(serde_json::Error),
30 Bincode(bincode::Error),
32 UnknownIdentifier(String),
34 IncorrectSchemeVersion,
36 ConstraintViolation,
38 FormatViolation(String),
40 Symmetric,
42 AlgorithmNotSupported(Algorithm),
44 ModeNotSupported(Mode),
46 KEM,
48 IncorrectSignature,
50 #[cfg(feature = "stream")]
52 FuturesIO(FuturesIOError),
53 #[cfg(feature = "web")]
55 JavaScript(JsValue),
56}
57
58impl core::fmt::Display for Error {
59 fn fmt(&self, f: &mut alloc::fmt::Formatter<'_>) -> alloc::fmt::Result {
60 match self {
61 Self::NotPostGuard => {
62 write!(f, "the bytestream does not start with the expected prelude")
63 }
64 Self::IncorrectVersion { expected, found } => {
65 write!(f, "wrong version, expected: {expected}, found: {found}")
66 }
67 Self::UnknownIdentifier(ident) => write!(f, "recipient unknown: {ident}"),
68 Self::FormatViolation(s) => write!(f, "{s} not (correctly) found in format"),
69 Self::Bincode(e) => {
70 write!(f, "Bincode error: {e}")
71 }
72 Self::Json(e) => write!(f, "JSON error: {e}"),
73 Self::IncorrectSchemeVersion => write!(f, "incorrect scheme version"),
74 Self::ConstraintViolation => write!(f, "constraint violation"),
75 Self::Symmetric => write!(f, "symmetric encryption operation error"),
76 Self::AlgorithmNotSupported(a) => write!(f, "algorithm is not supported: {a:?}"),
77 Self::ModeNotSupported(m) => write!(f, "mode is not supported: {m:?}"),
78 Self::KEM => write!(f, "KEM error"),
79 Self::IncorrectSignature => write!(f, "incorrect signature"),
80 #[cfg(feature = "stream")]
81 Self::FuturesIO(e) => write!(f, "futures IO error: {e}"),
82 #[cfg(feature = "web")]
83 Self::JavaScript(e) => write!(
84 f,
85 "JavaScript error: {}",
86 e.as_string().unwrap_or("no further details".to_string())
87 ),
88 }
89 }
90}
91
92impl From<bincode::Error> for Error {
93 fn from(e: bincode::Error) -> Self {
94 Self::Bincode(e)
95 }
96}
97
98impl From<TryFromIntError> for Error {
99 fn from(_: TryFromIntError) -> Self {
100 Self::ConstraintViolation
101 }
102}
103
104impl From<TryFromSliceError> for Error {
105 fn from(_: TryFromSliceError) -> Self {
106 Self::ConstraintViolation
107 }
108}