Skip to content

Commit 94450a6

Browse files
committed
refactor(toolchain/names): replace toolchain_sort with ToolchainName's Ord instance
1 parent 27e41b1 commit 94450a6

File tree

4 files changed

+22
-33
lines changed

4 files changed

+22
-33
lines changed

src/config.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -867,7 +867,7 @@ impl<'a> Cfg<'a> {
867867
.filter_map(|n| ToolchainName::try_from(&n).ok())
868868
.collect();
869869

870-
crate::toolchain::toolchain_sort(&mut toolchains);
870+
toolchains.sort();
871871

872872
Ok(toolchains)
873873
} else {

src/toolchain.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ pub(crate) use distributable::DistributableToolchain;
2929

3030
mod names;
3131
pub(crate) use names::{
32-
toolchain_sort, CustomToolchainName, LocalToolchainName, MaybeOfficialToolchainName,
32+
CustomToolchainName, LocalToolchainName, MaybeOfficialToolchainName,
3333
MaybeResolvableToolchainName, PathBasedToolchainName, ResolvableLocalToolchainName,
3434
ResolvableToolchainName, ToolchainName,
3535
};

src/toolchain/names.rs

Lines changed: 17 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -248,10 +248,10 @@ impl Display for MaybeOfficialToolchainName {
248248
/// ToolchainName can be used in calls to Cfg that alter configuration,
249249
/// like setting overrides, or that depend on configuration, like calculating
250250
/// the toolchain directory.
251-
#[derive(Clone, Debug, Eq, PartialEq)]
251+
#[derive(Clone, Debug, Eq, PartialEq, Ord, PartialOrd)]
252252
pub enum ToolchainName {
253-
Custom(CustomToolchainName),
254253
Official(ToolchainDesc),
254+
Custom(CustomToolchainName),
255255
}
256256

257257
impl ToolchainName {
@@ -280,31 +280,6 @@ impl Display for ToolchainName {
280280
}
281281
}
282282

283-
/// Sorts [`ToolchainName`]s in the following order:
284-
/// 1. `stable`/`beta`/`nightly`-prefixed names, in this exact order.
285-
/// 2. `X.Y.Z-suffix` names, sorted by semver rules on `X.Y.Z`, then by `suffix`.
286-
/// 3. Other names, sorted alphanumerically.
287-
pub(crate) fn toolchain_sort(v: &mut [ToolchainName]) {
288-
v.sort_by_key(|name| {
289-
let s = name.to_string();
290-
if s.starts_with("stable") {
291-
return (0, None, s);
292-
}
293-
if s.starts_with("beta") {
294-
return (1, None, s);
295-
}
296-
if s.starts_with("nightly") {
297-
return (2, None, s);
298-
}
299-
if let Some((ver_str, suffix)) = s.split_once('-') {
300-
if let Ok(ver) = semver::Version::parse(ver_str) {
301-
return (3, Some(ver), suffix.to_owned());
302-
}
303-
}
304-
(4, None, s)
305-
})
306-
}
307-
308283
/// ResolvableLocalToolchainName is used to process values set in
309284
/// RUSTUP_TOOLCHAIN: resolvable and resolved official names, custom names and
310285
/// absolute paths.
@@ -582,12 +557,18 @@ mod tests {
582557
"stable-x86_64-unknown-linux-gnu",
583558
"beta-x86_64-unknown-linux-gnu",
584559
"nightly-x86_64-unknown-linux-gnu",
560+
"nightly-2015-01-01-x86_64-unknown-linux-gnu",
585561
"1.0.0-x86_64-unknown-linux-gnu",
586562
"1.2.0-x86_64-unknown-linux-gnu",
563+
"1.8-beta-x86_64-apple-darwin",
564+
"1.8.0-beta-x86_64-apple-darwin",
565+
"1.8.0-beta.2-x86_64-apple-darwin",
566+
"1.8.0-x86_64-apple-darwin",
587567
"1.8.0-x86_64-unknown-linux-gnu",
588568
"1.10.0-x86_64-unknown-linux-gnu",
589569
"bar(baz)",
590570
"foo#bar",
571+
"the cake is a lie",
591572
"this.is.not-a+semver",
592573
]
593574
.into_iter()
@@ -598,20 +579,28 @@ mod tests {
598579
"1.8.0-x86_64-unknown-linux-gnu",
599580
"1.0.0-x86_64-unknown-linux-gnu",
600581
"nightly-x86_64-unknown-linux-gnu",
582+
"nightly-2015-01-01-x86_64-unknown-linux-gnu",
601583
"stable-x86_64-unknown-linux-gnu",
602584
"1.10.0-x86_64-unknown-linux-gnu",
603585
"beta-x86_64-unknown-linux-gnu",
604586
"1.2.0-x86_64-unknown-linux-gnu",
587+
// https://github.com/rust-lang/rustup/issues/1329
588+
"1.8.0-x86_64-apple-darwin",
589+
"1.8-beta-x86_64-apple-darwin",
590+
"1.8.0-beta-x86_64-apple-darwin",
591+
"1.8.0-beta.2-x86_64-apple-darwin",
605592
// https://github.com/rust-lang/rustup/issues/3517
606593
"foo#bar",
607594
"bar(baz)",
608595
"this.is.not-a+semver",
596+
// https://github.com/rust-lang/rustup/issues/3168
597+
"the cake is a lie",
609598
]
610599
.into_iter()
611600
.map(|s| ToolchainName::try_from(s).unwrap())
612601
.collect::<Vec<_>>();
613602

614-
super::toolchain_sort(&mut v);
603+
v.sort();
615604

616605
assert_eq!(expected, v);
617606
}

tests/suite/cli_rustup.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1218,12 +1218,12 @@ rustup home: {1}
12181218
12191219
installed toolchains
12201220
--------------------
1221-
nightly-2015-01-01-{0}
1222-
1.2.0 (hash-nightly-1)
1223-
12241221
nightly-{0} (active, default)
12251222
1.3.0 (hash-nightly-2)
12261223
1224+
nightly-2015-01-01-{0}
1225+
1.2.0 (hash-nightly-1)
1226+
12271227
active toolchain
12281228
----------------
12291229
name: nightly-{0}

0 commit comments

Comments
 (0)