Skip to content

Commit

Permalink
feat: explain command
Browse files Browse the repository at this point in the history
  • Loading branch information
a-frantz committed May 31, 2024
1 parent 8a35eef commit ce51f17
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 4 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,5 @@ nonempty = "0.9.0"
pest = { version = "2.7.5", features = ["pretty-print"] }
tracing = "0.1.40"
tracing-subscriber = "0.3.18"
wdl = { version = "0.2.0", features = ["ast", "core", "grammar"] }
wdl = { version = "0.3.0", features = ["ast", "core", "grammar"] }
walkdir = "2.4.0"
1 change: 1 addition & 0 deletions src/commands.rs
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
pub mod explain;
pub mod lint;
45 changes: 45 additions & 0 deletions src/commands/explain.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
use clap::Parser;
use wdl::ast::v1::lint as ast_lint;
use wdl::grammar::v1::lint as grammar_lint;

/// Arguments for the `explain` subcommand.
#[derive(Parser, Debug)]
#[command(author, version, about)]
pub struct Args {
/// The rule name to explain.
#[arg(required = true)]
pub rule_name: String,
}

pub fn explain(args: Args) -> anyhow::Result<()> {
let name = args.rule_name;

let rules = grammar_lint::rules()
.iter()
.map(|rule| rule.name())
.chain(ast_lint::rules().iter().map(|rule| rule.name()))
.collect::<Vec<String>>();

if !rules.contains(&name) {
return Err(anyhow::anyhow!("Unknown rule: {}", name));
}

let rule = grammar_lint::rules()
.into_iter()
.find(|rule| rule.name() == name);

match rule {
Some(rule) => {
println!("{}", rule.body());
}
None => {
let rule = ast_lint::rules()
.into_iter()
.find(|rule| rule.name() == name)
.unwrap();
println!("{}", rule.body());
}
}

Ok(())
}
4 changes: 4 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ git_testament!(TESTAMENT);
enum Commands {
/// Lints Workflow Description Language files.
Lint(commands::lint::Args),

/// Explains a lint warning.
Explain(commands::explain::Args),
}

#[derive(Parser)]
Expand Down Expand Up @@ -51,6 +54,7 @@ pub fn inner() -> anyhow::Result<()> {

match cli.command {
Commands::Lint(args) => commands::lint::lint(args),
Commands::Explain(args) => commands::explain::explain(args),
}
}

Expand Down
6 changes: 3 additions & 3 deletions src/report.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,9 @@ impl<'a> Reporter<'a> {
Concern::LintWarning(warning) => {
let mut diagnostic = Diagnostic::warning()
.with_code(format!(
"{}::{}/{:?}",
"{}::{}::{:?}",
warning.code(),
warning.group(),
warning.tags(),
warning.level()
))
.with_message(warning.subject());
Expand All @@ -56,7 +56,7 @@ impl<'a> Reporter<'a> {
let byte_range = location.byte_range().unwrap();

diagnostic = diagnostic.with_labels(vec![
Label::primary(handle, byte_range).with_message(warning.body()),
Label::primary(handle, byte_range).with_message(warning.subject()),
]);
}

Expand Down

0 comments on commit ce51f17

Please sign in to comment.