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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
#[cfg(any(target_os = "ios"))]
use core_foundation_sys::base::OSStatus;
#[cfg(target_os = "linux")]
use secret_service::Error as SsError;
#[cfg(target_os = "macos")]
use security_framework::base::Error as SfError;
use std::error;
use std::fmt;
use std::string::FromUtf8Error;

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

#[derive(Debug)]
pub enum KeyringError {
    IoError(std::io::Error),
    #[cfg(target_os = "macos")]
    MacOsKeychainError(SfError),
    #[cfg(target_os = "ios")]
    IOSKeychainError(OSStatus),
    #[cfg(target_os = "linux")]
    SecretServiceError(SsError),
    #[cfg(target_os = "windows")]
    WindowsVaultError,
    #[cfg(target_os = "android")]
    AndroidKeystoreError(String),
    NoBackendFound,
    NoPasswordFound,
    Parse(FromUtf8Error),
    Generic(String),
}

impl fmt::Display for KeyringError {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match self {
            KeyringError::IoError(ref err) => write!(f, "Keyring IO error: {}", err),
            #[cfg(target_os = "macos")]
            KeyringError::MacOsKeychainError(ref err) => {
                write!(f, "MacOS keychain Error: {}", err)
            }
            #[cfg(target_os = "ios")]
            KeyringError::IOSKeychainError(ref err) => {
                write!(f, "iOS keychain Error: {}", err)
            }
            #[cfg(target_os = "linux")]
            KeyringError::SecretServiceError(ref err) => write!(f, "Secret service error: {}", err),
            #[cfg(target_os = "windows")]
            KeyringError::WindowsVaultError => write!(f, "Windows vault error"),
            #[cfg(target_os = "android")]
            KeyringError::AndroidKeystoreError(s) => {
                write!(f, "Android keystore error: {}", s)
            }
            KeyringError::NoBackendFound => write!(f, "Keyring error: No Backend Found"),
            KeyringError::NoPasswordFound => write!(f, "Keyring error: No Password Found"),
            KeyringError::Parse(ref err) => write!(f, "Keyring parse error: {}", err),
            KeyringError::Generic(ref err) => write!(f, "Keyring generic error: {}", err),
        }
    }
}

impl error::Error for KeyringError {
    fn source(&self) -> Option<&(dyn error::Error + 'static)> {
        match *self {
            KeyringError::IoError(ref err) => Some(err),
            #[cfg(target_os = "linux")]
            KeyringError::SecretServiceError(ref err) => Some(err),
            #[cfg(target_os = "macos")]
            KeyringError::MacOsKeychainError(ref err) => Some(err),
            _ => None,
        }
    }
}

impl From<std::io::Error> for KeyringError {
    fn from(err: std::io::Error) -> KeyringError {
        KeyringError::IoError(err)
    }
}

#[cfg(target_os = "linux")]
impl From<SsError> for KeyringError {
    fn from(err: SsError) -> KeyringError {
        KeyringError::SecretServiceError(err)
    }
}

#[cfg(target_os = "macos")]
impl From<SfError> for KeyringError {
    fn from(err: SfError) -> KeyringError {
        KeyringError::MacOsKeychainError(err)
    }
}

#[cfg(target_os = "ios")]
impl From<OSStatus> for KeyringError {
    fn from(err: OSStatus) -> KeyringError {
        KeyringError::IOSKeychainError(err)
    }
}

impl From<FromUtf8Error> for KeyringError {
    fn from(err: FromUtf8Error) -> KeyringError {
        KeyringError::Parse(err)
    }
}

impl From<String> for KeyringError {
    fn from(err: String) -> KeyringError {
        KeyringError::Generic(err)
    }
}