1use purrdf_toml::{
7 Bounds, Document, Float, Kind, Node, Profile, Scalar, SourceDocument, TomlError, Vocabulary,
8};
9
10pub(crate) fn read(text: &str) -> Result<Document<'_>, TomlError> {
11 let profile = Profile::new(
12 "raf-cargo-toml",
13 1,
14 Vocabulary::standard()?,
15 Bounds {
16 max_source_bytes: text.len().max(1) as u64,
17 max_depth: 128,
18 ..Bounds::standard()
19 },
20 )?;
21 let document = purrdf_toml::analyze(
22 SourceDocument {
23 id: "urn:raf:cargo-toml",
24 bytes: text.as_bytes(),
25 },
26 &profile,
27 )?;
28 for node in document.nodes() {
29 if matches!(
30 node.kind,
31 Kind::Scalar(Scalar::Float(Float::Infinity { .. }))
32 ) && !["inf", "+inf", "-inf"].contains(&&text[node.span.clone()])
33 {
34 return Err(TomlError::Syntax {
35 location: purrdf_toml::Location {
36 byte: node.span.start,
37 line: text[..node.span.start]
38 .bytes()
39 .filter(|byte| *byte == b'\n')
40 .count()
41 + 1,
42 column: text[..node.span.start]
43 .rsplit('\n')
44 .next()
45 .unwrap_or_default()
46 .chars()
47 .count()
48 + 1,
49 },
50 expected: "Cargo floating-point number must not overflow; explicit inf is required",
51 });
52 }
53 }
54 Ok(document)
55}
56
57pub(crate) fn field<'a>(document: &'a Document<'_>, table: &Node, key: &str) -> Option<&'a Node> {
58 table
59 .children
60 .iter()
61 .map(|id| &document.nodes()[*id])
62 .find(|node| node.key.as_ref().is_some_and(|name| name.decoded == key))
63}
64
65pub(crate) fn string(node: &Node) -> Option<&str> {
66 match &node.kind {
67 Kind::Scalar(Scalar::String(text)) => Some(text),
68 _ => None,
69 }
70}
71
72pub(crate) fn boolean(node: &Node) -> Option<bool> {
73 match node.kind {
74 Kind::Scalar(Scalar::Boolean(value)) => Some(value),
75 _ => None,
76 }
77}
78
79pub(crate) fn table(node: &Node) -> Option<&Node> {
80 matches!(node.kind, Kind::Table(_)).then_some(node)
81}
82
83pub(crate) fn array(node: &Node) -> Option<&[usize]> {
84 matches!(node.kind, Kind::Array | Kind::ArrayOfTables).then_some(node.children.as_slice())
85}
86
87pub(crate) fn entries<'a>(document: &'a Document<'_>, table: &Node) -> Vec<(&'a str, &'a Node)> {
88 let mut entries: Vec<_> = table
89 .children
90 .iter()
91 .map(|id| {
92 let node = &document.nodes()[*id];
93 (
94 node.key
95 .as_ref()
96 .expect("native table member key")
97 .decoded
98 .as_str(),
99 node,
100 )
101 })
102 .collect();
103 purrdf_lex::walk::try_sort_unstable_by(&mut entries, |left, right| {
106 Ok::<_, std::convert::Infallible>(left.0.cmp(right.0))
107 })
108 .expect("infallible key comparison");
109 entries
110}
111
112#[derive(Debug)]
115pub struct CargoLintManifest<'a>(Document<'a>);
116
117impl<'a> CargoLintManifest<'a> {
118 pub fn parse(text: &'a str) -> Result<Self, TomlError> {
123 read(text).map(Self)
124 }
125
126 fn lookup(&self, keys: &[&str]) -> Option<&Node> {
127 keys.iter()
128 .try_fold(&self.0.nodes()[0], |at, key| field(&self.0, at, key))
129 }
130
131 #[must_use]
133 pub fn inherits(&self) -> bool {
134 self.lookup(&["lints", "workspace"]).and_then(boolean) == Some(true)
135 }
136
137 #[must_use]
141 pub fn production_config_override(&self) -> Option<&str> {
142 for (name, keys) in [
143 ("include", &["include"][..]),
144 ("alias.clippy", &["alias", "clippy"]),
145 ("build.rustflags", &["build", "rustflags"]),
146 ("build.target", &["build", "target"]),
147 ("build.rustc", &["build", "rustc"]),
148 ("build.rustc-wrapper", &["build", "rustc-wrapper"]),
149 (
150 "build.rustc-workspace-wrapper",
151 &["build", "rustc-workspace-wrapper"],
152 ),
153 ] {
154 if self.lookup(keys).is_some() {
155 return Some(name);
156 }
157 }
158 if let Some(targets) = self.lookup(&["target"]) {
159 for (_, target) in entries(&self.0, targets) {
160 if field(&self.0, target, "rustflags").is_some() {
161 return Some("target rustflags");
162 }
163 }
164 }
165 self.lookup(&["env"]).and_then(|env| {
166 entries(&self.0, env)
167 .into_iter()
168 .map(|(key, _)| key)
169 .find(|key| {
170 matches!(
171 *key,
172 "RUSTFLAGS"
173 | "RUSTC"
174 | "RUSTC_WRAPPER"
175 | "RUSTC_WORKSPACE_WRAPPER"
176 | "CARGO_ENCODED_RUSTFLAGS"
177 | "CARGO_BUILD_RUSTFLAGS"
178 | "CARGO_BUILD_TARGET"
179 | "CLIPPY_ARGS"
180 | "CLIPPY_CONF_DIR"
181 ) || (key.starts_with("CARGO_TARGET_") && key.ends_with("_RUSTFLAGS"))
182 })
183 })
184 }
185
186 #[must_use]
190 pub fn setting(&self, workspace: bool, tool: &str, name: &str) -> Option<(&str, i64)> {
191 let entry = if workspace {
192 self.lookup(&["workspace", "lints", tool, name])
193 } else {
194 self.lookup(&["lints", tool, name])
195 }?;
196 if let Some(level) = string(entry) {
197 return Some((level, 0));
198 }
199 let level = string(field(&self.0, entry, "level")?)?;
200 let priority = match field(&self.0, entry, "priority").map(|node| &node.kind) {
201 None => 0,
202 Some(Kind::Scalar(Scalar::Integer(value))) => *value,
203 _ => return None,
204 };
205 Some((level, priority))
206 }
207}