keyring_manager/
error.rs

1#[cfg(target_os = "linux")]
2use secret_service::Error as SsError;
3#[cfg(target_os = "macos")]
4use security_framework::base::Error as SfError;
5use std::fmt;
6use std::string::FromUtf8Error;
7
8#[cfg(target_os = "ios")]
9pub type OSStatus = i32;
10pub type Result<T> = ::std::result::Result<T, KeyringError>;
11
12#[derive(Debug)]
13pub enum KeyringError {
14    IoError(std::io::Error),
15    #[cfg(target_os = "macos")]
16    MacOsKeychainError(SfError),
17    #[cfg(target_os = "ios")]
18    IOSKeychainError(OSStatus),
19    #[cfg(target_os = "linux")]
20    SecretServiceError(SsError),
21    #[cfg(target_os = "windows")]
22    WindowsVaultError,
23    #[cfg(target_os = "android")]
24    AndroidKeystoreError(String),
25    NoBackendFound,
26    NoPasswordFound,
27    Parse(FromUtf8Error),
28    Generic(String),
29}
30
31impl fmt::Display for KeyringError {
32    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
33        match self {
34            KeyringError::IoError(ref err) => write!(f, "Keyring IO error: {}", err),
35            #[cfg(target_os = "macos")]
36            KeyringError::MacOsKeychainError(ref err) => {
37                write!(f, "MacOS keychain Error: {}", err)
38            }
39            #[cfg(target_os = "ios")]
40            KeyringError::IOSKeychainError(ref err) => {
41                write!(f, "iOS keychain Error: {}", err)
42            }
43            #[cfg(target_os = "linux")]
44            KeyringError::SecretServiceError(ref err) => write!(f, "Secret service error: {}", err),
45            #[cfg(target_os = "windows")]
46            KeyringError::WindowsVaultError => write!(f, "Windows vault error"),
47            #[cfg(target_os = "android")]
48            KeyringError::AndroidKeystoreError(s) => {
49                write!(f, "Android keystore error: {}", s)
50            }
51            KeyringError::NoBackendFound => write!(f, "Keyring error: No Backend Found"),
52            KeyringError::NoPasswordFound => write!(f, "Keyring error: No Password Found"),
53            KeyringError::Parse(ref err) => write!(f, "Keyring parse error: {}", err),
54            KeyringError::Generic(ref err) => write!(f, "Keyring generic error: {}", err),
55        }
56    }
57}
58
59impl std::error::Error for KeyringError {
60    fn source(&self) -> Option<&(dyn core::error::Error + 'static)> {
61        match *self {
62            KeyringError::IoError(ref err) => Some(err),
63            #[cfg(target_os = "linux")]
64            KeyringError::SecretServiceError(ref err) => Some(err),
65            #[cfg(target_os = "macos")]
66            KeyringError::MacOsKeychainError(ref err) => Some(err),
67            _ => None,
68        }
69    }
70}
71
72impl From<std::io::Error> for KeyringError {
73    fn from(err: std::io::Error) -> KeyringError {
74        KeyringError::IoError(err)
75    }
76}
77
78#[cfg(target_os = "linux")]
79impl From<SsError> for KeyringError {
80    fn from(err: SsError) -> KeyringError {
81        KeyringError::SecretServiceError(err)
82    }
83}
84
85#[cfg(target_os = "macos")]
86impl From<SfError> for KeyringError {
87    fn from(err: SfError) -> KeyringError {
88        KeyringError::MacOsKeychainError(err)
89    }
90}
91
92#[cfg(target_os = "ios")]
93impl From<OSStatus> for KeyringError {
94    fn from(err: OSStatus) -> KeyringError {
95        KeyringError::IOSKeychainError(err)
96    }
97}
98
99impl From<FromUtf8Error> for KeyringError {
100    fn from(err: FromUtf8Error) -> KeyringError {
101        KeyringError::Parse(err)
102    }
103}
104
105impl From<String> for KeyringError {
106    fn from(err: String) -> KeyringError {
107        KeyringError::Generic(err)
108    }
109}