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 serialization/deserialization error.
31    Bincode(bincode::Error),
32    /// The recipient identifier was not found in the policies.
33    UnknownIdentifier(String),
34    /// Incorrect scheme version.
35    IncorrectSchemeVersion,
36    /// Constraint violation.
37    ConstraintViolation,
38    /// Format violation.
39    FormatViolation(String),
40    /// Opaque symmetric encryption error.
41    Symmetric,
42    /// The symmetric encryption algorithm is not supported.
43    AlgorithmNotSupported(Algorithm),
44    /// The encryption mode is not supported.
45    ModeNotSupported(Mode),
46    /// Opaque key encapsulation error.
47    KEM,
48    /// The identity-based signature did not verify.
49    IncorrectSignature,
50    /// Opaque asynchronous IO error from the futures crate.
51    #[cfg(feature = "stream")]
52    FuturesIO(FuturesIOError),
53    /// A JavaScript error.
54    #[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}