Skip to main content

shadow_crypt_core/
errors.rs

1use std::{error::Error, fmt::Display};
2
3#[derive(Debug)]
4pub enum HeaderError {
5    InsufficientBytes,
6    InvalidData,
7    FilenameTooLong,
8}
9
10impl std::fmt::Display for HeaderError {
11    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
12        match self {
13            HeaderError::InsufficientBytes => write!(f, "Insufficient bytes to read header"),
14            HeaderError::InvalidData => write!(f, "Invalid header data"),
15            HeaderError::FilenameTooLong => {
16                write!(f, "Encrypted filename is too long to fit in the header")
17            }
18        }
19    }
20}
21
22impl std::error::Error for HeaderError {}
23
24#[derive(Debug)]
25pub enum KeyDerivationError {
26    InvalidParameters(String),
27    DerivationFailed(String),
28}
29
30impl std::fmt::Display for KeyDerivationError {
31    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
32        match self {
33            KeyDerivationError::InvalidParameters(msg) => write!(f, "Invalid parameters: {}", msg),
34            KeyDerivationError::DerivationFailed(msg) => write!(f, "Derivation failed: {}", msg),
35        }
36    }
37}
38
39impl std::error::Error for KeyDerivationError {}
40
41#[derive(Debug)]
42pub enum CryptError {
43    EncryptionError(String),
44    DecryptionError(String),
45}
46
47impl Display for CryptError {
48    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
49        match self {
50            CryptError::EncryptionError(msg) => write!(f, "Encryption error: {}", msg),
51            CryptError::DecryptionError(msg) => write!(f, "Decryption error: {}", msg),
52        }
53    }
54}
55
56impl Error for CryptError {}
57
58#[cfg(test)]
59mod tests {
60    use super::*;
61    use std::error::Error;
62
63    #[test]
64    fn test_header_error_display() {
65        let insufficient = HeaderError::InsufficientBytes;
66        assert_eq!(
67            format!("{}", insufficient),
68            "Insufficient bytes to read header"
69        );
70
71        let invalid = HeaderError::InvalidData;
72        assert_eq!(format!("{}", invalid), "Invalid header data");
73    }
74
75    #[test]
76    fn test_header_error_is_error_trait() {
77        let error = HeaderError::InsufficientBytes;
78        // Just ensure it implements Error trait (compilation test)
79        let _error_trait: &dyn Error = &error;
80    }
81
82    #[test]
83    fn test_key_derivation_error_display() {
84        let invalid = KeyDerivationError::InvalidParameters("test msg".to_string());
85        assert_eq!(format!("{}", invalid), "Invalid parameters: test msg");
86
87        let failed = KeyDerivationError::DerivationFailed("test msg".to_string());
88        assert_eq!(format!("{}", failed), "Derivation failed: test msg");
89    }
90
91    #[test]
92    fn test_key_derivation_error_is_error_trait() {
93        let error = KeyDerivationError::InvalidParameters("test".to_string());
94        let _error_trait: &dyn Error = &error;
95    }
96
97    #[test]
98    fn test_crypt_error_display() {
99        let encrypt = CryptError::EncryptionError("test msg".to_string());
100        assert_eq!(format!("{}", encrypt), "Encryption error: test msg");
101
102        let decrypt = CryptError::DecryptionError("test msg".to_string());
103        assert_eq!(format!("{}", decrypt), "Decryption error: test msg");
104    }
105
106    #[test]
107    fn test_crypt_error_is_error_trait() {
108        let error = CryptError::EncryptionError("test".to_string());
109        let _error_trait: &dyn Error = &error;
110    }
111}