cargo_platform/
lib.rs
1use std::str::FromStr;
15use std::{fmt, path::Path};
16
17mod cfg;
18mod error;
19
20use cfg::KEYWORDS;
21pub use cfg::{Cfg, CfgExpr, Ident};
22pub use error::{ParseError, ParseErrorKind};
23
24#[derive(Eq, PartialEq, Hash, Ord, PartialOrd, Clone, Debug)]
26pub enum Platform {
27 Name(String),
29 Cfg(CfgExpr),
31}
32
33impl Platform {
34 pub fn matches(&self, name: &str, cfg: &[Cfg]) -> bool {
38 match *self {
39 Platform::Name(ref p) => p == name,
40 Platform::Cfg(ref p) => p.matches(cfg),
41 }
42 }
43
44 fn validate_named_platform(name: &str) -> Result<(), ParseError> {
45 if let Some(ch) = name
46 .chars()
47 .find(|&c| !(c.is_alphanumeric() || c == '_' || c == '-' || c == '.'))
48 {
49 if name.chars().any(|c| c == '(') {
50 return Err(ParseError::new(
51 name,
52 ParseErrorKind::InvalidTarget(
53 "unexpected `(` character, cfg expressions must start with `cfg(`"
54 .to_string(),
55 ),
56 ));
57 }
58 return Err(ParseError::new(
59 name,
60 ParseErrorKind::InvalidTarget(format!(
61 "unexpected character {} in target name",
62 ch
63 )),
64 ));
65 }
66 Ok(())
67 }
68
69 pub fn check_cfg_attributes(&self, warnings: &mut Vec<String>) {
70 fn check_cfg_expr(expr: &CfgExpr, warnings: &mut Vec<String>) {
71 match *expr {
72 CfgExpr::Not(ref e) => check_cfg_expr(e, warnings),
73 CfgExpr::All(ref e) | CfgExpr::Any(ref e) => {
74 for e in e {
75 check_cfg_expr(e, warnings);
76 }
77 }
78 CfgExpr::Value(ref e) => match e {
79 Cfg::Name(name) => match name.as_str() {
80 "test" | "debug_assertions" | "proc_macro" =>
81 warnings.push(format!(
82 "Found `{}` in `target.'cfg(...)'.dependencies`. \
83 This value is not supported for selecting dependencies \
84 and will not work as expected. \
85 To learn more visit \
86 https://doc.rust-lang.org/cargo/reference/specifying-dependencies.html#platform-specific-dependencies",
87 name
88 )),
89 _ => (),
90 },
91 Cfg::KeyPair(name, _) => if name.as_str() == "feature" {
92 warnings.push(String::from(
93 "Found `feature = ...` in `target.'cfg(...)'.dependencies`. \
94 This key is not supported for selecting dependencies \
95 and will not work as expected. \
96 Use the [features] section instead: \
97 https://doc.rust-lang.org/cargo/reference/features.html"
98 ))
99 },
100 }
101 }
102 }
103
104 if let Platform::Cfg(cfg) = self {
105 check_cfg_expr(cfg, warnings);
106 }
107 }
108
109 pub fn check_cfg_keywords(&self, warnings: &mut Vec<String>, path: &Path) {
110 fn check_cfg_expr(expr: &CfgExpr, warnings: &mut Vec<String>, path: &Path) {
111 match *expr {
112 CfgExpr::Not(ref e) => check_cfg_expr(e, warnings, path),
113 CfgExpr::All(ref e) | CfgExpr::Any(ref e) => {
114 for e in e {
115 check_cfg_expr(e, warnings, path);
116 }
117 }
118 CfgExpr::Value(ref e) => match e {
119 Cfg::Name(name) | Cfg::KeyPair(name, _) => {
120 if !name.raw && KEYWORDS.contains(&name.as_str()) {
121 if name.as_str() == "true" || name.as_str() == "false" {
122 warnings.push(format!(
123 "[{}] future-incompatibility: the meaning of `cfg({e})` will change in the future\n \
124 | Cargo is erroneously allowing `cfg(true)` and `cfg(false)`, but both forms are interpreted as false unless manually overridden with `--cfg`.\n \
125 | In the future these will be built-in defines that will have the corresponding true/false value.\n \
126 | It is recommended to avoid using these configs until they are properly supported.\n \
127 | See <https://github.com/rust-lang/rust/issues/131204> for more information.\n \
128 |\n \
129 | help: use raw-idents instead: `cfg(r#{name})`",
130 path.display()
131 ));
132 } else {
133 warnings.push(format!(
134 "[{}] future-incompatibility: `cfg({e})` is deprecated as `{name}` is a keyword \
135 and not an identifier and should not have have been accepted in this position.\n \
136 | this was previously accepted by Cargo but is being phased out; it will become a hard error in a future release!\n \
137 |\n \
138 | help: use raw-idents instead: `cfg(r#{name})`",
139 path.display()
140 ));
141 }
142 }
143 }
144 },
145 }
146 }
147
148 if let Platform::Cfg(cfg) = self {
149 check_cfg_expr(cfg, warnings, path);
150 }
151 }
152}
153
154impl serde::Serialize for Platform {
155 fn serialize<S>(&self, s: S) -> Result<S::Ok, S::Error>
156 where
157 S: serde::Serializer,
158 {
159 self.to_string().serialize(s)
160 }
161}
162
163impl<'de> serde::Deserialize<'de> for Platform {
164 fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
165 where
166 D: serde::Deserializer<'de>,
167 {
168 let s = String::deserialize(deserializer)?;
169 FromStr::from_str(&s).map_err(serde::de::Error::custom)
170 }
171}
172
173impl FromStr for Platform {
174 type Err = ParseError;
175
176 fn from_str(s: &str) -> Result<Platform, ParseError> {
177 if let Some(s) = s.strip_prefix("cfg(").and_then(|s| s.strip_suffix(')')) {
178 s.parse().map(Platform::Cfg)
179 } else {
180 Platform::validate_named_platform(s)?;
181 Ok(Platform::Name(s.to_string()))
182 }
183 }
184}
185
186impl fmt::Display for Platform {
187 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
188 match *self {
189 Platform::Name(ref n) => n.fmt(f),
190 Platform::Cfg(ref e) => write!(f, "cfg({})", e),
191 }
192 }
193}