From cffd8531140ffd4ee735eaa3ca53e9bc602a9f26 Mon Sep 17 00:00:00 2001 From: Steve Biedermann Date: Sun, 12 May 2024 23:27:33 +0200 Subject: [PATCH] update --- Cargo.lock | 42 ++++++++++++ Cargo.toml | 1 + generate.sh | 6 +- req.yml => requirements.yml | 0 src/lib.rs | 3 +- src/main.rs | 24 +++++-- template.html | 126 ++++++++++++++++++++++++++++++++++++ 7 files changed, 193 insertions(+), 9 deletions(-) rename req.yml => requirements.yml (100%) create mode 100644 template.html diff --git a/Cargo.lock b/Cargo.lock index 7528216..e153e67 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -145,6 +145,47 @@ dependencies = [ "strsim", ] +[[package]] +name = "clap_complete" +version = "4.5.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dd79504325bf38b10165b02e89b4347300f855f273c4cb30c4a3209e6583275e" +dependencies = [ + "clap", +] + +[[package]] +name = "clap_complete_command" +version = "0.5.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "183495371ea78d4c9ff638bfc6497d46fed2396e4f9c50aebc1278a4a9919a3d" +dependencies = [ + "clap", + "clap_complete", + "clap_complete_fig", + "clap_complete_nushell", +] + +[[package]] +name = "clap_complete_fig" +version = "4.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "54b3e65f91fabdd23cac3d57d39d5d938b4daabd070c335c006dccb866a61110" +dependencies = [ + "clap", + "clap_complete", +] + +[[package]] +name = "clap_complete_nushell" +version = "0.1.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5d02bc8b1a18ee47c4d2eec3fb5ac034dc68ebea6125b1509e9ccdffcddce66e" +dependencies = [ + "clap", + "clap_complete", +] + [[package]] name = "clap_derive" version = "4.5.4" @@ -476,6 +517,7 @@ version = "0.1.0" dependencies = [ "anyhow", "clap", + "clap_complete_command", "crossterm 0.27.0", "indexmap", "markdown", diff --git a/Cargo.toml b/Cargo.toml index 30b6617..a2a566d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -9,6 +9,7 @@ default-run = "req" [dependencies] anyhow = "1.0.83" clap = { version = "4.5.4", features = ["derive"] } +clap_complete_command = "0.5.1" crossterm = "0.27.0" indexmap = { version = "2.2.6", features = ["serde"] } markdown = "1.0.0-alpha.17" diff --git a/generate.sh b/generate.sh index 3614140..b20b26f 100755 --- a/generate.sh +++ b/generate.sh @@ -7,6 +7,6 @@ mkdir -p out cargo build cargo run -q -- schema > out/schema.json cargo run -q -- demo > out/demo.yml -cargo run -q -- md req.yml > out/requirements.md -cargo run -q -- html req.yml > out/requirements.html -cargo run -q -- check req.yml test_result.txt > out/text_result.md +cargo run -q -- md requirements.yml > out/requirements.md +cargo run -q -- html requirements.yml > out/requirements.html +cargo run -q -- check requirements.yml test_result.txt > out/text_result.md diff --git a/req.yml b/requirements.yml similarity index 100% rename from req.yml rename to requirements.yml diff --git a/src/lib.rs b/src/lib.rs index 2790380..05e6184 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -125,6 +125,7 @@ pub struct Project { serialize_with = "serialize_version", deserialize_with = "deserialize_version" )] + #[schemars(with = "String", regex(pattern = r"^\d\.\d\.\d$"))] pub version: Version, #[serde(serialize_with = "my_trim")] pub description: String, @@ -138,5 +139,5 @@ pub struct Project { #[must_use] pub fn demo_project() -> Project { - serde_yaml::from_str(include_str!("../req.yml")).expect("Should never happen!") + serde_yaml::from_str(include_str!("../requirements.yml")).expect("Should never happen!") } diff --git a/src/main.rs b/src/main.rs index 8151b77..9fb80f2 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,6 +1,6 @@ use std::path::PathBuf; -use clap::Parser; +use clap::{CommandFactory, Parser, Subcommand}; use indexmap::{ map::{Keys, Values}, IndexMap, @@ -39,7 +39,7 @@ fn check_requirements( requirements: &IndexMap, allowed_requirements: &Regex, ) { - for (id, _) in dbg!(requirements) { + for (id, _) in requirements { if allowed_requirements.is_match(id) { if test_results.contains(&format!("{}: failed", id.trim())) { output.insert(id.trim().to_string(), false); @@ -163,7 +163,7 @@ fn add_topics(output: &mut Vec, topics: &IndexMap, level: } } -#[derive(Parser)] +#[derive(Subcommand)] enum Command { /// Outputs the JSON schema for the input data Schema, @@ -191,6 +191,12 @@ enum Command { #[arg(required=true, num_args=1..)] test_results: Vec, }, + /// Generate shell completions + Completions { + /// The shell to generate the completions for + #[arg(value_enum)] + shell: clap_complete_command::Shell, + }, } #[derive(Parser)] @@ -295,10 +301,14 @@ fn main() -> anyhow::Result<()> { } Command::Html { requirements } => { let output = to_markdown(requirements, false)?; + let template = include_str!("../template.html"); println!( "{}", - markdown::to_html_with_options(&output, &markdown::Options::gfm()) - .map_err(|e| anyhow::anyhow!("{e}"))? + template.replace( + "{{content}}", + &markdown::to_html_with_options(&output, &markdown::Options::gfm()) + .map_err(|e| anyhow::anyhow!("{e}"))? + ) ); } Command::Schema => { @@ -322,7 +332,11 @@ fn main() -> anyhow::Result<()> { let output = output.join("\n"); println!("{output}"); } + Command::Completions { shell } => { + shell.generate(&mut Args::command(), &mut std::io::stdout()); + } } Ok(()) } + diff --git a/template.html b/template.html new file mode 100644 index 0000000..6a492f1 --- /dev/null +++ b/template.html @@ -0,0 +1,126 @@ + + + + + + {{content}} +