Skip to content

Commit 0aa56d0

Browse files
committed
refactor: yank consolidating version logic
1 parent 0186d6b commit 0aa56d0

File tree

2 files changed

+24
-22
lines changed

2 files changed

+24
-22
lines changed

src/bin/cargo/commands/yank.rs

Lines changed: 23 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,11 @@ pub fn exec(gctx: &mut GlobalContext, args: &ArgMatches) -> CliResult {
3030
args.get_one::<String>("crate").map(String::as_str),
3131
args.get_one::<String>("version").map(String::as_str),
3232
)?;
33-
if version.is_none() {
34-
return Err(anyhow::format_err!("`--version` is required").into());
35-
}
3633

3734
ops::yank(
3835
gctx,
3936
krate.map(|s| s.to_string()),
40-
version.map(|s| s.to_string()),
37+
version.to_string(),
4138
args.get_one::<String>("token").cloned().map(Secret::from),
4239
args.registry_or_index(gctx)?,
4340
args.flag("undo"),
@@ -46,19 +43,28 @@ pub fn exec(gctx: &mut GlobalContext, args: &ArgMatches) -> CliResult {
4643
}
4744

4845
fn resolve_crate<'k>(
49-
mut krate: Option<&'k str>,
50-
mut version: Option<&'k str>,
51-
) -> crate::CargoResult<(Option<&'k str>, Option<&'k str>)> {
52-
if let Some((k, v)) = krate.and_then(|k| k.split_once('@')) {
53-
if version.is_some() {
54-
anyhow::bail!("cannot specify both `@{v}` and `--version`");
55-
}
56-
if k.is_empty() {
57-
// by convention, arguments starting with `@` are response files
58-
anyhow::bail!("missing crate name for `@{v}`");
46+
krate: Option<&'k str>,
47+
version: Option<&'k str>,
48+
) -> crate::CargoResult<(Option<&'k str>, &'k str)> {
49+
match krate.and_then(|k| k.split_once('@')) {
50+
Some((name, embedded_version)) => {
51+
if name.is_empty() {
52+
// by convention, arguments starting with `@` are response files
53+
anyhow::bail!("missing crate name for `@{embedded_version}`");
54+
}
55+
56+
match version {
57+
None => Ok((Some(name), embedded_version)),
58+
Some(_) => {
59+
anyhow::bail!("cannot specify both `@{embedded_version}` and `--version`");
60+
}
61+
}
5962
}
60-
krate = Some(k);
61-
version = Some(v);
63+
None => match version {
64+
Some(version) => Ok((krate, version)),
65+
None => {
66+
anyhow::bail!("`--version` is required");
67+
}
68+
},
6269
}
63-
Ok((krate, version))
6470
}

src/cargo/ops/registry/yank.rs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
//! [yank]: https://doc.rust-lang.org/nightly/cargo/reference/registry-web-api.html#yank
44
//! [unyank]: https://doc.rust-lang.org/nightly/cargo/reference/registry-web-api.html#unyank
55
6-
use anyhow::bail;
76
use anyhow::Context as _;
87
use cargo_credential::Operation;
98
use cargo_credential::Secret;
@@ -18,7 +17,7 @@ use super::RegistryOrIndex;
1817
pub fn yank(
1918
gctx: &GlobalContext,
2019
krate: Option<String>,
21-
version: Option<String>,
20+
version: String,
2221
token: Option<Secret<String>>,
2322
reg_or_index: Option<RegistryOrIndex>,
2423
undo: bool,
@@ -31,9 +30,6 @@ pub fn yank(
3130
ws.current()?.package_id().name().to_string()
3231
}
3332
};
34-
let Some(version) = version else {
35-
bail!("a version must be specified to yank")
36-
};
3733

3834
let message = if undo {
3935
Operation::Unyank {

0 commit comments

Comments
 (0)