From f01dd60cd57c5b95fc19751226ce34bcbdb94ce9 Mon Sep 17 00:00:00 2001 From: Jonathan Pallant Date: Wed, 31 Jul 2024 19:26:44 +0100 Subject: [PATCH] generate-copyright: Render Node with rinja too. --- Cargo.lock | 4 + src/tools/generate-copyright/Cargo.toml | 1 - src/tools/generate-copyright/src/main.rs | 73 +------------------ .../generate-copyright/templates/Node.html | 71 ++++++++++++++++++ 4 files changed, 77 insertions(+), 72 deletions(-) create mode 100644 src/tools/generate-copyright/templates/Node.html diff --git a/Cargo.lock b/Cargo.lock index 74376ad488029..e216aeb23d0eb 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1478,6 +1478,7 @@ version = "0.1.0" dependencies = [ "anyhow", "cargo_metadata 0.18.1", + "rinja", "serde", "serde_json", "tempfile", @@ -3298,6 +3299,9 @@ version = "0.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d2d47a46d7729e891c8accf260e9daa02ae6d570aa2a94fb1fb27eb5364a2323" dependencies = [ + "humansize", + "num-traits", + "percent-encoding", "rinja_derive", ] diff --git a/src/tools/generate-copyright/Cargo.toml b/src/tools/generate-copyright/Cargo.toml index c00292cf33108..1e269cd54d554 100644 --- a/src/tools/generate-copyright/Cargo.toml +++ b/src/tools/generate-copyright/Cargo.toml @@ -9,7 +9,6 @@ description = "Produces a manifest of all the copyrighted materials in the Rust [dependencies] anyhow = "1.0.65" cargo_metadata = "0.18.1" -html-escape = "0.2.13" rinja = "0.2.0" serde = { version = "1.0.147", features = ["derive"] } serde_json = "1.0.85" diff --git a/src/tools/generate-copyright/src/main.rs b/src/tools/generate-copyright/src/main.rs index 03b789b739298..37de24648d56d 100644 --- a/src/tools/generate-copyright/src/main.rs +++ b/src/tools/generate-copyright/src/main.rs @@ -62,8 +62,9 @@ struct Metadata { } /// Describes one node in our metadata tree -#[derive(serde::Deserialize)] +#[derive(serde::Deserialize, rinja::Template)] #[serde(rename_all = "kebab-case", tag = "type")] +#[template(path = "Node.html")] pub(crate) enum Node { Root { children: Vec }, Directory { name: String, children: Vec, license: Option }, @@ -71,76 +72,6 @@ pub(crate) enum Node { Group { files: Vec, directories: Vec, license: License }, } -fn with_box(fmt: &mut std::fmt::Formatter<'_>, inner: F) -> std::fmt::Result -where - F: FnOnce(&mut std::fmt::Formatter<'_>) -> std::fmt::Result, -{ - writeln!(fmt, r#"
"#)?; - inner(fmt)?; - writeln!(fmt, "
")?; - 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, "

Exceptions:

")?; - 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>( - names: impl Iterator, - license: Option<&License>, - f: &mut std::fmt::Formatter<'_>, -) -> std::fmt::Result { - writeln!(f, "

File/Directory: ")?; - for name in names { - writeln!(f, "{}", html_escape::encode_text(&name))?; - } - writeln!(f, "

")?; - - if let Some(license) = license { - writeln!(f, "

License: {}

", html_escape::encode_text(&license.spdx))?; - for copyright in license.copyright.iter() { - writeln!(f, "

Copyright: {}

", html_escape::encode_text(©right))?; - } - } - - Ok(()) -} - /// A License has an SPDX license name and a list of copyright holders. #[derive(serde::Deserialize)] struct License { diff --git a/src/tools/generate-copyright/templates/Node.html b/src/tools/generate-copyright/templates/Node.html new file mode 100644 index 0000000000000..a71a1bf3b73d7 --- /dev/null +++ b/src/tools/generate-copyright/templates/Node.html @@ -0,0 +1,71 @@ +{% match self %} + +{% when Node::Root { children } %} + +{% for child in children %} +{{ child|safe }} +{% endfor %} + +{% when Node::Directory { name, children, license } %} + +
+ +

+ File/Directory: {{ name }} +

+ + {% if let Some(license) = license %} + +

License: {{ license.spdx }}

+ {% for copyright in license.copyright.iter() %} +

Copyright: {{ copyright }}

+ {% endfor %} + + {% endif %} + + {% if !children.is_empty() %} + +

Exceptions:

+ {% for child in children %} + {{ child|safe }} + {% endfor %} + + {% endif %} + +
+ +{% when Node::File { name, license } %} + +
+

+ File/Directory: {{ name }} +

+ +

License: {{ license.spdx }}

+ {% for copyright in license.copyright.iter() %} +

Copyright: {{ copyright }}

+ {% endfor %} +
+ +{% when Node::Group { files, directories, license } %} + +
+ +

+ File/Directory: + {% for name in files %} + {{ name }} + {% endfor %} + {% for name in directories %} + {{ name }} + {% endfor %} +

+ +

License: {{ license.spdx }}

+ {% for copyright in license.copyright.iter() %} +

Copyright: {{ copyright }}

+ {% endfor %} + +
+ +{% endmatch %}