Skip to content

Commit 4f60fe0

Browse files
CAD97epage
authored andcommitted
Cleanup before upstream submission
1 parent e243d71 commit 4f60fe0

File tree

9 files changed

+573
-142
lines changed

9 files changed

+573
-142
lines changed

Cargo.lock

+9
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

+3
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ edition = "2021"
55
rust-version = "1.76"
66
publish = false
77

8+
[dependencies]
9+
unicode-ident = "1.0.13"
10+
811
[features]
912

1013
## Experimental API. This feature flag is **NOT** semver stable.

src/allow_use.rs

+31-2
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,46 @@
1-
use std::sync::OnceLock;
1+
use std::{process::Command, sync::OnceLock};
22

33
fn rust_version_minor() -> u32 {
44
static VERSION_MINOR: OnceLock<u32> = OnceLock::new();
55
*VERSION_MINOR.get_or_init(|| {
66
crate::input::cargo_pkg_rust_version()
77
.split('.')
88
.nth(1)
9-
.unwrap_or("70")
9+
// 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()
13+
})
14+
}
15+
16+
fn cargo_version_minor() -> u32 {
17+
static VERSION_MINOR: OnceLock<u32> = OnceLock::new();
18+
*VERSION_MINOR.get_or_init(|| {
19+
let out = Command::new(crate::input::cargo())
20+
.arg("-V")
21+
.output()
22+
.expect("running `cargo -V` should succeed");
23+
assert!(out.status.success(), "running `cargo -V` should succeed");
24+
25+
// > cargo -V # example output
26+
// cargo 1.82.0 (8f40fc59f 2024-08-21)
27+
28+
String::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")
1033
.parse()
1134
.unwrap()
1235
})
1336
}
1437

1538
pub(crate) fn double_colon_directives() -> bool {
39+
// cargo errors on `cargo::` directives with insufficient package.rust-version
1640
rust_version_minor() >= 77
1741
}
42+
43+
pub(crate) fn check_cfg() -> bool {
44+
// emit check-cfg if the toolchain being used supports it
45+
cargo_version_minor() >= 80
46+
}

src/ident.rs

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
use unicode_ident::{is_xid_continue, is_xid_start};
2+
3+
pub(crate) fn is_feature_name(s: &str) -> bool {
4+
s.chars()
5+
.all(|ch| is_xid_continue(ch) || matches!(ch, '-' | '+' | '.'))
6+
}
7+
8+
pub(crate) fn is_ident(s: &str) -> bool {
9+
let mut cs = s.chars();
10+
cs.next()
11+
.is_some_and(|ch| is_xid_start(ch) || matches!(ch, '_'))
12+
&& cs.all(is_xid_continue)
13+
}
14+
15+
pub(crate) fn is_ascii_ident(s: &str) -> bool {
16+
let mut cs = s.chars();
17+
cs.next()
18+
.is_some_and(|ch| ch.is_ascii_alphabetic() || matches!(ch, '_'))
19+
&& cs.all(|ch| ch.is_ascii_alphanumeric() || matches!(ch, '_'))
20+
}
21+
22+
pub(crate) fn is_crate_name(s: &str) -> bool {
23+
let mut cs = s.chars();
24+
cs.next()
25+
.is_some_and(|ch| is_xid_start(ch) || matches!(ch, '-' | '_'))
26+
&& cs.all(|ch| is_xid_continue(ch) || matches!(ch, '-'))
27+
}

0 commit comments

Comments
 (0)