crab_gnupg/utils/
errors.rs

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
use std::fmt::{Display, Formatter};

use super::response::CmdResult;

#[derive(Debug)]
pub struct GPGError {
    // the type of error
    pub error_type: GPGErrorType,
    // provide more insight if error occured during the gpg cmd process
    pub cmd_result: Option<CmdResult>,
}

#[doc(hidden)]
impl GPGError {
    pub fn new(error_type: GPGErrorType, cmd_result: Option<CmdResult>) -> GPGError {
        return GPGError {
            error_type,
            cmd_result,
        };
    }
}

#[derive(Debug)]
pub enum GPGErrorType {
    HomedirError(String),
    OutputDirError(String),
    GPGInitError(String),
    GPGNotFoundError(String),
    GPGProcessError(String),
    InvalidArgumentError(String),
    FailedToStartProcess(String),
    FailedToRetrieveChildProcess(String),
    WriteFailError(String),
    ReadFailError(String),
    PassphraseError(String),
    FileNotFoundError(String),
    FileNotProvidedError(String),
}

#[doc(hidden)]
impl Display for GPGErrorType {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        match self {
            GPGErrorType::HomedirError(err) => write!(f, "[HomedirError] {}", err),
            GPGErrorType::OutputDirError(err) => write!(f, "[OutputDirError] {}", err),
            GPGErrorType::GPGInitError(err) => write!(f, "[GPGInitError] {}", err),
            GPGErrorType::GPGNotFoundError(err) => write!(f, "[GPGNotFoundError] {}", err),
            GPGErrorType::GPGProcessError(err) => write!(f, "[GPGProcessError] {}", err),
            GPGErrorType::InvalidArgumentError(err) => write!(f, "[InvalidArgumentError] {}", err),
            GPGErrorType::FailedToStartProcess(err) => write!(f, "[FailedToStartProcess] {}", err),
            GPGErrorType::FailedToRetrieveChildProcess(err) => {
                write!(f, "[FailedToRetrieveChildProcess] {}", err)
            }
            GPGErrorType::WriteFailError(err) => write!(f, "[WriteFailError] {}", err),
            GPGErrorType::ReadFailError(err) => write!(f, "[ReadFailError] {}", err),
            GPGErrorType::PassphraseError(err) => write!(f, "[PassphraseError] {}", err),
            GPGErrorType::FileNotFoundError(err) => write!(f, "[FileNotFoundError] {}", err),
            GPGErrorType::FileNotProvidedError(err) => write!(f, "[FileNotProvidedError] {}", err),
        }
    }
}