1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
pub mod decrypt_file;
pub mod decrypt_text;
pub mod encrypt_file;
pub mod encrypt_text;

pub mod logic;

pub enum Command {
    Encrypt,
    Decrypt,
    // Pack,
    // Unpack,
}

pub type Result<T, E = Error> = std::result::Result<T, E>;

#[derive(Debug)]
pub struct Error {
    message: String,
    debug: String,
}

impl Error {
    pub fn to_string(&self, debug_mode: bool) -> String {
        format!(
            "Error: {}{}",
            self.message,
            debug_mode
                .then_some(format!("\n{}", self.debug))
                .unwrap_or("".to_string())
        )
    }
}

pub struct ErrorBuilder {
    message: Option<String>,
    debug: Option<String>,
}

impl ErrorBuilder {
    pub fn new() -> Self {
        ErrorBuilder {
            message: None,
            debug: None,
        }
    }

    pub fn message<S>(mut self, message: S) -> Self
    where
        S: AsRef<str>,
    {
        self.message = Some(message.as_ref().to_string());
        self
    }

    pub fn error<E>(mut self, error: E) -> Self
    where
        E: std::fmt::Debug,
    {
        self.debug = Some(format!("{:#?}", error));
        self
    }

    pub fn build(self) -> Error {
        Error {
            message: self.message.unwrap_or_default(),
            debug: self.debug.unwrap_or_default(),
        }
    }
}