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