use crate::{CliResult, GlobalConfig};
use clap::Subcommand;
use css_parse::{Diagnostic, DiagnosticMeta};
mod build;
mod check;
mod colors;
mod dbg_lex;
mod dbg_parse;
mod expand;
mod find;
mod fmt;
mod lsp;
mod min;
mod tree;
#[derive(Subcommand, Debug)]
pub enum Commands {
Check(check::Check),
Find(find::Find),
Tree(tree::Tree),
Fmt(fmt::Fmt),
Min(min::Min),
#[command(hide = true)]
Expand(expand::Expand),
Colors(colors::ColorCommand),
#[command(hide = true)]
Colours(colors::ColorCommand),
#[command(hide = true)]
DbgLex(dbg_lex::DbgLex),
#[command(hide = true)]
DbgParse(dbg_parse::DbgParse),
#[command(arg_required_else_help(true))]
Build(build::Build),
Lsp(lsp::Lsp),
}
impl Commands {
pub fn run(&self, config: GlobalConfig) -> CliResult {
match self {
Commands::Check(cmd) => cmd.run(config),
Commands::Find(cmd) => cmd.run(config),
Commands::Tree(cmd) => cmd.run(config),
Commands::Fmt(cmd) => cmd.run(config),
Commands::Min(cmd) => cmd.run(config),
Commands::Expand(cmd) => cmd.run(config),
Commands::Colors(cmd) => cmd.run(config),
Commands::Colours(cmd) => cmd.run(config),
Commands::DbgLex(cmd) => cmd.run(config),
Commands::DbgParse(cmd) => cmd.run(config),
Commands::Build(cmd) => cmd.run(config),
Commands::Lsp(cmd) => cmd.run(config),
}
}
}
pub fn format_diagnostic_error(err: &Diagnostic, source: &str, file_name: &str) -> String {
use miette::{GraphicalReportHandler, GraphicalTheme, NamedSource};
let handler = GraphicalReportHandler::new_themed(GraphicalTheme::unicode_nocolor());
let mut report = String::new();
let named = NamedSource::new(file_name, source.to_string());
let miette_err = err.into_diagnostic(source);
let err_with_source = miette::Report::new(miette_err).with_source_code(named);
if handler.render_report(&mut report, &*err_with_source).is_ok() {
return report;
}
let DiagnosticMeta { code, message, help, .. } = (err.formatter)(err, source);
format!("Error [{code}]: {message}\nHelp: {help}\n")
}