Skip to content

Commit 888b19b

Browse files
committed
fix(credential)!: Decouple ErrorKind from BoxError
BREAKING CHANGE: `Error` because an opaque type with the enum broken out into `ErrorKind` - To construct an error from an `ErrorKind`, you can use `Into`. - To wrap an existing error, use `Error::other`
1 parent 5c142dd commit 888b19b

File tree

12 files changed

+183
-111
lines changed

12 files changed

+183
-111
lines changed

credential/cargo-credential-1password/src/main.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
#![allow(clippy::print_stderr)]
55

66
use cargo_credential::{
7-
Action, CacheControl, Credential, CredentialResponse, Error, RegistryInfo, Secret,
7+
Action, CacheControl, Credential, CredentialResponse, Error, ErrorKind, RegistryInfo, Secret,
88
};
99
use serde::Deserialize;
1010
use std::io::Read;
@@ -278,7 +278,7 @@ impl Credential for OnePasswordCredential {
278278
operation_independent: true,
279279
})
280280
} else {
281-
Err(Error::NotFound)
281+
Err(ErrorKind::NotFound.into())
282282
}
283283
}
284284
Action::Login(options) => {
@@ -301,10 +301,10 @@ impl Credential for OnePasswordCredential {
301301
op.delete(&session, &id)?;
302302
Ok(CredentialResponse::Logout)
303303
} else {
304-
Err(Error::NotFound)
304+
Err(ErrorKind::NotFound.into())
305305
}
306306
}
307-
_ => Err(Error::OperationNotSupported),
307+
_ => Err(ErrorKind::OperationNotSupported.into()),
308308
}
309309
}
310310
}

credential/cargo-credential-libsecret/src/lib.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ mod linux {
44
55
use anyhow::Context;
66
use cargo_credential::{
7-
read_token, Action, CacheControl, Credential, CredentialResponse, Error, RegistryInfo,
8-
Secret,
7+
read_token, Action, CacheControl, Credential, CredentialResponse, Error, ErrorKind,
8+
RegistryInfo, Secret,
99
};
1010
use libloading::{Library, Symbol};
1111
use std::ffi::{CStr, CString};
@@ -153,7 +153,7 @@ mod linux {
153153
.into());
154154
}
155155
if token_c.is_null() {
156-
return Err(Error::NotFound);
156+
return Err(ErrorKind::NotFound.into());
157157
}
158158
let token = Secret::from(
159159
CStr::from_ptr(token_c)
@@ -223,7 +223,7 @@ mod linux {
223223
}
224224
Ok(CredentialResponse::Logout)
225225
}
226-
_ => Err(Error::OperationNotSupported),
226+
_ => Err(ErrorKind::OperationNotSupported.into()),
227227
}
228228
}
229229
}

credential/cargo-credential-macos-keychain/src/lib.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55
#[cfg(target_os = "macos")]
66
mod macos {
77
use cargo_credential::{
8-
read_token, Action, CacheControl, Credential, CredentialResponse, Error, RegistryInfo,
8+
read_token, Action, CacheControl, Credential, CredentialResponse, Error, ErrorKind,
9+
RegistryInfo,
910
};
1011
use security_framework::os::macos::keychain::SecKeychain;
1112

@@ -31,7 +32,7 @@ mod macos {
3132
let not_found = security_framework::base::Error::from(NOT_FOUND).code();
3233
match action {
3334
Action::Get(_) => match keychain.find_generic_password(&service_name, ACCOUNT) {
34-
Err(e) if e.code() == not_found => Err(Error::NotFound),
35+
Err(e) if e.code() == not_found => Err(ErrorKind::NotFound.into()),
3536
Err(e) => Err(Box::new(e).into()),
3637
Ok((pass, _)) => {
3738
let token = String::from_utf8(pass.as_ref().to_vec()).map_err(Box::new)?;
@@ -64,14 +65,14 @@ mod macos {
6465
Ok(CredentialResponse::Login)
6566
}
6667
Action::Logout => match keychain.find_generic_password(&service_name, ACCOUNT) {
67-
Err(e) if e.code() == not_found => Err(Error::NotFound),
68+
Err(e) if e.code() == not_found => Err(ErrorKind::NotFound.into()),
6869
Err(e) => Err(Box::new(e).into()),
6970
Ok((_, item)) => {
7071
item.delete();
7172
Ok(CredentialResponse::Logout)
7273
}
7374
},
74-
_ => Err(Error::OperationNotSupported),
75+
_ => Err(ErrorKind::OperationNotSupported.into()),
7576
}
7677
}
7778
}

credential/cargo-credential-wincred/src/lib.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
#[cfg(windows)]
44
mod win {
55
use cargo_credential::{read_token, Action, CacheControl, CredentialResponse, RegistryInfo};
6-
use cargo_credential::{Credential, Error};
6+
use cargo_credential::{Credential, Error, ErrorKind};
77
use std::ffi::OsStr;
88

99
use std::os::windows::ffi::OsStrExt;
@@ -56,7 +56,7 @@ mod win {
5656
{
5757
let err = std::io::Error::last_os_error();
5858
if err.raw_os_error() == Some(ERROR_NOT_FOUND as i32) {
59-
return Err(Error::NotFound);
59+
return Err(ErrorKind::NotFound.into());
6060
}
6161
return Err(Box::new(err).into());
6262
}
@@ -107,13 +107,13 @@ mod win {
107107
if result != TRUE {
108108
let err = std::io::Error::last_os_error();
109109
if err.raw_os_error() == Some(ERROR_NOT_FOUND as i32) {
110-
return Err(Error::NotFound);
110+
return Err(ErrorKind::NotFound.into());
111111
}
112112
return Err(Box::new(err).into());
113113
}
114114
Ok(CredentialResponse::Logout)
115115
}
116-
_ => Err(Error::OperationNotSupported),
116+
_ => Err(ErrorKind::OperationNotSupported.into()),
117117
}
118118
}
119119
}

credential/cargo-credential/examples/file-provider.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,11 @@ impl Credential for FileCredential {
2121
// another provider for any other registry.
2222
//
2323
// If a provider supports any registry, then this check should be omitted.
24-
return Err(cargo_credential::Error::UrlNotSupported);
24+
return Err(cargo_credential::ErrorKind::UrlNotSupported.into());
2525
}
2626

2727
// `Error::Other` takes a boxed `std::error::Error` type that causes Cargo to show the error.
28-
let mut creds = FileCredential::read().map_err(cargo_credential::Error::Other)?;
28+
let mut creds = FileCredential::read().map_err(cargo_credential::Error::other)?;
2929

3030
match action {
3131
Action::Get(_) => {
@@ -39,7 +39,7 @@ impl Credential for FileCredential {
3939
} else {
4040
// Credential providers should respond with `NotFound` when a credential can not be
4141
// found, allowing Cargo to attempt another provider.
42-
Err(cargo_credential::Error::NotFound)
42+
Err(cargo_credential::ErrorKind::NotFound.into())
4343
}
4444
}
4545
Action::Login(login_options) => {
@@ -50,7 +50,7 @@ impl Credential for FileCredential {
5050
let token = cargo_credential::read_token(login_options, registry)?;
5151
creds.insert(registry.index_url.to_string(), token);
5252

53-
FileCredential::write(&creds).map_err(cargo_credential::Error::Other)?;
53+
FileCredential::write(&creds).map_err(cargo_credential::Error::other)?;
5454

5555
// Credentials were successfully stored.
5656
Ok(CredentialResponse::Login)
@@ -59,14 +59,14 @@ impl Credential for FileCredential {
5959
if creds.remove(registry.index_url).is_none() {
6060
// If the user attempts to log out from a registry that has no credentials
6161
// stored, then NotFound is the appropriate error.
62-
Err(cargo_credential::Error::NotFound)
62+
Err(cargo_credential::ErrorKind::NotFound.into())
6363
} else {
6464
// Credentials were successfully erased.
6565
Ok(CredentialResponse::Logout)
6666
}
6767
}
6868
// If a credential provider doesn't support a given operation, it should respond with `OperationNotSupported`.
69-
_ => Err(cargo_credential::Error::OperationNotSupported),
69+
_ => Err(cargo_credential::ErrorKind::OperationNotSupported.into()),
7070
}
7171
}
7272
}

credential/cargo-credential/examples/stdout-redirected.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
#![allow(clippy::print_stderr)]
44
#![allow(clippy::print_stdout)]
55

6-
use cargo_credential::{Action, Credential, CredentialResponse, Error, RegistryInfo};
6+
use cargo_credential::{Action, Credential, CredentialResponse, Error, ErrorKind, RegistryInfo};
77

88
struct MyCredential;
99

@@ -19,7 +19,7 @@ impl Credential for MyCredential {
1919

2020
// Reading from stdin and writing to stdout will go to the attached console (tty).
2121
println!("message from test credential provider");
22-
Err(Error::OperationNotSupported)
22+
Err(ErrorKind::OperationNotSupported.into())
2323
}
2424
}
2525

0 commit comments

Comments
 (0)