crab_gnupg/utils/
errors.rs

1use std::fmt::{Display, Formatter};
2
3use super::response::CmdResult;
4
5#[derive(Debug)]
6pub struct GPGError {
7    // the type of error
8    pub error_type: GPGErrorType,
9    // provide more insight if error occured during the gpg cmd process
10    pub cmd_result: Option<CmdResult>,
11}
12
13#[doc(hidden)]
14impl GPGError {
15    pub fn new(error_type: GPGErrorType, cmd_result: Option<CmdResult>) -> GPGError {
16        return GPGError {
17            error_type,
18            cmd_result,
19        };
20    }
21}
22
23#[derive(Debug)]
24pub enum GPGErrorType {
25    HomedirError(String),
26    OutputDirError(String),
27    GPGInitError(String),
28    GPGNotFoundError(String),
29    GPGProcessError(String),
30    InvalidArgumentError(String),
31    FailedToStartProcess(String),
32    FailedToRetrieveChildProcess(String),
33    WriteFailError(String),
34    ReadFailError(String),
35    PassphraseError(String),
36    KeyNotSubkey(String),
37    InvalidReasonCode(String),
38    FileNotFoundError(String),
39    FileNotProvidedError(String),
40}
41
42#[doc(hidden)]
43impl Display for GPGErrorType {
44    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
45        match self {
46            GPGErrorType::HomedirError(err) => write!(f, "[HomedirError] {}", err),
47            GPGErrorType::OutputDirError(err) => write!(f, "[OutputDirError] {}", err),
48            GPGErrorType::GPGInitError(err) => write!(f, "[GPGInitError] {}", err),
49            GPGErrorType::GPGNotFoundError(err) => write!(f, "[GPGNotFoundError] {}", err),
50            GPGErrorType::GPGProcessError(err) => write!(f, "[GPGProcessError] {}", err),
51            GPGErrorType::InvalidArgumentError(err) => write!(f, "[InvalidArgumentError] {}", err),
52            GPGErrorType::FailedToStartProcess(err) => write!(f, "[FailedToStartProcess] {}", err),
53            GPGErrorType::FailedToRetrieveChildProcess(err) => {
54                write!(f, "[FailedToRetrieveChildProcess] {}", err)
55            }
56            GPGErrorType::WriteFailError(err) => write!(f, "[WriteFailError] {}", err),
57            GPGErrorType::ReadFailError(err) => write!(f, "[ReadFailError] {}", err),
58            GPGErrorType::PassphraseError(err) => write!(f, "[PassphraseError] {}", err),
59            GPGErrorType::KeyNotSubkey(err) => write!(f, "[KeyNotSubkey] {}", err),
60            GPGErrorType::InvalidReasonCode(err) => write!(f, "[InvalidReasonCode] {}", err),
61            GPGErrorType::FileNotFoundError(err) => write!(f, "[FileNotFoundError] {}", err),
62            GPGErrorType::FileNotProvidedError(err) => write!(f, "[FileNotProvidedError] {}", err),
63        }
64    }
65}