1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
use std::fmt;
use std::str::FromStr;
mod cfg;
mod error;
pub use cfg::{Cfg, CfgExpr};
pub use error::{ParseError, ParseErrorKind};
#[derive(Eq, PartialEq, Hash, Ord, PartialOrd, Clone, Debug)]
pub enum Platform {
Name(String),
Cfg(CfgExpr),
}
impl Platform {
pub fn matches(&self, name: &str, cfg: &[Cfg]) -> bool {
match *self {
Platform::Name(ref p) => p == name,
Platform::Cfg(ref p) => p.matches(cfg),
}
}
fn validate_named_platform(name: &str) -> Result<(), ParseError> {
if let Some(ch) = name
.chars()
.find(|&c| !(c.is_alphanumeric() || c == '_' || c == '-' || c == '.'))
{
if name.chars().any(|c| c == '(') {
return Err(ParseError::new(
name,
ParseErrorKind::InvalidTarget(
"unexpected `(` character, cfg expressions must start with `cfg(`"
.to_string(),
),
));
}
return Err(ParseError::new(
name,
ParseErrorKind::InvalidTarget(format!(
"unexpected character {} in target name",
ch
)),
));
}
Ok(())
}
}
impl serde::Serialize for Platform {
fn serialize<S>(&self, s: S) -> Result<S::Ok, S::Error>
where
S: serde::Serializer,
{
self.to_string().serialize(s)
}
}
impl<'de> serde::Deserialize<'de> for Platform {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: serde::Deserializer<'de>,
{
let s = String::deserialize(deserializer)?;
FromStr::from_str(&s).map_err(serde::de::Error::custom)
}
}
impl FromStr for Platform {
type Err = ParseError;
fn from_str(s: &str) -> Result<Platform, ParseError> {
if s.starts_with("cfg(") && s.ends_with(')') {
let s = &s[4..s.len() - 1];
s.parse().map(Platform::Cfg)
} else {
Platform::validate_named_platform(s)?;
Ok(Platform::Name(s.to_string()))
}
}
}
impl fmt::Display for Platform {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match *self {
Platform::Name(ref n) => n.fmt(f),
Platform::Cfg(ref e) => write!(f, "cfg({})", e),
}
}
}