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 BincodeEncode(bincode_next::error::EncodeError),
32 BincodeDecode(bincode_next::error::DecodeError),
34 UnknownIdentifier(String),
36 IncorrectSchemeVersion,
38 ConstraintViolation,
40 FormatViolation(String),
42 Symmetric,
44 AlgorithmNotSupported(Algorithm),
46 ModeNotSupported(Mode),
48 KEM,
50 IncorrectSignature,
52 #[cfg(feature = "stream")]
54 FuturesIO(FuturesIOError),
55 #[cfg(feature = "web")]
57 JavaScript(JsValue),
58}
59
60impl core::fmt::Display for Error {
61 fn fmt(&self, f: &mut alloc::fmt::Formatter<'_>) -> alloc::fmt::Result {
62 match self {
63 Self::NotPostGuard => {
64 write!(f, "the bytestream does not start with the expected prelude")
65 }
66 Self::IncorrectVersion { expected, found } => {
67 write!(f, "wrong version, expected: {expected}, found: {found}")
68 }
69 Self::UnknownIdentifier(ident) => write!(f, "recipient unknown: {ident}"),
70 Self::FormatViolation(s) => write!(f, "{s} not (correctly) found in format"),
71 Self::BincodeEncode(e) => write!(f, "Bincode encode error: {e}"),
72 Self::BincodeDecode(e) => write!(f, "Bincode decode error: {e}"),
73 Self::Json(e) => write!(f, "JSON error: {e}"),
74 Self::IncorrectSchemeVersion => write!(f, "incorrect scheme version"),
75 Self::ConstraintViolation => write!(f, "constraint violation"),
76 Self::Symmetric => write!(f, "symmetric encryption operation error"),
77 Self::AlgorithmNotSupported(a) => write!(f, "algorithm is not supported: {a:?}"),
78 Self::ModeNotSupported(m) => write!(f, "mode is not supported: {m:?}"),
79 Self::KEM => write!(f, "KEM error"),
80 Self::IncorrectSignature => write!(f, "incorrect signature"),
81 #[cfg(feature = "stream")]
82 Self::FuturesIO(e) => write!(f, "futures IO error: {e}"),
83 #[cfg(feature = "web")]
84 Self::JavaScript(e) => write!(
85 f,
86 "JavaScript error: {}",
87 e.as_string().unwrap_or("no further details".to_string())
88 ),
89 }
90 }
91}
92
93impl From<bincode_next::error::EncodeError> for Error {
94 fn from(e: bincode_next::error::EncodeError) -> Self {
95 Self::BincodeEncode(e)
96 }
97}
98
99impl From<bincode_next::error::DecodeError> for Error {
100 fn from(e: bincode_next::error::DecodeError) -> Self {
101 Self::BincodeDecode(e)
102 }
103}
104
105impl From<TryFromIntError> for Error {
106 fn from(_: TryFromIntError) -> Self {
107 Self::ConstraintViolation
108 }
109}
110
111impl From<TryFromSliceError> for Error {
112 fn from(_: TryFromSliceError) -> Self {
113 Self::ConstraintViolation
114 }
115}