use std::path::PathBuf;
use clap::Subcommand;
use oxvg_lint::{error::LintingError, Rules, Severity};
use crate::{args::RunCommand, config::Config, walk::Walk};
mod lsp;
#[derive(clap::Args, Debug)]
pub struct Check {
#[clap(value_parser)]
pub paths: Vec<PathBuf>,
#[clap(long, short, num_args(0..=1))]
pub config: Option<Vec<PathBuf>>,
#[clap(long, short, default_value = "false")]
pub recursive: bool,
#[clap(long, short = '.', default_value = "false")]
pub hidden: bool,
#[clap(long, default_value = "false")]
pub no_ignore: bool,
#[clap(long, short, default_value = "0")]
pub threads: usize,
#[clap(long, short, default_value = "error")]
pub level: Severity,
}
impl RunCommand for Check {
async fn run(self, config: Config) -> anyhow::Result<()> {
self.walk(&config.lint.unwrap_or_else(Rules::recommended))
}
}
impl Check {
fn walk(self, rules: &Rules) -> anyhow::Result<()> {
let walk = Walk {
paths: &self.paths,
output: None,
recursive: self.recursive,
hidden: self.hidden,
no_ignore: self.no_ignore,
threads: self.threads,
};
walk.run(|| {
let rules = rules.clone();
Box::new(move |source, path, _| {
let result = rules.lint_with_path(source, path);
match result {
Err(LintingError::Reported { .. }) => {}
Err(err) => eprintln!("{err}"),
_ => {}
}
})
})
}
}
#[derive(clap::Args, Debug)]
pub struct Serve {
#[clap(long, short, num_args(0..=1))]
pub config: Option<Vec<PathBuf>>,
}
impl RunCommand for Serve {
async fn run(self, config: Config) -> anyhow::Result<()> {
lsp::serve(config.lint.unwrap_or_else(Rules::recommended)).await;
Ok(())
}
}
#[derive(Subcommand)]
pub enum Lint {
Check(Check),
Serve(Serve),
}