Skip to content

Commit

Permalink
feat: explain command (#4)
Browse files Browse the repository at this point in the history
* feat: explain command

* Update explain.rs

* feat: pretty print explain

* chore: clean

* fix: get pretty_print_rule() to work

Co-Authored-By: Andrew Thrasher <1165729+adthrasher@users.noreply.github.com>

* Update README.md

* docs: warning -> rule

Co-Authored-By: Andrew Thrasher <1165729+adthrasher@users.noreply.github.com>

---------

Co-authored-by: Andrew Thrasher <1165729+adthrasher@users.noreply.github.com>
  • Loading branch information
a-frantz and adthrasher authored Jun 3, 2024
1 parent 8a35eef commit f4cc42b
Show file tree
Hide file tree
Showing 6 changed files with 73 additions and 8 deletions.
3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,6 @@ 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"
colored = "2.1.0"
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@

## 🎨 Features

* **`sprocket lint`.** Lint Workflow Description Language files.
* **`sprocket lint`** Lint Workflow Description Language files.
* **`sprocket explain`** Explain lint rules.

## Guiding Principles

Expand Down
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;
51 changes: 51 additions & 0 deletions src/commands/explain.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
use clap::Parser;
use colored::Colorize;
use wdl::ast::v1::lint as ast_lint;
use wdl::core::concern::lint::Rule;
use wdl::grammar::v1::lint as grammar_lint;

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

pub fn pretty_print_rule<E>(rule: &dyn Rule<E>) {
println!("{}", rule.name().bold().underline());
println!("{}", format!("{}::{}", rule.code(), rule.tags(),).yellow());
println!();
println!("{}", rule.body());
}

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

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

match rule {
Some(rule) => {
pretty_print_rule(&*rule);
}
None => {
let rule = ast_lint::rules()
.into_iter()
.find(|rule| rule.name() == ident || rule.code().to_string() == ident);

match rule {
Some(rule) => {
pretty_print_rule(&*rule);
}
None => {
anyhow::bail!("No rule found with the identifier '{}'", ident);
}
}
}
}

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
19 changes: 13 additions & 6 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,13 +56,20 @@ 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()),
]);
}

if let Some(fix) = warning.fix() {
diagnostic = diagnostic.with_notes(vec![format!("fix: {}", fix)]);
}
let mut notes = match warning.fix() {
Some(fix) => vec![format!("fix: {}", fix)],
None => vec![],
};
notes.extend(vec![format!(
"see `sprocket explain {}` for more information",
warning.code()
)]);

diagnostic = diagnostic.with_notes(notes);

diagnostic
}
Expand Down

0 comments on commit f4cc42b

Please sign in to comment.