Skip to content

Commit

Permalink
Cleanup before upstream submission
Browse files Browse the repository at this point in the history
  • Loading branch information
CAD97 committed Nov 6, 2024
1 parent c3addaa commit 7010eb5
Show file tree
Hide file tree
Showing 9 changed files with 573 additions and 142 deletions.
9 changes: 9 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ edition = "2021"
rust-version = "1.76"
publish = false

[dependencies]
unicode-ident = "1.0.13"

[features]

## Experimental API. This feature flag is **NOT** semver stable.
Expand Down
33 changes: 31 additions & 2 deletions src/allow_use.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,46 @@
use std::sync::OnceLock;
use std::{process::Command, sync::OnceLock};

fn rust_version_minor() -> u32 {
static VERSION_MINOR: OnceLock<u32> = OnceLock::new();
*VERSION_MINOR.get_or_init(|| {
crate::input::cargo_pkg_rust_version()
.split('.')
.nth(1)
.unwrap_or("70")
// assume build-rs's MSRV if none specified for the current package
.unwrap_or(env!("CARGO_PKG_RUST_VERSION").split('.').nth(1).unwrap())
.parse()
.unwrap()
})
}

fn cargo_version_minor() -> u32 {
static VERSION_MINOR: OnceLock<u32> = OnceLock::new();
*VERSION_MINOR.get_or_init(|| {
let out = Command::new(crate::input::cargo())
.arg("-V")
.output()
.expect("running `cargo -V` should succeed");
assert!(out.status.success(), "running `cargo -V` should succeed");

// > cargo -V # example output
// cargo 1.82.0 (8f40fc59f 2024-08-21)

String::from_utf8(out.stdout).expect("`cargo -V` should output valid UTF-8")
["cargo 1.".len()..]
.split('.')
.next()
.expect("`cargo -V` format should be stable")
.parse()
.unwrap()
})
}

pub(crate) fn double_colon_directives() -> bool {
// cargo errors on `cargo::` directives with insufficient package.rust-version
rust_version_minor() >= 77
}

pub(crate) fn check_cfg() -> bool {
// emit check-cfg if the toolchain being used supports it
cargo_version_minor() >= 80
}
27 changes: 27 additions & 0 deletions src/ident.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
use unicode_ident::{is_xid_continue, is_xid_start};

pub(crate) fn is_feature_name(s: &str) -> bool {
s.chars()
.all(|ch| is_xid_continue(ch) || matches!(ch, '-' | '+' | '.'))
}

pub(crate) fn is_ident(s: &str) -> bool {
let mut cs = s.chars();
cs.next()
.is_some_and(|ch| is_xid_start(ch) || matches!(ch, '_'))
&& cs.all(is_xid_continue)
}

pub(crate) fn is_ascii_ident(s: &str) -> bool {
let mut cs = s.chars();
cs.next()
.is_some_and(|ch| ch.is_ascii_alphabetic() || matches!(ch, '_'))
&& cs.all(|ch| ch.is_ascii_alphanumeric() || matches!(ch, '_'))
}

pub(crate) fn is_crate_name(s: &str) -> bool {
let mut cs = s.chars();
cs.next()
.is_some_and(|ch| is_xid_start(ch) || matches!(ch, '-' | '_'))
&& cs.all(|ch| is_xid_continue(ch) || matches!(ch, '-'))
}
Loading

0 comments on commit 7010eb5

Please sign in to comment.