~gpanders/passage

ref: 849cef24e44e6b1979a78f9244c02a329a8ed634 passage/src/error.rs -rw-r--r-- 2.0 KiB
849cef24Gregory Anders Add uninstall target to Makefile 11 months ago
                                                                                
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
71
72
73
74
75
76
77
78
use std::error;
use std::fmt;
use std::io;

#[derive(Debug)]
pub enum Error {
    ItemNotFound(String),
    ItemAlreadyExists(String),
    StoreNotInitialized,
    NoSecretKey,
    SecretKeyExists,
    KeyNotEncrypted,
    PasswordsDoNotMatch,
    Other(String),
}

impl fmt::Display for Error {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Error::ItemNotFound(item) => write!(f, "{} is not in the password store.", item),
            Error::ItemAlreadyExists(item) => {
                write!(f, "{} already exists in the password store.", item)
            }
            Error::StoreNotInitialized => {
                write!(f, "Password store is empty. Try \"passage init\".")
            }
            Error::NoSecretKey => write!(f, "No secret key found. Try \"passage init\"."),
            Error::SecretKeyExists => {
                write!(f, "Secret key already exists. Use --force to overwrite.")
            }
            Error::KeyNotEncrypted => write!(f, "Password store is not encrypted."),
            Error::PasswordsDoNotMatch => write!(f, "Passwords do not match."),
            Error::Other(msg) => write!(f, "{}", msg),
        }
    }
}

impl error::Error for Error {
    fn source(&self) -> Option<&(dyn error::Error + 'static)> {
        None
    }
}

impl From<io::Error> for Error {
    fn from(e: io::Error) -> Self {
        Error::Other(e.to_string())
    }
}

impl From<age::DecryptError> for Error {
    fn from(e: age::DecryptError) -> Self {
        Error::Other(e.to_string())
    }
}

impl From<age::EncryptError> for Error {
    fn from(e: age::EncryptError) -> Self {
        Error::Other(e.to_string())
    }
}

impl From<pinentry::Error> for Error {
    fn from(e: pinentry::Error) -> Self {
        Error::Other(e.to_string())
    }
}

impl From<String> for Error {
    fn from(e: String) -> Self {
        Error::Other(e)
    }
}

impl From<Box<dyn error::Error>> for Error {
    fn from(e: Box<dyn error::Error>) -> Self {
        Error::Other(e.to_string())
    }
}