Skip to content

Commit b04e99a

Browse files
committed
Sketch recursive template solution
1 parent 041e4ba commit b04e99a

File tree

3 files changed

+68
-39
lines changed

3 files changed

+68
-39
lines changed

src/tools/generate-copyright/src/main.rs

Lines changed: 46 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,13 @@ fn main() -> Result<(), Error> {
4444
}
4545

4646
let template = CopyrightTemplate {
47-
in_tree: collected_tree_metadata.files,
47+
// in_tree: collected_tree_metadata.files,
48+
in_tree: Node::Root {
49+
children: vec![Node::File {
50+
name: "<script>alert(1)</script>".to_string(),
51+
license: License { spdx: "".to_string(), copyright: vec![] },
52+
}],
53+
},
4854
dependencies: collected_cargo_metadata,
4955
};
5056

@@ -62,6 +68,8 @@ struct Metadata {
6268
}
6369

6470
/// Describes one node in our metadata tree
71+
#[derive(Template)]
72+
#[template(path = "node.html")]
6573
#[derive(serde::Deserialize)]
6674
#[serde(rename_all = "kebab-case", tag = "type")]
6775
pub(crate) enum Node {
@@ -81,43 +89,43 @@ where
8189
Ok(())
8290
}
8391

84-
impl std::fmt::Display for Node {
85-
fn fmt(&self, fmt: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
86-
match self {
87-
Node::Root { children } => {
88-
if children.len() > 1 {
89-
with_box(fmt, |f| {
90-
for child in children {
91-
writeln!(f, "{child}")?;
92-
}
93-
Ok(())
94-
})
95-
} else {
96-
for child in children {
97-
writeln!(fmt, "{child}")?;
98-
}
99-
Ok(())
100-
}
101-
}
102-
Node::Directory { name, children, license } => with_box(fmt, |f| {
103-
render_tree_license(std::iter::once(name), license.as_ref(), f)?;
104-
if !children.is_empty() {
105-
writeln!(f, "<p><b>Exceptions:</b></p>")?;
106-
for child in children {
107-
writeln!(f, "{child}")?;
108-
}
109-
}
110-
Ok(())
111-
}),
112-
Node::Group { files, directories, license } => with_box(fmt, |f| {
113-
render_tree_license(directories.iter().chain(files.iter()), Some(license), f)
114-
}),
115-
Node::File { name, license } => {
116-
with_box(fmt, |f| render_tree_license(std::iter::once(name), Some(license), f))
117-
}
118-
}
119-
}
120-
}
92+
// impl std::fmt::Display for Node {
93+
// fn fmt(&self, fmt: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
94+
// match self {
95+
// Node::Root { children } => {
96+
// if children.len() > 1 {
97+
// with_box(fmt, |f| {
98+
// for child in children {
99+
// writeln!(f, "{child}")?;
100+
// }
101+
// Ok(())
102+
// })
103+
// } else {
104+
// for child in children {
105+
// writeln!(fmt, "{child}")?;
106+
// }
107+
// Ok(())
108+
// }
109+
// }
110+
// Node::Directory { name, children, license } => with_box(fmt, |f| {
111+
// render_tree_license(std::iter::once(name), license.as_ref(), f)?;
112+
// if !children.is_empty() {
113+
// writeln!(f, "<p><b>Exceptions:</b></p>")?;
114+
// for child in children {
115+
// writeln!(f, "{child}")?;
116+
// }
117+
// }
118+
// Ok(())
119+
// }),
120+
// Node::Group { files, directories, license } => with_box(fmt, |f| {
121+
// render_tree_license(directories.iter().chain(files.iter()), Some(license), f)
122+
// }),
123+
// Node::File { name, license } => {
124+
// with_box(fmt, |f| render_tree_license(std::iter::once(name), Some(license), f))
125+
// }
126+
// }
127+
// }
128+
// }
121129

122130
/// Draw a series of sibling files/folders, as HTML, into the given formatter.
123131
fn render_tree_license<'a>(

src/tools/generate-copyright/templates/COPYRIGHT.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,4 +51,4 @@ <h3>📦 {{key.name}}-{{key.version}}</h3>
5151
{% endif %}
5252
{% endfor %}
5353
</body>
54-
</html>
54+
</html>
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
{% match self %}
2+
{% when Node::Root with { children } %}
3+
<div><span style="color: red;">root</span>
4+
{% for child in children %}
5+
{{ child|safe }}
6+
{% endfor %}
7+
</div>
8+
{% when Node::Directory with { name, children, license } %}
9+
<div>
10+
<div>{{ name }}</div>
11+
{% for child in children %}
12+
{{ child|safe }}
13+
{% endfor %}
14+
</div>
15+
{% when Node::File with { name, license } %}
16+
{{ name }}
17+
{% when Node::Group with { files, directories, license } %}
18+
<div>
19+
group
20+
</div>
21+
{% endmatch %}

0 commit comments

Comments
 (0)