Skip to content

Commit b60508a

Browse files
committed
Make set and map initializers more readable.
1 parent 638f37a commit b60508a

File tree

1 file changed

+28
-32
lines changed

1 file changed

+28
-32
lines changed

src/render.rs

Lines changed: 28 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ impl<'a> MarkdownRenderer<'a> {
1818
/// Per `readme_to_html`, `base_url` is the base URL prepended to any
1919
/// relative links in the input document. See that function for more detail.
2020
fn new(base_url: Option<&'a str>) -> MarkdownRenderer<'a> {
21-
let tags = [
21+
let tags = hashset(&[
2222
"a",
2323
"b",
2424
"blockquote",
@@ -58,30 +58,15 @@ impl<'a> MarkdownRenderer<'a> {
5858
"ul",
5959
"hr",
6060
"span",
61-
]
62-
.iter()
63-
.cloned()
64-
.collect();
65-
let tag_attributes = [
66-
("a", ["href", "id", "target"].iter().cloned().collect()),
67-
(
68-
"img",
69-
["width", "height", "src", "alt", "align"]
70-
.iter()
71-
.cloned()
72-
.collect(),
73-
),
74-
(
75-
"input",
76-
["checked", "disabled", "type"].iter().cloned().collect(),
77-
),
78-
]
79-
.iter()
80-
.cloned()
81-
.collect();
82-
let allowed_classes = [(
61+
]);
62+
let tag_attributes = hashmap(&[
63+
("a", hashset(&["href", "id", "target"])),
64+
("img", hashset(&["width", "height", "src", "alt", "align"])),
65+
("input", hashset(&["checked", "disabled", "type"])),
66+
]);
67+
let allowed_classes = hashmap(&[(
8368
"code",
84-
[
69+
hashset(&[
8570
"language-bash",
8671
"language-clike",
8772
"language-glsl",
@@ -96,14 +81,8 @@ impl<'a> MarkdownRenderer<'a> {
9681
"language-scss",
9782
"language-sql",
9883
"yaml",
99-
]
100-
.iter()
101-
.cloned()
102-
.collect(),
103-
)]
104-
.iter()
105-
.cloned()
106-
.collect();
84+
]),
85+
)]);
10786

10887
let sanitizer_base_url = base_url.map(ToString::to_string);
10988

@@ -255,6 +234,23 @@ pub fn readme_to_html(text: &str, filename: &str, base_url: Option<&str>) -> Car
255234
Ok(encode_minimal(text).replace("\n", "<br>\n"))
256235
}
257236

237+
/// Helper function to build a new `HashSet` from the items slice.
238+
fn hashset<T>(items: &[T]) -> std::collections::HashSet<T>
239+
where
240+
T: Clone + Eq + std::hash::Hash,
241+
{
242+
items.iter().cloned().collect()
243+
}
244+
245+
/// Helper function to build a new `HashMap` from a slice of key-value pairs.
246+
fn hashmap<K, V>(items: &[(K, V)]) -> std::collections::HashMap<K, V>
247+
where
248+
K: Clone + Eq + std::hash::Hash,
249+
V: Clone,
250+
{
251+
items.iter().cloned().collect()
252+
}
253+
258254
#[cfg(test)]
259255
mod tests {
260256
use super::*;

0 commit comments

Comments
 (0)