fencryption_lib/
commands.rs

1//! Commands.
2
3pub mod decrypt_file;
4pub mod decrypt_text;
5pub mod encrypt_file;
6pub mod encrypt_text;
7
8mod logic;
9
10/// Command enum.
11pub enum Command {
12    EncryptFile,
13    DecryptFile,
14}
15
16/// Command execution Result.
17pub type Result<T, E = Error> = std::result::Result<T, E>;
18
19#[derive(Debug)]
20/// Command execution Error.
21pub struct Error {
22    message: String,
23    debug: String,
24}
25
26impl Error {
27    pub fn to_string(&self, debug_mode: bool) -> String {
28        format!(
29            "Error: {}{}",
30            self.message,
31            debug_mode
32                .then_some(format!("\n{}", self.debug))
33                .unwrap_or("".to_string())
34        )
35    }
36}
37
38/// Command execution Error builder.
39pub struct ErrorBuilder {
40    message: Option<String>,
41    debug: Option<String>,
42}
43
44impl ErrorBuilder {
45    pub fn new() -> Self {
46        ErrorBuilder {
47            message: None,
48            debug: None,
49        }
50    }
51
52    pub fn message<S>(mut self, message: S) -> Self
53    where
54        S: AsRef<str>,
55    {
56        self.message = Some(message.as_ref().to_string());
57        self
58    }
59
60    pub fn error<E>(mut self, error: E) -> Self
61    where
62        E: std::fmt::Debug,
63    {
64        self.debug = Some(format!("{:#?}", error));
65        self
66    }
67
68    pub fn build(self) -> Error {
69        Error {
70            message: self.message.unwrap_or_default(),
71            debug: self.debug.unwrap_or_default(),
72        }
73    }
74}