Skip to content

Commit f0d8f2a

Browse files
committed
General Rust fixes and updates
Update to Rust edition 2024, update dependency versions, and run Clippy to apply some suggested improvements.
1 parent a9b335b commit f0d8f2a

12 files changed

+97
-84
lines changed

Cargo.lock

Lines changed: 29 additions & 15 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
name = "html-build"
33
version = "0.0.0"
44
publish = false
5-
edition = "2021"
5+
edition = "2024"
66

77
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
88

@@ -12,7 +12,7 @@ html5ever = "0.26.0"
1212
markup5ever_rcdom = "0.2.0"
1313
regex = "1"
1414
delegate = "0.12.0"
15-
url = "2.2.2"
15+
url = "2.5.4"
1616

1717
[dev-dependencies]
1818
tempfile = "3"

src/annotate_attributes.rs

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use std::io;
55
use std::rc::Rc;
66

77
use html5ever::tendril::StrTendril;
8-
use html5ever::{local_name, namespace_url, ns, LocalName, QualName};
8+
use html5ever::{LocalName, QualName, local_name, namespace_url, ns};
99
use markup5ever_rcdom::{Handle, NodeData};
1010

1111
use crate::dom_utils::{self, NodeHandleExt};
@@ -142,7 +142,7 @@ impl Processor {
142142
let mut variant_comment = None;
143143
let mut variant_str = None;
144144
for node in description.iter() {
145-
if let NodeData::Comment { ref contents } = node.data {
145+
if let NodeData::Comment { contents } = &node.data {
146146
if contents.trim().starts_with("or:") {
147147
variant_comment = Some(node);
148148
variant_str = Some(StrTendril::from(contents.trim()[3..].trim_start()));
@@ -158,7 +158,7 @@ impl Processor {
158158
.children
159159
.borrow()
160160
.iter()
161-
.filter(|c| variant_comment.map_or(true, |vc| !Rc::ptr_eq(c, vc)))
161+
.filter(|c| variant_comment.is_none_or(|vc| !Rc::ptr_eq(c, vc)))
162162
.map(|c| c.deep_clone())
163163
.collect(),
164164
variant: variant_str,
@@ -167,7 +167,7 @@ impl Processor {
167167
if existing.default.is_empty() {
168168
existing.default = descriptions.default;
169169
} else if !descriptions.default.is_empty() {
170-
if let NodeData::Text { ref contents } = existing.default.last().unwrap().data {
170+
if let NodeData::Text { contents } = &existing.default.last().unwrap().data {
171171
let mut borrow = contents.borrow_mut();
172172
if let Some(last_non_ws) = borrow.rfind(|c: char| !c.is_ascii_whitespace())
173173
{
@@ -209,13 +209,13 @@ impl Processor {
209209
let mut has_special_semantics = false;
210210
let mut key = None;
211211
dom_utils::scan_dom(dd, &mut |n| match &n.data {
212-
NodeData::Comment { ref contents } if contents.trim() == "no-annotate" => {
212+
NodeData::Comment { contents } if contents.trim() == "no-annotate" => {
213213
can_annotate = false;
214214
}
215-
NodeData::Comment { ref contents } if contents.trim() == "variant" => {
215+
NodeData::Comment { contents } if contents.trim() == "variant" => {
216216
wants_variant_description = true;
217217
}
218-
NodeData::Text { ref contents }
218+
NodeData::Text { contents }
219219
if contents.borrow().contains("has special semantics") =>
220220
{
221221
has_special_semantics = true;
@@ -257,7 +257,7 @@ impl Processor {
257257
};
258258
let mut description: Vec<Handle> = match descriptions {
259259
Descriptions {
260-
variant: Some(ref variant),
260+
variant: Some(variant),
261261
..
262262
} if wants_variant_description => {
263263
parser::parse_fragment_async(variant[..].as_bytes(), &dd).await?
@@ -268,22 +268,22 @@ impl Processor {
268268
format!(
269269
"Attribute {key} wants variant description, but no <!--or--> was found"
270270
),
271-
))
272-
}
273-
Descriptions { ref default, .. } => {
274-
default.iter().map(|n| n.deep_clone()).collect()
271+
));
275272
}
273+
Descriptions { default, .. } => default.iter().map(|n| n.deep_clone()).collect(),
276274
};
277275

278276
let mut dd_children = dd.children.borrow_mut();
279277
if has_special_semantics {
280278
// Replace the trailing period with a separating colon.
281-
if let Some(NodeData::Text { contents }) = dd_children.last_mut().map(|n| &n.data) {
282-
let mut text = contents.borrow_mut();
283-
*text = StrTendril::from(
284-
text.trim_end_matches(|c: char| c.is_ascii_whitespace() || c == '.'),
285-
);
286-
text.push_slice(": ");
279+
if let Some(last) = dd_children.last_mut() {
280+
if let NodeData::Text { contents } = &last.data {
281+
let mut text = contents.borrow_mut();
282+
*text = StrTendril::from(
283+
text.trim_end_matches(|c: char| c.is_ascii_whitespace() || c == '.'),
284+
);
285+
text.push_slice(": ");
286+
}
287287
}
288288
} else {
289289
// Insert an em dash.

src/boilerplate.rs

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use std::io;
77
use std::path::{Path, PathBuf};
88

99
use html5ever::tendril::{self, SendTendril};
10-
use html5ever::{local_name, Attribute, LocalName, QualName};
10+
use html5ever::{Attribute, LocalName, QualName, local_name};
1111
use markup5ever_rcdom::{Handle, NodeData};
1212
use tokio::fs::File;
1313
use tokio::task::JoinHandle;
@@ -48,11 +48,11 @@ impl Processor {
4848
/// Identifies replacements which will be needed, and starts the necessary
4949
/// I/O.
5050
pub fn visit(&mut self, node: &Handle) {
51-
match node.data {
51+
match &node.data {
5252
// BOILERPLATE comments will need to be replaced with their
5353
// corresponding HTML, parsed. Open the file so that we can do so on
5454
// demand.
55-
NodeData::Comment { ref contents } if contents.starts_with("BOILERPLATE ") => {
55+
NodeData::Comment { contents } if contents.starts_with("BOILERPLATE ") => {
5656
let path = Path::new(contents[12..].trim());
5757
let file = if is_safe_path(path) {
5858
tokio::spawn(File::open(self.path.join(path)))
@@ -67,12 +67,8 @@ impl Processor {
6767
// Pseudo-comments can also appear in element attributes. These are
6868
// not parsed as HTML, so we simply want to read them into memory so
6969
// they can be replaced.
70-
NodeData::Element { ref attrs, .. } => {
71-
for Attribute {
72-
ref name,
73-
ref value,
74-
} in attrs.borrow().iter()
75-
{
70+
NodeData::Element { attrs, .. } => {
71+
for Attribute { name, value } in attrs.borrow().iter() {
7672
if value.starts_with("<!--BOILERPLATE ") && value.ends_with("-->") {
7773
let path = Path::new(value[16..value.len() - 3].trim());
7874
let file_contents = if is_safe_path(path) {
@@ -94,18 +90,18 @@ impl Processor {
9490
// <pre> and <pre><code> which contain EXAMPLE also need to be
9591
// replaced, but as plain text. These are loaded from the "examples"
9692
// directory instead.
97-
NodeData::Text { ref contents } => {
93+
NodeData::Text { contents } => {
9894
let borrowed_contents = contents.borrow();
9995
let text = borrowed_contents.trim();
10096
if !text.starts_with("EXAMPLE ") {
10197
return;
10298
}
10399
const PRE: LocalName = local_name!("pre");
104100
const CODE: LocalName = local_name!("code");
105-
let has_suitable_parent = node.parent_node().map_or(false, |p| {
101+
let has_suitable_parent = node.parent_node().is_some_and(|p| {
106102
p.is_html_element(&PRE)
107103
|| (p.is_html_element(&CODE)
108-
&& p.parent_node().map_or(false, |p2| p2.is_html_element(&PRE)))
104+
&& p.parent_node().is_some_and(|p2| p2.is_html_element(&PRE)))
109105
});
110106
if has_suitable_parent {
111107
let path = Path::new(text[8..].trim());
@@ -179,7 +175,8 @@ mod tests {
179175
proc.apply().await?;
180176
assert_eq!(
181177
serialize_for_test(&[document]),
182-
"<!DOCTYPE html><html><head></head><body><table><tbody><tr><td>en</td><td>English</td></tr></tbody></table></body></html>");
178+
"<!DOCTYPE html><html><head></head><body><table><tbody><tr><td>en</td><td>English</td></tr></tbody></table></body></html>"
179+
);
183180
Ok(())
184181
}
185182

@@ -200,7 +197,8 @@ mod tests {
200197
proc.apply().await?;
201198
assert_eq!(
202199
serialize_for_test(&[document]),
203-
"<!DOCTYPE html><html><head></head><body><a href=\"data:text/html,Hello, world!\">hello</a></body></html>");
200+
"<!DOCTYPE html><html><head></head><body><a href=\"data:text/html,Hello, world!\">hello</a></body></html>"
201+
);
204202
Ok(())
205203
}
206204

@@ -218,7 +216,8 @@ mod tests {
218216
proc.apply().await?;
219217
assert_eq!(
220218
serialize_for_test(&[document]),
221-
"<!DOCTYPE html><html><head></head><body><pre>first</pre><pre><code class=\"html\">second</code></pre><p>EXAMPLE ignored</p></body></html>" );
219+
"<!DOCTYPE html><html><head></head><body><pre>first</pre><pre><code class=\"html\">second</code></pre><p>EXAMPLE ignored</p></body></html>"
220+
);
222221
Ok(())
223222
}
224223

src/dom_utils.rs

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use std::cell::RefCell;
22
use std::rc::Rc;
33

44
use html5ever::tendril::StrTendril;
5-
use html5ever::{local_name, namespace_url, ns, Attribute, LocalName, QualName};
5+
use html5ever::{Attribute, LocalName, QualName, local_name, namespace_url, ns};
66
use markup5ever_rcdom::{Handle, Node, NodeData};
77

88
/// Extensions to the DOM interface to make manipulation more ergonimc.
@@ -231,7 +231,7 @@ impl NodeHandleExt for Handle {
231231
name:
232232
QualName {
233233
ns: ns!(html),
234-
ref local,
234+
local,
235235
..
236236
},
237237
..
@@ -247,7 +247,7 @@ impl NodeHandleExt for Handle {
247247
local: local_name!("class"),
248248
};
249249
self.get_attribute(&CLASS)
250-
.map_or(false, |v| v.split_ascii_whitespace().any(|c| c == class))
250+
.is_some_and(|v| v.split_ascii_whitespace().any(|c| c == class))
251251
}
252252

253253
fn has_any_class(&self, classes: &[&str]) -> bool {
@@ -256,22 +256,21 @@ impl NodeHandleExt for Handle {
256256
ns: ns!(),
257257
local: local_name!("class"),
258258
};
259-
self.get_attribute(&CLASS).map_or(false, |v| {
260-
v.split_ascii_whitespace().any(|c| classes.contains(&c))
261-
})
259+
self.get_attribute(&CLASS)
260+
.is_some_and(|v| v.split_ascii_whitespace().any(|c| classes.contains(&c)))
262261
}
263262

264263
fn node_text(&self) -> Option<StrTendril> {
265264
match &self.data {
266-
NodeData::Text { ref contents } => Some(contents.borrow().clone()),
265+
NodeData::Text { contents } => Some(contents.borrow().clone()),
267266
_ => None,
268267
}
269268
}
270269

271270
fn text_content(&self) -> StrTendril {
272271
let mut text = StrTendril::new();
273272
scan_dom(self, &mut |n| {
274-
if let NodeData::Text { ref contents } = &n.data {
273+
if let NodeData::Text { contents } = &n.data {
275274
text.push_tendril(&contents.borrow());
276275
}
277276
});

0 commit comments

Comments
 (0)