Skip to content

Commit 120bea7

Browse files
committed
Handle nightly
1 parent 9ec86a4 commit 120bea7

File tree

2 files changed

+93
-41
lines changed

2 files changed

+93
-41
lines changed

crates/cargo-platform/src/cfg.rs

Lines changed: 33 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -20,18 +20,44 @@ pub enum CfgExpr {
2020
#[derive(Eq, PartialEq, Hash, Ord, PartialOrd, Clone, Debug)]
2121
pub struct CfgRustVersion {
2222
pub minor: u64,
23-
pub patch: u64,
23+
pub patch: Option<u64>,
2424
}
2525

2626
impl CfgRustVersion {
27-
fn parse(version: &str) -> Option<Self> {
27+
pub fn parse(version: &str) -> Option<Self> {
2828
let minor_patch = version.strip_prefix("1.")?;
2929
let (minor, patch) = match minor_patch.split_once('.') {
30-
Some((minor, patch)) => (minor.parse().ok()?, patch.parse().ok()?),
31-
None => (minor_patch.parse().ok()?, 0),
30+
Some((minor, patch)) => (minor.parse().ok()?, Some(patch.parse().ok()?)),
31+
None => (minor_patch.parse().ok()?, None),
3232
};
3333
Some(Self { minor, patch })
3434
}
35+
36+
pub fn matches(&self, rustc_version: &semver::Version) -> bool {
37+
match self.minor.cmp(&rustc_version.minor) {
38+
Ordering::Less => true,
39+
Ordering::Equal => match self.patch {
40+
Some(patch) => {
41+
if rustc_version.pre.as_str() == "nightly" {
42+
false
43+
} else {
44+
patch <= rustc_version.patch
45+
}
46+
}
47+
None => true,
48+
},
49+
Ordering::Greater => false,
50+
}
51+
}
52+
}
53+
54+
impl fmt::Display for CfgRustVersion {
55+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
56+
match self.patch {
57+
Some(patch) => write!(f, "version(\"1.{}.{patch}\")", self.minor),
58+
None => write!(f, "version(\"1.{}\")", self.minor),
59+
}
60+
}
3561
}
3662

3763
/// A cfg value.
@@ -145,9 +171,7 @@ impl fmt::Display for Cfg {
145171
match *self {
146172
Cfg::Name(ref s) => s.fmt(f),
147173
Cfg::KeyPair(ref k, ref v) => write!(f, "{} = \"{}\"", k, v),
148-
Cfg::Version(CfgRustVersion { minor, patch }) => {
149-
write!(f, "version(\"1.{minor}.{patch}\")")
150-
}
174+
Cfg::Version(ref cfg_rust_version) => cfg_rust_version.fmt(f),
151175
}
152176
}
153177
}
@@ -172,12 +196,8 @@ impl CfgExpr {
172196
CfgExpr::Not(ref e) => !e.matches(cfg, rustc_version),
173197
CfgExpr::All(ref e) => e.iter().all(|e| e.matches(cfg, rustc_version)),
174198
CfgExpr::Any(ref e) => e.iter().any(|e| e.matches(cfg, rustc_version)),
175-
CfgExpr::Value(Cfg::Version(CfgRustVersion { minor, patch })) => {
176-
match minor.cmp(&rustc_version.minor) {
177-
Ordering::Less => true,
178-
Ordering::Equal => patch <= rustc_version.patch,
179-
Ordering::Greater => false,
180-
}
199+
CfgExpr::Value(Cfg::Version(ref cfg_rust_version)) => {
200+
cfg_rust_version.matches(rustc_version)
181201
}
182202
CfgExpr::Value(ref e) => cfg.contains(e),
183203
CfgExpr::True => true,

crates/cargo-platform/tests/test_cfg.rs

Lines changed: 60 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use cargo_platform::{Cfg, CfgExpr, CfgRustVersion, Ident, Platform};
2+
use semver::{BuildMetadata, Prerelease};
23
use std::fmt;
34
use std::str::FromStr;
45

@@ -33,10 +34,16 @@ macro_rules! c {
3334
$e.to_string(),
3435
)
3536
};
37+
(version($minor:literal)) => {
38+
Cfg::Version(CfgRustVersion {
39+
minor: $minor,
40+
patch: None,
41+
})
42+
};
3643
(version($minor:literal, $patch:literal)) => {
3744
Cfg::Version(CfgRustVersion {
3845
minor: $minor,
39-
patch: $patch,
46+
patch: Some($patch),
4047
})
4148
};
4249
}
@@ -51,6 +58,7 @@ macro_rules! e {
5158
($($t:tt)*) => (CfgExpr::Value(c!($($t)*)));
5259
}
5360

61+
#[track_caller]
5462
fn good<T>(s: &str, expected: T)
5563
where
5664
T: FromStr + PartialEq + fmt::Debug,
@@ -63,6 +71,7 @@ where
6371
assert_eq!(c, expected);
6472
}
6573

74+
#[track_caller]
6675
fn bad<T>(s: &str, err: &str)
6776
where
6877
T: FromStr + fmt::Display,
@@ -96,6 +105,7 @@ fn cfg_syntax() {
96105
good("foo = \"3 e\"", c!(foo = "3 e"));
97106
good(" r#foo = \"3 e\"", c!(r # foo = "3 e"));
98107
good("version(\"1.23.4\")", c!(version(23, 4)));
108+
good("version(\"1.23\")", c!(version(23)));
99109
good("version(\"1.234.56\")", c!(version(234, 56)));
100110
good(" version(\"1.23.4\")", c!(version(23, 4)));
101111
good("version(\"1.23.4\") ", c!(version(23, 4)));
@@ -172,34 +182,56 @@ fn cfg_expr_bad() {
172182

173183
#[test]
174184
fn cfg_matches() {
175-
let rustc_version = semver::Version::new(0, 0, 0);
176-
assert!(e!(foo).matches(&[c!(bar), c!(foo), c!(baz)], &rustc_version));
177-
assert!(e!(any(foo)).matches(&[c!(bar), c!(foo), c!(baz)], &rustc_version));
178-
assert!(e!(any(foo, bar)).matches(&[c!(bar)], &rustc_version));
179-
assert!(e!(any(foo, bar)).matches(&[c!(foo)], &rustc_version));
180-
assert!(e!(all(foo, bar)).matches(&[c!(foo), c!(bar)], &rustc_version));
181-
assert!(e!(all(foo, bar)).matches(&[c!(foo), c!(bar)], &rustc_version));
182-
assert!(e!(not(foo)).matches(&[c!(bar)], &rustc_version));
183-
assert!(e!(not(foo)).matches(&[], &rustc_version));
184-
assert!(e!(any((not(foo)), (all(foo, bar)))).matches(&[c!(bar)], &rustc_version));
185-
assert!(e!(any((not(foo)), (all(foo, bar)))).matches(&[c!(foo), c!(bar)], &rustc_version));
186-
assert!(e!(foo).matches(&[c!(r # foo)], &rustc_version));
187-
assert!(e!(r # foo).matches(&[c!(foo)], &rustc_version));
188-
assert!(e!(r # foo).matches(&[c!(r # foo)], &rustc_version));
185+
let v87 = semver::Version::new(1, 87, 0);
186+
assert!(e!(foo).matches(&[c!(bar), c!(foo), c!(baz)], &v87));
187+
assert!(e!(any(foo)).matches(&[c!(bar), c!(foo), c!(baz)], &v87));
188+
assert!(e!(any(foo, bar)).matches(&[c!(bar)], &v87));
189+
assert!(e!(any(foo, bar)).matches(&[c!(foo)], &v87));
190+
assert!(e!(all(foo, bar)).matches(&[c!(foo), c!(bar)], &v87));
191+
assert!(e!(all(foo, bar)).matches(&[c!(foo), c!(bar)], &v87));
192+
assert!(e!(not(foo)).matches(&[c!(bar)], &v87));
193+
assert!(e!(not(foo)).matches(&[], &v87));
194+
assert!(e!(any((not(foo)), (all(foo, bar)))).matches(&[c!(bar)], &v87));
195+
assert!(e!(any((not(foo)), (all(foo, bar)))).matches(&[c!(foo), c!(bar)], &v87));
196+
assert!(e!(foo).matches(&[c!(r # foo)], &v87));
197+
assert!(e!(r # foo).matches(&[c!(foo)], &v87));
198+
assert!(e!(r # foo).matches(&[c!(r # foo)], &v87));
189199

190-
assert!(!e!(foo).matches(&[], &rustc_version));
191-
assert!(!e!(foo).matches(&[c!(bar)], &rustc_version));
192-
assert!(!e!(foo).matches(&[c!(fo)], &rustc_version));
193-
assert!(!e!(any(foo)).matches(&[], &rustc_version));
194-
assert!(!e!(any(foo)).matches(&[c!(bar)], &rustc_version));
195-
assert!(!e!(any(foo)).matches(&[c!(bar), c!(baz)], &rustc_version));
196-
assert!(!e!(all(foo)).matches(&[c!(bar), c!(baz)], &rustc_version));
197-
assert!(!e!(all(foo, bar)).matches(&[c!(bar)], &rustc_version));
198-
assert!(!e!(all(foo, bar)).matches(&[c!(foo)], &rustc_version));
199-
assert!(!e!(all(foo, bar)).matches(&[], &rustc_version));
200-
assert!(!e!(not(bar)).matches(&[c!(bar)], &rustc_version));
201-
assert!(!e!(not(bar)).matches(&[c!(baz), c!(bar)], &rustc_version));
202-
assert!(!e!(any((not(foo)), (all(foo, bar)))).matches(&[c!(foo)], &rustc_version));
200+
assert!(!e!(foo).matches(&[], &v87));
201+
assert!(!e!(foo).matches(&[c!(bar)], &v87));
202+
assert!(!e!(foo).matches(&[c!(fo)], &v87));
203+
assert!(!e!(any(foo)).matches(&[], &v87));
204+
assert!(!e!(any(foo)).matches(&[c!(bar)], &v87));
205+
assert!(!e!(any(foo)).matches(&[c!(bar), c!(baz)], &v87));
206+
assert!(!e!(all(foo)).matches(&[c!(bar), c!(baz)], &v87));
207+
assert!(!e!(all(foo, bar)).matches(&[c!(bar)], &v87));
208+
assert!(!e!(all(foo, bar)).matches(&[c!(foo)], &v87));
209+
assert!(!e!(all(foo, bar)).matches(&[], &v87));
210+
assert!(!e!(not(bar)).matches(&[c!(bar)], &v87));
211+
assert!(!e!(not(bar)).matches(&[c!(baz), c!(bar)], &v87));
212+
assert!(!e!(any((not(foo)), (all(foo, bar)))).matches(&[c!(foo)], &v87));
213+
214+
assert!(e!(version(87)).matches(&[], &v87));
215+
assert!(e!(version(87, 0)).matches(&[], &v87));
216+
assert!(e!(version(86)).matches(&[], &v87));
217+
assert!(e!(version(86, 1)).matches(&[], &v87));
218+
assert!(!e!(version(87, 1)).matches(&[], &v87));
219+
assert!(!e!(version(88)).matches(&[], &v87));
220+
assert!(!e!(version(88, 1)).matches(&[], &v87));
221+
assert!(e!(not(version(88))).matches(&[], &v87));
222+
assert!(e!(not(version(88, 1))).matches(&[], &v87));
223+
224+
let v89_nightly = semver::Version {
225+
major: 1,
226+
minor: 89,
227+
patch: 0,
228+
pre: Prerelease::new("nightly").unwrap(),
229+
build: BuildMetadata::EMPTY,
230+
};
231+
assert!(e!(version(89)).matches(&[], &v89_nightly));
232+
assert!(!e!(version(89, 0)).matches(&[], &v89_nightly));
233+
assert!(e!(version(88)).matches(&[], &v89_nightly));
234+
assert!(e!(version(88, 0)).matches(&[], &v89_nightly));
203235
}
204236

205237
#[test]

0 commit comments

Comments
 (0)