Skip to content

Commit

Permalink
Sketch recursive template solution
Browse files Browse the repository at this point in the history
  • Loading branch information
Kobzol committed Jul 31, 2024
1 parent 041e4ba commit b04e99a
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 39 deletions.
84 changes: 46 additions & 38 deletions src/tools/generate-copyright/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,13 @@ fn main() -> Result<(), Error> {
}

let template = CopyrightTemplate {
in_tree: collected_tree_metadata.files,
// in_tree: collected_tree_metadata.files,
in_tree: Node::Root {
children: vec![Node::File {
name: "<script>alert(1)</script>".to_string(),
license: License { spdx: "".to_string(), copyright: vec![] },
}],
},
dependencies: collected_cargo_metadata,
};

Expand All @@ -62,6 +68,8 @@ struct Metadata {
}

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

impl std::fmt::Display for Node {
fn fmt(&self, fmt: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Node::Root { children } => {
if children.len() > 1 {
with_box(fmt, |f| {
for child in children {
writeln!(f, "{child}")?;
}
Ok(())
})
} else {
for child in children {
writeln!(fmt, "{child}")?;
}
Ok(())
}
}
Node::Directory { name, children, license } => with_box(fmt, |f| {
render_tree_license(std::iter::once(name), license.as_ref(), f)?;
if !children.is_empty() {
writeln!(f, "<p><b>Exceptions:</b></p>")?;
for child in children {
writeln!(f, "{child}")?;
}
}
Ok(())
}),
Node::Group { files, directories, license } => with_box(fmt, |f| {
render_tree_license(directories.iter().chain(files.iter()), Some(license), f)
}),
Node::File { name, license } => {
with_box(fmt, |f| render_tree_license(std::iter::once(name), Some(license), f))
}
}
}
}
// impl std::fmt::Display for Node {
// fn fmt(&self, fmt: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
// match self {
// Node::Root { children } => {
// if children.len() > 1 {
// with_box(fmt, |f| {
// for child in children {
// writeln!(f, "{child}")?;
// }
// Ok(())
// })
// } else {
// for child in children {
// writeln!(fmt, "{child}")?;
// }
// Ok(())
// }
// }
// Node::Directory { name, children, license } => with_box(fmt, |f| {
// render_tree_license(std::iter::once(name), license.as_ref(), f)?;
// if !children.is_empty() {
// writeln!(f, "<p><b>Exceptions:</b></p>")?;
// for child in children {
// writeln!(f, "{child}")?;
// }
// }
// Ok(())
// }),
// Node::Group { files, directories, license } => with_box(fmt, |f| {
// render_tree_license(directories.iter().chain(files.iter()), Some(license), f)
// }),
// Node::File { name, license } => {
// with_box(fmt, |f| render_tree_license(std::iter::once(name), Some(license), f))
// }
// }
// }
// }

/// Draw a series of sibling files/folders, as HTML, into the given formatter.
fn render_tree_license<'a>(
Expand Down
2 changes: 1 addition & 1 deletion src/tools/generate-copyright/templates/COPYRIGHT.html
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,4 @@ <h3>📦 {{key.name}}-{{key.version}}</h3>
{% endif %}
{% endfor %}
</body>
</html>
</html>
21 changes: 21 additions & 0 deletions src/tools/generate-copyright/templates/node.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{% match self %}
{% when Node::Root with { children } %}
<div><span style="color: red;">root</span>
{% for child in children %}
{{ child|safe }}
{% endfor %}
</div>
{% when Node::Directory with { name, children, license } %}
<div>
<div>{{ name }}</div>
{% for child in children %}
{{ child|safe }}
{% endfor %}
</div>
{% when Node::File with { name, license } %}
{{ name }}
{% when Node::Group with { files, directories, license } %}
<div>
group
</div>
{% endmatch %}

0 comments on commit b04e99a

Please sign in to comment.