Skip to main content

pg_core/
error.rs

1//! PostGuard errors.
2
3use 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/// An PostGuard error.
17#[derive(Debug)]
18pub enum Error {
19    /// The packet/bytestream does not start with the expected prelude.
20    NotPostGuard,
21    /// The wrong version specifier was found in the header.
22    IncorrectVersion {
23        /// The expected version specifier.
24        expected: u16,
25        /// The found version specifier,
26        found: u16,
27    },
28    /// Serde JSON error.
29    Json(serde_json::Error),
30    /// Bincode encoding error.
31    BincodeEncode(bincode_next::error::EncodeError),
32    /// Bincode decoding error.
33    BincodeDecode(bincode_next::error::DecodeError),
34    /// The recipient identifier was not found in the policies.
35    UnknownIdentifier(String),
36    /// Incorrect scheme version.
37    IncorrectSchemeVersion,
38    /// Constraint violation.
39    ConstraintViolation,
40    /// Format violation.
41    FormatViolation(String),
42    /// Opaque symmetric encryption error.
43    Symmetric,
44    /// The symmetric encryption algorithm is not supported.
45    AlgorithmNotSupported(Algorithm),
46    /// The encryption mode is not supported.
47    ModeNotSupported(Mode),
48    /// Opaque key encapsulation error.
49    KEM,
50    /// The identity-based signature did not verify.
51    IncorrectSignature,
52    /// Opaque asynchronous IO error from the futures crate.
53    #[cfg(feature = "stream")]
54    FuturesIO(FuturesIOError),
55    /// A JavaScript error.
56    #[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}