sare_core/encryption/
error.rs

1use aead::Error as AeadError;
2use aes_kw::Error as KekError;
3use std::{fmt, io::Error as IOError};
4
5#[derive(Debug)]
6pub enum ErrSection {
7    IO(IOError),
8    Aead(AeadError),
9    Kek(KekError),
10}
11
12impl fmt::Display for ErrSection {
13    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
14        match self {
15            ErrSection::IO(err) => write!(f, "IO Error: {}", err),
16            ErrSection::Aead(err) => write!(f, "AEAD Error: {}", err),
17            ErrSection::Kek(err) => write!(f, "KEK Error: {}", err),
18        }
19    }
20}
21
22#[derive(Debug)]
23pub enum EncryptionError {
24    FailedToReadOrWrite(ErrSection),
25    InvalidKeyLength,
26    FailedToEncryptOrDecrypt(ErrSection),
27    Unexpected,
28}
29
30impl From<IOError> for EncryptionError {
31    fn from(err: IOError) -> Self {
32        EncryptionError::FailedToReadOrWrite(ErrSection::IO(err))
33    }
34}
35
36impl From<AeadError> for EncryptionError {
37    fn from(err: AeadError) -> Self {
38        EncryptionError::FailedToEncryptOrDecrypt(ErrSection::Aead(err))
39    }
40}
41
42impl From<KekError> for EncryptionError {
43    fn from(err: KekError) -> Self {
44        EncryptionError::FailedToEncryptOrDecrypt(ErrSection::Kek(err))
45    }
46}
47
48impl fmt::Display for EncryptionError {
49    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
50        match self {
51            EncryptionError::FailedToReadOrWrite(err) => {
52                write!(f, "Failed to read or write: {}", err)
53            }
54            EncryptionError::InvalidKeyLength => write!(f, "Invalid key length"),
55            EncryptionError::FailedToEncryptOrDecrypt(err) => {
56                write!(f, "Failed to encrypt or decrypt: {}", err)
57            }
58            EncryptionError::Unexpected => write!(f, "Unexpected error"),
59        }
60    }
61}