1#[cfg(target_os = "linux")]
2use dbus_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)]
14pub enum KeyringError {
15 IoError(std::io::Error),
17 #[cfg(target_os = "macos")]
19 MacOsKeychainError(SfError),
20 #[cfg(target_os = "ios")]
22 IOSKeychainError(OSStatus),
23 #[cfg(target_os = "linux")]
25 SecretServiceError(SsError),
26 #[cfg(target_os = "windows")]
28 WindowsVaultError(u32),
29 #[cfg(target_os = "android")]
31 AndroidKeystoreError(String),
32 NoBackendFound,
34 NoPasswordFound,
36 Unsupported,
38 Parse(FromUtf8Error),
40 Generic(String),
42}
43
44impl fmt::Display for KeyringError {
45 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
46 match self {
47 KeyringError::IoError(ref err) => write!(f, "Keyring IO error: {}", err),
48 #[cfg(target_os = "macos")]
49 KeyringError::MacOsKeychainError(ref err) => {
50 write!(f, "MacOS keychain Error: {}", err)
51 }
52 #[cfg(target_os = "ios")]
53 KeyringError::IOSKeychainError(ref err) => {
54 write!(f, "iOS keychain Error: {}", err)
55 }
56 #[cfg(target_os = "linux")]
57 KeyringError::SecretServiceError(ref err) => write!(f, "Secret service error: {}", err),
58 #[cfg(target_os = "windows")]
59 KeyringError::WindowsVaultError(code) => {
60 write!(f, "Windows vault error: {}", code)
61 }
62 #[cfg(target_os = "android")]
63 KeyringError::AndroidKeystoreError(s) => {
64 write!(f, "Android keystore error: {}", s)
65 }
66 KeyringError::NoBackendFound => write!(f, "Keyring error: No Backend Found"),
67 KeyringError::NoPasswordFound => write!(f, "Keyring error: No Password Found"),
68 KeyringError::Unsupported => {
69 write!(f, "Keyring error: Operation Unsupported by backend")
70 }
71 KeyringError::Parse(ref err) => write!(f, "Keyring parse error: {}", err),
72 KeyringError::Generic(ref err) => write!(f, "Keyring generic error: {}", err),
73 }
74 }
75}
76
77impl std::error::Error for KeyringError {
78 fn source(&self) -> Option<&(dyn core::error::Error + 'static)> {
79 match *self {
80 KeyringError::IoError(ref err) => Some(err),
81 #[cfg(target_os = "linux")]
82 KeyringError::SecretServiceError(ref err) => Some(err),
83 #[cfg(target_os = "macos")]
84 KeyringError::MacOsKeychainError(ref err) => Some(err),
85 _ => None,
86 }
87 }
88}
89
90impl From<std::io::Error> for KeyringError {
91 fn from(err: std::io::Error) -> KeyringError {
92 KeyringError::IoError(err)
93 }
94}
95
96#[cfg(target_os = "linux")]
97impl From<SsError> for KeyringError {
98 fn from(err: SsError) -> KeyringError {
99 KeyringError::SecretServiceError(err)
100 }
101}
102
103#[cfg(target_os = "macos")]
104impl From<SfError> for KeyringError {
105 fn from(err: SfError) -> KeyringError {
106 KeyringError::MacOsKeychainError(err)
107 }
108}
109
110#[cfg(target_os = "ios")]
111impl From<OSStatus> for KeyringError {
112 fn from(err: OSStatus) -> KeyringError {
113 KeyringError::IOSKeychainError(err)
114 }
115}
116
117impl From<FromUtf8Error> for KeyringError {
118 fn from(err: FromUtf8Error) -> KeyringError {
119 KeyringError::Parse(err)
120 }
121}
122
123impl From<String> for KeyringError {
124 fn from(err: String) -> KeyringError {
125 KeyringError::Generic(err)
126 }
127}