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