abe_gpsw/
error.rs

1use std::{
2    array::TryFromSliceError,
3    convert::{From, Infallible},
4    ffi::NulError,
5    num::{ParseIntError, TryFromIntError},
6    str::Utf8Error,
7};
8
9#[cfg(feature = "interfaces")]
10use cosmian_crypto_base::{CryptoBaseError, CryptoCoreError};
11use hex::FromHexError;
12use thiserror::Error;
13
14#[derive(Error, Debug)]
15pub enum FormatErr {
16    //
17    // External errors conversion
18    #[error("{0}")]
19    CryptoError(String),
20    #[error(transparent)]
21    PolicyError(#[from] abe_policy::Error),
22    #[error(transparent)]
23    Infallible(Infallible),
24    #[error(transparent)]
25    NulError(NulError),
26    #[error(transparent)]
27    TryFromIntError(#[from] TryFromIntError),
28    #[error(transparent)]
29    ParsingError(#[from] ParsingError),
30    #[error(transparent)]
31    FromHexError(#[from] FromHexError),
32    #[error(transparent)]
33    Utf8Error(#[from] Utf8Error),
34
35    //
36    // Internal errors
37    #[error("attribute not found: {0}")]
38    AttributeNotFound(String),
39    #[error("{} is missing{}",
40        .item.clone().unwrap_or_else(|| "attribute".to_string()),
41        match .axis_name {
42            Some(axis) => format!(" in axis {}", axis),
43            None => "".to_string(),
44    })]
45    MissingAttribute {
46        item: Option<String>,
47        axis_name: Option<String>,
48    },
49    #[error("{0} is missing")]
50    MissingAxis(String),
51    #[error("attribute {0} expected in {1:?}")]
52    ExpectedAttribute(String, Vec<String>),
53    #[error("symmetric key generation {0}")]
54    SymmetricKeyGeneration(String),
55    #[error("symmetric encryption {0}")]
56    SymmetricEncryption(String),
57    #[error("symmetric decryption {0}")]
58    SymmetricDecryption(String),
59    #[error("asymmetric decryption {0}")]
60    AsymmetricDecryption(String),
61    #[error("invalid size")]
62    InvalidSize(String),
63    #[error("{0}")]
64    Deserialization(String),
65    #[error("{0}")]
66    Serialization(String),
67    #[error("{0}")]
68    InternalOperation(String),
69    #[error("invalid formula: {0}")]
70    InvalidFormula(String),
71    #[error("invalid encrypted data")]
72    InvalidEncryptedData,
73    #[error("conversion failed")]
74    ConversionFailed,
75    #[error("Empty private key")]
76    EmptyPrivateKey,
77    #[error("Empty ciphertext")]
78    EmptyCiphertext,
79    #[error("Empty plaintext")]
80    EmptyPlaintext,
81    #[error("Header length must be at least 4 bytes. Found {0}")]
82    InvalidHeaderSize(usize),
83}
84
85impl From<TryFromSliceError> for FormatErr {
86    fn from(_e: TryFromSliceError) -> Self {
87        Self::ConversionFailed
88    }
89}
90
91impl From<serde_json::Error> for FormatErr {
92    fn from(e: serde_json::Error) -> Self {
93        Self::Deserialization(e.to_string())
94    }
95}
96
97#[cfg(feature = "interfaces")]
98impl From<CryptoBaseError> for FormatErr {
99    fn from(e: CryptoBaseError) -> Self {
100        match e {
101            CryptoBaseError::SizeError { given, expected } => {
102                Self::InvalidSize(format!("expected: {}, given: {}", expected, given))
103            }
104            CryptoBaseError::InvalidSize(e) => Self::InvalidSize(e),
105            e => Self::CryptoError(e.to_string()),
106        }
107    }
108}
109
110#[cfg(feature = "interfaces")]
111impl From<CryptoCoreError> for FormatErr {
112    fn from(e: CryptoCoreError) -> Self {
113        Self::CryptoError(e.to_string())
114    }
115}
116
117impl From<Infallible> for FormatErr {
118    fn from(e: Infallible) -> Self {
119        Self::Infallible(e)
120    }
121}
122
123#[cfg(feature = "ffi")]
124impl From<std::ffi::NulError> for FormatErr {
125    fn from(e: std::ffi::NulError) -> Self {
126        Self::NulError(e)
127    }
128}
129
130#[derive(Error, Debug, Clone, PartialEq)]
131pub enum ParsingError {
132    #[error("{0}")]
133    UnexpectedCharacter(String),
134    #[error("{0}")]
135    UnexpectedEnd(String),
136    #[error("empty string")]
137    EmptyString,
138    #[error("wrong range")]
139    RangeError,
140    #[error(transparent)]
141    RegexError(#[from] regex::Error),
142    #[error(transparent)]
143    ParseIntError(#[from] ParseIntError),
144    #[error("unsupported operand {0}")]
145    UnsupportedOperand(String),
146    #[error("unsupported operator {0}")]
147    UnsupportedOperator(String),
148}