Skip to content

Commit 27c5772

Browse files
committed
refactor(build-rs): Extract minor version extraction
1 parent 347fa09 commit 27c5772

File tree

1 file changed

+11
-13
lines changed

1 file changed

+11
-13
lines changed

crates/build-rs/src/allow_use.rs

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,9 @@ use std::{process::Command, sync::OnceLock};
33
fn rust_version_minor() -> u32 {
44
static VERSION_MINOR: OnceLock<u32> = OnceLock::new();
55
*VERSION_MINOR.get_or_init(|| {
6-
crate::input::cargo_pkg_rust_version()
7-
.split('.')
8-
.nth(1)
6+
version_minor(&crate::input::cargo_pkg_rust_version())
97
// assume build-rs's MSRV if none specified for the current package
10-
.unwrap_or(env!("CARGO_PKG_RUST_VERSION").split('.').nth(1).unwrap())
11-
.parse()
12-
.unwrap()
8+
.unwrap_or_else(|| version_minor(env!("CARGO_PKG_RUST_VERSION")).unwrap())
139
})
1410
}
1511

@@ -25,16 +21,18 @@ fn cargo_version_minor() -> u32 {
2521
// > cargo -V # example output
2622
// cargo 1.82.0 (8f40fc59f 2024-08-21)
2723

28-
std::str::from_utf8(&out.stdout).expect("`cargo -V` should output valid UTF-8")
29-
["cargo 1.".len()..]
30-
.split('.')
31-
.next()
32-
.expect("`cargo -V` format should be stable")
33-
.parse()
34-
.unwrap()
24+
let out = std::str::from_utf8(&out.stdout).expect("`cargo -V` should output valid UTF-8");
25+
let version = out.split(' ').nth(1).unwrap();
26+
version_minor(version).unwrap()
3527
})
3628
}
3729

30+
fn version_minor(version: &str) -> Option<u32> {
31+
let minor = version.split('.').nth(1)?;
32+
let minor = minor.parse().unwrap();
33+
Some(minor)
34+
}
35+
3836
pub(crate) fn double_colon_directives() -> bool {
3937
// cargo errors on `cargo::` directives with insufficient package.rust-version
4038
rust_version_minor() >= 77

0 commit comments

Comments
 (0)