This commit is contained in:
Biedermann Steve 2024-05-10 11:39:01 +02:00
parent ed2b803ce5
commit e16b92f526
1 changed files with 19 additions and 9 deletions

View File

@ -149,19 +149,29 @@ fn add_topics(output: &mut Vec<String>, topics: &IndexMap<String, Topic>, level:
#[derive(Parser)] #[derive(Parser)]
enum Command { enum Command {
/// Outputs the JSON schema for the input data
Schema, Schema,
/// Outputs demo data in YAML format
Demo, Demo,
#[clap(alias = "md")] #[clap(alias = "md")]
/// Transform requirements into Markdown
Markdown { Markdown {
/// The path to the requirements file
requirements: PathBuf, requirements: PathBuf,
}, },
/// Transform requirements into HTML
Html { Html {
/// The path to the requirements file
requirements: PathBuf, requirements: PathBuf,
}, },
/// Check test output against requirements
Check { Check {
#[arg(short, long, default_value = "REQ-.*")] #[arg(short, long, default_value = "REQ-.*")]
/// Regex to select which requirements should be checked
allowed_requirements: String, allowed_requirements: String,
/// The path to the requirements file
requirements: PathBuf, requirements: PathBuf,
/// The path to the test output file
test_results: PathBuf, test_results: PathBuf,
}, },
} }
@ -179,14 +189,14 @@ fn parse(value: &str) -> anyhow::Result<Project> {
.or_else(|_| toml::from_str(value))?) .or_else(|_| toml::from_str(value))?)
} }
fn to_markdown(requirements: PathBuf) -> anyhow::Result<String> { fn to_markdown(requirements: PathBuf, add_toc: bool) -> anyhow::Result<String> {
let project: Project = parse(&std::fs::read_to_string(requirements)?)?; let project: Project = parse(&std::fs::read_to_string(requirements)?)?;
let mut output = vec![ let mut output = vec![format!("# Requirements for {}", project.name.trim()), nl()];
format!("# Requirements for {}", project.name.trim()), if add_toc {
nl(), output.extend([s!("[[_TOC_]]"), nl()]);
s!("[[_TOC_]]"), }
nl(), output.extend([
WORD_DESCRIPTION.trim().to_string(), WORD_DESCRIPTION.trim().to_string(),
nl(), nl(),
format!("**VERSION: {}**", project.version), format!("**VERSION: {}**", project.version),
@ -194,7 +204,7 @@ fn to_markdown(requirements: PathBuf) -> anyhow::Result<String> {
s!("## Description"), s!("## Description"),
project.description.trim().to_string(), project.description.trim().to_string(),
nl(), nl(),
]; ]);
if !project.topics.is_empty() { if !project.topics.is_empty() {
output.push(s!("## Requirements")); output.push(s!("## Requirements"));
@ -266,7 +276,7 @@ fn main() -> anyhow::Result<()> {
println!("{}", serde_yaml::to_string(&demo_project())?); println!("{}", serde_yaml::to_string(&demo_project())?);
} }
Command::Html { requirements } => { Command::Html { requirements } => {
let output = to_markdown(requirements)?; let output = to_markdown(requirements, false)?;
println!( println!(
"{}", "{}",
markdown::to_html_with_options(&output, &markdown::Options::gfm()) markdown::to_html_with_options(&output, &markdown::Options::gfm())
@ -278,7 +288,7 @@ fn main() -> anyhow::Result<()> {
println!("{}", serde_json::to_string_pretty(&schema).unwrap()); println!("{}", serde_json::to_string_pretty(&schema).unwrap());
} }
Command::Markdown { requirements } => { Command::Markdown { requirements } => {
let output = to_markdown(requirements)?; let output = to_markdown(requirements, true)?;
println!("{output}"); println!("{output}");
} }
Command::Check { Command::Check {