1use std::fmt::Display;
2
3pub use zip::result::ZipError;
4pub use rsa::errors::Error as RSAError;
5pub use std::io::Error as IOError;
6pub use rand::Error as RngError;
7pub use chacha20poly1305::aead::Error as ChaChaError;
8
9pub type DirectoryContentPathResult<T> = Result<T, DirectoryContentPathError>;
10
11#[derive(Debug, Clone, PartialEq, Eq)]
12pub enum DirectoryContentPathError {
13    ElementCannotBeEmpty,
14}
15
16impl Display for DirectoryContentPathError {
17    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
18        write!(f, "{}", match self {
19            Self::ElementCannotBeEmpty => "ElementCannotBeEmpty",
20        }.to_string())
21    }
22}
23
24impl std::error::Error for DirectoryContentPathError { }
25
26pub type SymmetricCipherResult<T> = Result<T, SymmetricCipherError>;
27
28#[derive(Debug)]
29pub enum SymmetricCipherError {
30    ConvertionError,
31    IOError(IOError),
32    XChaCha20Poly1305Error(ChaChaError),
33}
34
35impl Display for SymmetricCipherError {
36    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
37        write!(f, "{}", match self {
38            Self::ConvertionError => "ConvertionError".to_string(),
39            Self::IOError(err) => err.to_string(),
40            Self::XChaCha20Poly1305Error(err) => err.to_string(),
41        })
42    }
43}
44
45impl std::error::Error for SymmetricCipherError { }
46
47impl From<IOError> for SymmetricCipherError {
48    fn from(value: IOError) -> Self {
49        Self::IOError(value)
50    }
51}
52
53impl From<ChaChaError> for SymmetricCipherError {
54    fn from(value: ChaChaError) -> Self {
55        Self::XChaCha20Poly1305Error(value)
56    }
57}
58
59pub type AsymetricKeyResult<T> = Result<T, AsymetricKeyError>;
60
61#[derive(Debug)]
62pub enum AsymetricKeyError {
63    NotAValidSymmetricKey,
64    KeySizeIsTooSmall,
65    XChaCha20Poly1305Error(ChaChaError),
66    RandError(RngError),
67    RSAError(RSAError),
68}
69
70impl Display for AsymetricKeyError {
71    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
72        write!(f, "{}", match self {
73            AsymetricKeyError::NotAValidSymmetricKey => "NotAValidSymmetricKey".to_owned(),
74            AsymetricKeyError::KeySizeIsTooSmall => format!("KeySizeIsTooSmall (at least {})", crate::asymetric_key::MIN_RSA_KEY_SIZE),
75            AsymetricKeyError::XChaCha20Poly1305Error(err) => err.to_string(),
76            AsymetricKeyError::RandError(err) => err.to_string(),
77            AsymetricKeyError::RSAError(err) => err.to_string(),
78        })
79    }
80}
81
82impl std::error::Error for AsymetricKeyError { }
83
84impl From<RngError> for AsymetricKeyError {
85    fn from(value: RngError) -> Self {
86        Self::RandError(value)
87    }
88}
89
90impl From<RSAError> for AsymetricKeyError {
91    fn from(value: RSAError) -> Self {
92        Self::RSAError(value)
93    }
94}
95
96impl From<ChaChaError> for AsymetricKeyError {
97    fn from(value: chacha20poly1305::aead::Error) -> Self {
98        Self::XChaCha20Poly1305Error(value)
99    }
100}
101
102pub type DirectoryContentResult<T> = Result<T, ContentError>;
103
104#[derive(Debug, Clone, PartialEq, Eq)]
105pub enum ContentError {
106    FileAlreadyExists,
107    DirectoryAlreadyExists,
108    FileDoesNotExist,
109    DirectoryDoesNotExist,
110    NameCanNotBeEmpty,
111}
112
113impl Display for ContentError {
114    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
115        write!(f, "{}", match self {
116            ContentError::FileAlreadyExists => "FileAlreadyExists",
117            ContentError::DirectoryAlreadyExists => "DirectoryAlreadyExists",
118            ContentError::FileDoesNotExist => "FileDoesNotExist",
119            ContentError::DirectoryDoesNotExist => "DirectoryDoesNotExist(Content)",
120            ContentError::NameCanNotBeEmpty => "NameCanNotBeEmpty",
121        })
122    }
123}
124
125#[cfg(feature = "signers-list")]
126mod signers_list_error {
127    use std::fmt::Display;
128    pub type SignersListResult<T> = Result<T, SignersListError>;
129    
130    #[derive(Debug)]
131    pub enum SignersListError {
132        DirectoryDoesNotExist,
133        ItIsNotAnDirectory,
134        SignerDoesNotExist,
135        SignerIsNotValid,
136        MoreThanOneSignerHasSameKeyFile,
137        SignerAlreadyExist,
138        IOError(std::io::Error),
139        JSONSerdeError(serde_json::error::Error),
140    }
141    
142    impl From<std::io::Error> for SignersListError {
143        fn from(value: std::io::Error) -> Self {
144            Self::IOError(value)
145        }
146    }
147    
148    impl From<serde_json::error::Error> for SignersListError {
149        fn from(value: serde_json::error::Error) -> Self {
150            Self::JSONSerdeError(value)
151        }
152    }
153    
154    impl Display for SignersListError {
155        fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
156            write!(f, "{}", match self {
157                SignersListError::DirectoryDoesNotExist => "DirectoryDoesNotExist(SignersList)".to_owned(),
158                SignersListError::ItIsNotAnDirectory => "ItIsNotAnDirectory".to_owned(),
159                SignersListError::SignerDoesNotExist => "SignerDoesNotExist".to_owned(),
160                SignersListError::SignerIsNotValid => "SignerIsNotValid".to_owned(),
161                SignersListError::MoreThanOneSignerHasSameKeyFile => "MoreThanOneSignerHasSameKeyFile".to_owned(),
162                SignersListError::SignerAlreadyExist => "SignerAlreadyExist".to_owned(),
163                SignersListError::IOError(err) => err.to_string(),
164                SignersListError::JSONSerdeError(err) => err.to_string(),
165            })
166        }
167    }
168    
169    impl std::error::Error for SignersListError { }
170}
171
172#[cfg(feature = "signers-list")]
173pub use signers_list_error::*;
174
175pub type EncryptedFileResult<T> = Result<T, EncryptedFileError>;
176
177#[derive(Debug)]
178pub enum EncryptedFileError {
179    FileAlreadyExists,
180    FileDoesNotExist,
182    FileIsNotSigned,
183    FileKeyIsMissing,
184    FileContentIsMissing,
185    InvalidPath,
186    DirectoryDoesNotExist,
187    ThisIsNotADirectory,
188    ContentIsUnknown,
189    ZipError(ZipError),
190    DirectoryContentError(ContentError),
191    RSAError(RSAError),
192    IOError(IOError),
193    SymmetricCipherError(SymmetricCipherError),
194    AsymetricKeyError(AsymetricKeyError),
195}
196
197impl From<ZipError> for EncryptedFileError {
198    fn from(value: ZipError) -> Self {
199        Self::ZipError(value)
200    }
201}
202
203impl From<ContentError> for EncryptedFileError {
204    fn from(value: ContentError) -> Self {
205        Self::DirectoryContentError(value)
206    }
207}
208
209impl From<RSAError> for EncryptedFileError {
210    fn from(value: RSAError) -> Self {
211        Self::RSAError(value)
212    }
213}
214
215impl From<IOError> for EncryptedFileError {
216    fn from(value: IOError) -> Self {
217        Self::IOError(value)
218    }
219}
220
221impl From<SymmetricCipherError> for EncryptedFileError {
222    fn from(value: SymmetricCipherError) -> Self {
223        Self::SymmetricCipherError(value)
224    }
225}
226
227impl From<AsymetricKeyError> for EncryptedFileError {
228    fn from(value: AsymetricKeyError) -> Self {
229        Self::AsymetricKeyError(value)
230    }
231}
232
233impl Display for EncryptedFileError {
234    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
235        write!(f, "{}", match self {
236            EncryptedFileError::FileAlreadyExists => "FileAlreadyExists".to_owned(),
237            EncryptedFileError::FileDoesNotExist => "FileDoesNotExist".to_owned(),
239            EncryptedFileError::FileIsNotSigned => "FileIsNotSigned".to_owned(),
240            EncryptedFileError::FileKeyIsMissing => "FileKeyIsMissing".to_owned(),
241            EncryptedFileError::FileContentIsMissing => "FileContentIsMissing".to_owned(),
242            EncryptedFileError::InvalidPath => "InvalidPath".to_owned(),
243            EncryptedFileError::DirectoryDoesNotExist => "DirectoryDoesNotExist(EncryptedFile)".to_owned(),
244            EncryptedFileError::ThisIsNotADirectory => "ThisIsNotADirectory".to_owned(),
245            EncryptedFileError::ContentIsUnknown => "ContentIsUnknown".to_owned(),
246            EncryptedFileError::ZipError(err) => err.to_string(),
247            EncryptedFileError::DirectoryContentError(err) => err.to_string(),
248            EncryptedFileError::RSAError(err) => err.to_string(),
249            EncryptedFileError::IOError(err) => err.to_string(),
250            EncryptedFileError::SymmetricCipherError(err) => err.to_string(),
251            EncryptedFileError::AsymetricKeyError(err) => err.to_string(),
252        })
253    }
254}
255
256impl std::error::Error for EncryptedFileError { }