Skip to content

Commit 7918c7f

Browse files
committed
Remove from impls
1 parent a81d558 commit 7918c7f

File tree

6 files changed

+60
-40
lines changed

6 files changed

+60
-40
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ impl OnePasswordKeychain {
8080
let mut cmd = Command::new("op");
8181
cmd.args(["signin", "--raw"]);
8282
cmd.stdout(Stdio::piped());
83-
cmd.stdin(cargo_credential::tty()?);
83+
cmd.stdin(cargo_credential::tty().map_err(Box::new)?);
8484
let mut child = cmd
8585
.spawn()
8686
.map_err(|e| format!("failed to spawn `op`: {}", e))?;
@@ -228,7 +228,7 @@ impl OnePasswordKeychain {
228228
// For unknown reasons, `op item create` seems to not be happy if
229229
// stdin is not a tty. Otherwise it returns with a 0 exit code without
230230
// doing anything.
231-
cmd.stdin(cargo_credential::tty()?);
231+
cmd.stdin(cargo_credential::tty().map_err(Box::new)?);
232232
self.run_cmd(cmd)?;
233233
Ok(())
234234
}

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ mod win {
5858
if err.raw_os_error() == Some(ERROR_NOT_FOUND as i32) {
5959
return Err(Error::NotFound);
6060
}
61-
return Err(err.into());
61+
return Err(Box::new(err).into());
6262
}
6363
std::slice::from_raw_parts(
6464
(*p_credential).CredentialBlob,
@@ -97,7 +97,7 @@ mod win {
9797
let result = unsafe { CredWriteW(&credential, 0) };
9898
if result != TRUE {
9999
let err = std::io::Error::last_os_error();
100-
return Err(err.into());
100+
return Err(Box::new(err).into());
101101
}
102102
Ok(CredentialResponse::Login)
103103
}
@@ -109,7 +109,7 @@ mod win {
109109
if err.raw_os_error() == Some(ERROR_NOT_FOUND as i32) {
110110
return Err(Error::NotFound);
111111
}
112-
return Err(err.into());
112+
return Err(Box::new(err).into());
113113
}
114114
Ok(CredentialResponse::Logout)
115115
}

credential/cargo-credential/src/error.rs

Lines changed: 26 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -13,29 +13,32 @@ use thiserror::Error as ThisError;
1313
#[serde(rename_all = "kebab-case", tag = "kind")]
1414
#[non_exhaustive]
1515
pub enum Error {
16+
/// Registry URL is not supported. This should be used if
17+
/// the provider only works for some registries. Cargo will
18+
/// try another provider, if available
1619
#[error("registry not supported")]
1720
UrlNotSupported,
21+
22+
/// Credentials could not be found. Cargo will try another
23+
/// provider, if available
1824
#[error("credential not found")]
1925
NotFound,
26+
27+
/// The provider doesn't support this operation, such as
28+
/// a provider that can't support 'login' / 'logout'
2029
#[error("requested operation not supported")]
2130
OperationNotSupported,
22-
#[error("protocol version {version} not supported")]
23-
ProtocolNotSupported { version: u32 },
31+
32+
/// The provider failed to perform the operation. Other
33+
/// providers will not be attempted
2434
#[error(transparent)]
2535
#[serde(with = "error_serialize")]
2636
Other(Box<dyn StdError + Sync + Send>),
27-
}
28-
29-
impl From<std::io::Error> for Error {
30-
fn from(err: std::io::Error) -> Self {
31-
Box::new(err).into()
32-
}
33-
}
3437

35-
impl From<serde_json::Error> for Error {
36-
fn from(err: serde_json::Error) -> Self {
37-
Box::new(err).into()
38-
}
38+
/// A new variant was added to this enum since Cargo was built
39+
#[error("unknown error kind; try updating Cargo?")]
40+
#[serde(other)]
41+
Unknown,
3942
}
4043

4144
impl From<String> for Error {
@@ -156,6 +159,16 @@ mod error_serialize {
156159
mod tests {
157160
use super::Error;
158161

162+
#[test]
163+
pub fn unknown_kind() {
164+
let json = r#"{
165+
"kind": "unexpected-kind",
166+
"unexpected-content": "test"
167+
}"#;
168+
let e: Error = serde_json::from_str(&json).unwrap();
169+
assert!(matches!(e, Error::Unknown));
170+
}
171+
159172
#[test]
160173
pub fn roundtrip() {
161174
// Construct an error with context

credential/cargo-credential/src/lib.rs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -189,23 +189,24 @@ fn doit(credential: impl Credential) -> Result<(), Error> {
189189
let hello = CredentialHello {
190190
v: vec![PROTOCOL_VERSION_1],
191191
};
192-
serde_json::to_writer(std::io::stdout(), &hello)?;
192+
serde_json::to_writer(std::io::stdout(), &hello).map_err(Box::new)?;
193193
println!();
194194

195195
loop {
196196
let mut buffer = String::new();
197-
let len = std::io::stdin().read_line(&mut buffer)?;
197+
let len = std::io::stdin().read_line(&mut buffer).map_err(Box::new)?;
198198
if len == 0 {
199199
return Ok(());
200200
}
201-
let request: CredentialRequest = serde_json::from_str(&buffer)?;
201+
let request: CredentialRequest = serde_json::from_str(&buffer).map_err(Box::new)?;
202202
if request.v != PROTOCOL_VERSION_1 {
203-
return Err(Error::ProtocolNotSupported { version: request.v });
203+
return Err(format!("unsupported protocol version {}", request.v).into());
204204
}
205205
serde_json::to_writer(
206206
std::io::stdout(),
207207
&credential.perform(&request.registry, &request.action, &request.args),
208-
)?;
208+
)
209+
.map_err(Box::new)?;
209210
println!();
210211
}
211212
}
@@ -248,5 +249,5 @@ pub fn read_token(
248249
eprintln!("please paste the token for {} below", registry.index_url);
249250
}
250251

251-
Ok(Secret::from(read_line()?))
252+
Ok(Secret::from(read_line().map_err(Box::new)?))
252253
}

src/cargo/util/credential/adaptor.rs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ use std::{
55
process::{Command, Stdio},
66
};
77

8+
use anyhow::Context;
89
use cargo_credential::{
910
Action, CacheControl, Credential, CredentialResponse, RegistryInfo, Secret,
1011
};
@@ -32,9 +33,14 @@ impl Credential for BasicProcessCredential {
3233
cmd.env("CARGO_REGISTRY_NAME_OPT", name);
3334
}
3435
cmd.stdout(Stdio::piped());
35-
let mut child = cmd.spawn()?;
36+
let mut child = cmd.spawn().context("failed to spawn credential process")?;
3637
let mut buffer = String::new();
37-
child.stdout.take().unwrap().read_to_string(&mut buffer)?;
38+
child
39+
.stdout
40+
.take()
41+
.unwrap()
42+
.read_to_string(&mut buffer)
43+
.context("failed to read from credential provider")?;
3844
if let Some(end) = buffer.find('\n') {
3945
if buffer.len() > end + 1 {
4046
return Err(format!(
@@ -46,7 +52,7 @@ impl Credential for BasicProcessCredential {
4652
}
4753
buffer.truncate(end);
4854
}
49-
let status = child.wait().expect("process was started");
55+
let status = child.wait().context("credential process never started")?;
5056
if !status.success() {
5157
return Err(format!("process `{}` failed with status `{status}`", exe).into());
5258
}

src/cargo/util/credential/process.rs

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -36,17 +36,15 @@ impl<'a> Credential for CredentialProcessCredential {
3636
cmd.stdin(Stdio::piped());
3737
cmd.arg("--cargo-plugin");
3838
log::debug!("credential-process: {cmd:?}");
39-
let mut child = cmd.spawn().with_context(|| {
40-
format!(
41-
"failed to spawn credential process `{}`",
42-
self.path.display()
43-
)
44-
})?;
39+
let mut child = cmd.spawn().context("failed to spawn credential process")?;
4540
let mut output_from_child = BufReader::new(child.stdout.take().unwrap());
4641
let mut input_to_child = child.stdin.take().unwrap();
4742
let mut buffer = String::new();
48-
output_from_child.read_line(&mut buffer)?;
49-
let credential_hello: CredentialHello = serde_json::from_str(&buffer)?;
43+
output_from_child
44+
.read_line(&mut buffer)
45+
.context("failed to read hello from credential provider")?;
46+
let credential_hello: CredentialHello =
47+
serde_json::from_str(&buffer).context("failed to deserialize hello")?;
5048
log::debug!("credential-process > {credential_hello:?}");
5149

5250
let req = CredentialRequest {
@@ -55,17 +53,19 @@ impl<'a> Credential for CredentialProcessCredential {
5553
registry: registry.clone(),
5654
args: args.to_vec(),
5755
};
58-
let request = serde_json::to_string(&req)?;
56+
let request = serde_json::to_string(&req).context("failed to serialize request")?;
5957
log::debug!("credential-process < {req:?}");
60-
writeln!(input_to_child, "{request}")?;
58+
writeln!(input_to_child, "{request}").context("failed to write to credential provider")?;
6159

6260
buffer.clear();
63-
output_from_child.read_line(&mut buffer)?;
61+
output_from_child
62+
.read_line(&mut buffer)
63+
.context("failed to read response from credential provider")?;
6464
let response: Result<CredentialResponse, cargo_credential::Error> =
65-
serde_json::from_str(&buffer)?;
65+
serde_json::from_str(&buffer).context("failed to deserialize response")?;
6666
log::debug!("credential-process > {response:?}");
6767
drop(input_to_child);
68-
let status = child.wait().expect("credential process never started");
68+
let status = child.wait().context("credential process never started")?;
6969
if !status.success() {
7070
return Err(anyhow::anyhow!(
7171
"credential process `{}` failed with status {}`",

0 commit comments

Comments
 (0)