quillmark-cli 0.145.0

Command-line interface for Quillmark, a schema-driven document framework
mod commands;
mod errors;
mod output;

use clap::{Parser, Subcommand};
use std::process;
use std::sync::LazyLock;

static VERSION: LazyLock<String> = LazyLock::new(|| {
    format!(
        "{} (Typst {})",
        env!("CARGO_PKG_VERSION"),
        quillmark::typst_version()
    )
});

/// The engine's own command line: `render` and `check`, in the grammar
/// quillkit shares. Every other verb is quillkit's.
#[derive(Parser)]
#[command(name = "quillmark")]
#[command(about = "Render and check Quillmark quills and documents", long_about = None)]
#[command(version = VERSION.as_str())]
#[command(after_help = "quillkit carries every other verb: npx quillkit --help")]
struct Cli {
    #[command(subcommand)]
    command: Commands,
}

#[derive(Subcommand)]
enum Commands {
    /// Render a quill's example, its blank form, or a Markdown document
    Render(commands::render::RenderArgs),

    /// Check a quill, or documents against a quill's schema
    Check(commands::check::CheckArgs),
}

/// The verbs this binary no longer carries, each with where it went.
const GONE: &[(&str, &str)] = &[
    ("validate", "quillmark check <quill>"),
    (
        "info",
        "read quill.schema in the quillmark library on npm or PyPI",
    ),
    (
        "schema",
        "read quill.schema in the quillmark library on npm or PyPI",
    ),
    (
        "pack",
        "call quill.toBundle() in the quillmark library on npm",
    ),
    (
        "workspace",
        "call engine.typstWorkspace() in the quillmark library on npm",
    ),
];

fn main() {
    let argv: Vec<String> = std::env::args().collect();
    let json = argv.iter().any(|arg| arg == "--json");
    if let Some((verb, instead)) = argv
        .get(1)
        .and_then(|verb| GONE.iter().find(|(gone, _)| gone == verb))
    {
        refuse(json, format!("quillmark {verb} is gone: {instead}"));
    }
    let cli = match Cli::try_parse() {
        Ok(cli) => cli,
        Err(e) if json && e.use_stderr() => {
            let text = e.to_string();
            let first = text.lines().next().unwrap_or_default();
            refuse(json, first.trim_start_matches("error: ").to_string())
        }
        Err(e) => e.exit(),
    };

    let result = match cli.command {
        Commands::Render(args) => commands::render::execute(args),
        Commands::Check(args) => commands::check::execute(args),
    };

    if let Err(e) = result {
        if json && !matches!(e, errors::CliError::Reported) {
            output::print_report(&quillmark::Report {
                ok: false,
                diagnostics: e
                    .diagnostics()
                    .iter()
                    .map(quillmark::ReportDiagnostic::from)
                    .collect(),
                ..quillmark::Report::default()
            });
        } else {
            errors::print_cli_error(&e);
        }
        process::exit(e.exit_code());
    }
}

/// A wrong invocation, reported as one and exiting 2.
fn refuse(json: bool, message: String) -> ! {
    let e = errors::CliError::InvalidArgument(message);
    if json {
        output::print_report(&quillmark::Report {
            ok: false,
            diagnostics: e
                .diagnostics()
                .iter()
                .map(quillmark::ReportDiagnostic::from)
                .collect(),
            ..quillmark::Report::default()
        });
    } else {
        errors::print_cli_error(&e);
    }
    process::exit(2);
}