ovunto_security/primitives/
error.rs

1//! Error handling module for the ovunto_security library.
2//!
3//! This module defines the `Error` type, which is used for error handling within the library.
4//!
5//! # Examples
6//!
7//! ```
8//! use ovunto_security::primitives::Error;
9//!
10//! fn my_function() -> Result<(), Error> {
11//!     // Some operation that might fail
12//!     Err(Error::Hidden)
13//! }
14//!
15//! fn main() {
16//!     match my_function() {
17//!         Ok(_) => println!("Operation successful"),
18//!         Err(e) => println!("Error occurred: {:?}", e),
19//!     }
20//! }
21//! ```
22
23use std::fmt;
24
25pub type Result<T> = std::result::Result<T, Error>;
26
27#[derive(Debug)]
28pub enum Error {
29    Hidden,
30    Argon2Hash(argon2::password_hash::Error),
31    Argon2(argon2::Error),
32    AesGcm(aes_gcm::Error),
33    Io(std::io::Error),
34    Secp256k1(secp256k1::Error),
35    TryFromSlice(std::array::TryFromSliceError),
36    FromUtf8(std::string::FromUtf8Error),
37    Poison,
38    NotInCache,
39    Rsa(rsa::Error),
40    Pkcs8(rsa::pkcs8::spki::Error),
41    SerdeJson(serde_json::Error),
42    Base64Decode(base64::DecodeError),
43    Split,
44    WrongAlgorithm(String),
45    WrongIssuer(String),
46    Expired(u128),
47    WrongKeySize(usize),
48    Custom(String),
49    Duplicate,
50    NotFound,
51}
52
53impl Error {
54    /// Maps a result to a result with an `Error`.
55    ///
56    /// # Examples
57    ///
58    /// ```
59    /// use ovunto_security::primitives::Error;
60    ///
61    /// let result: Result<(), String> = Err("An error occurred".to_string());
62    /// assert!(Error::map(result).is_err());
63    /// ```
64    pub fn map<T, E: Into<Self>>(result: std::result::Result<T, E>) -> Result<T> {
65        result.map_err(Self::mapper)
66    }
67
68    pub fn mapper<E: Into<Self>>(err: E) -> Self {
69        err.into()
70    }
71
72    pub fn from_string<S: ToString>(err: S) -> Self {
73        let string = err.to_string();
74        string.into()
75    }
76
77    pub fn to_string<E: Into<Self>>(error: E) -> String {
78        let error = error.into();
79        format!("{error:?}")
80    }
81}
82
83impl From<argon2::password_hash::Error> for Error {
84    fn from(err: argon2::password_hash::Error) -> Self {
85        Self::Argon2Hash(err)
86    }
87}
88
89impl From<argon2::Error> for Error {
90    fn from(err: argon2::Error) -> Self {
91        Self::Argon2(err)
92    }
93}
94
95impl From<aes_gcm::Error> for Error {
96    fn from(err: aes_gcm::Error) -> Self {
97        Self::AesGcm(err)
98    }
99}
100
101impl From<std::io::Error> for Error {
102    fn from(err: std::io::Error) -> Self {
103        Self::Io(err)
104    }
105}
106
107impl From<secp256k1::Error> for Error {
108    fn from(err: secp256k1::Error) -> Self {
109        Self::Secp256k1(err)
110    }
111}
112
113impl From<std::array::TryFromSliceError> for Error {
114    fn from(err: std::array::TryFromSliceError) -> Self {
115        Self::TryFromSlice(err)
116    }
117}
118
119impl<T> From<std::sync::PoisonError<T>> for Error {
120    fn from(_: std::sync::PoisonError<T>) -> Self {
121        Self::Poison
122    }
123}
124
125impl From<rsa::Error> for Error {
126    fn from(err: rsa::Error) -> Self {
127        Self::Rsa(err)
128    }
129}
130
131impl From<serde_json::Error> for Error {
132    fn from(err: serde_json::Error) -> Self {
133        Self::SerdeJson(err)
134    }
135}
136
137impl From<base64::DecodeError> for Error {
138    fn from(err: base64::DecodeError) -> Self {
139        Self::Base64Decode(err)
140    }
141}
142
143impl From<rsa::pkcs8::spki::Error> for Error {
144    fn from(err: rsa::pkcs8::spki::Error) -> Self {
145        Self::Pkcs8(err)
146    }
147}
148
149impl From<&Error> for Error {
150    fn from(error: &Error) -> Self {
151        match error {
152            Self::Hidden => Self::Hidden,
153            Self::Poison => Self::Poison,
154            Self::Split => Self::Split,
155            Self::NotInCache => Self::NotInCache,
156            Self::Duplicate => Self::Duplicate,
157            Self::NotFound => Self::NotFound,
158            Self::Argon2Hash(err) => Self::Argon2Hash(err.clone()),
159            Self::Argon2(err) => Self::Argon2(err.clone()),
160            Self::AesGcm(err) => Self::AesGcm(err.clone()),
161            Self::Secp256k1(err) => Self::Secp256k1(err.clone()),
162            Self::TryFromSlice(err) => Self::TryFromSlice(*err),
163            Self::FromUtf8(err) => Self::FromUtf8(err.clone()),
164            Self::Pkcs8(err) => Self::Pkcs8(err.clone()),
165            Self::SerdeJson(err) => err.to_string().into(),
166            Self::Base64Decode(err) => Self::Base64Decode(err.clone()),
167            Self::WrongAlgorithm(string) => Self::WrongAlgorithm(string.clone()),
168            Self::WrongIssuer(issuer) => Self::WrongIssuer(issuer.clone()),
169            Self::Custom(string) => Self::Custom(string.clone()),
170            Self::Expired(exp) => Self::Expired(*exp),
171            Self::WrongKeySize(size) => Self::WrongKeySize(*size),
172            Self::Rsa(err) => err.to_string().into(),
173            Self::Io(err) => err.to_string().into(),
174        }
175    }
176}
177
178impl From<String> for Error {
179    fn from(string: String) -> Self {
180        Self::Custom(string)
181    }
182}
183
184impl From<std::string::FromUtf8Error> for Error {
185    fn from(err: std::string::FromUtf8Error) -> Self {
186        Self::FromUtf8(err)
187    }
188}
189
190impl fmt::Display for Error {
191    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
192        write!(f, "{:?}", self)
193    }
194}
195
196impl std::error::Error for Error {
197    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
198        match self {
199            Error::Hidden => None,
200            Error::Argon2Hash(_) => None,
201            Error::Argon2(_) => None,
202            Error::AesGcm(_) => None,
203            Error::Io(err) => Some(err),
204            Error::Secp256k1(err) => Some(err),
205            Error::TryFromSlice(err) => Some(err),
206            Error::FromUtf8(err) => Some(err),
207            Error::Poison => None,
208            Error::NotInCache => None,
209            Error::Rsa(err) => Some(err),
210            Error::Pkcs8(err) => Some(err),
211            Error::SerdeJson(err) => Some(err),
212            Error::Base64Decode(err) => Some(err),
213            Error::Split => None,
214            Error::WrongAlgorithm(_) => None,
215            Error::WrongIssuer(_) => None,
216            Error::Expired(_) => None,
217            Error::WrongKeySize(_) => None,
218            Error::Custom(_) => None,
219            Error::Duplicate => None,
220            Error::NotFound => None,
221        }
222    }
223}