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()
)
});
#[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(commands::render::RenderArgs),
Check(commands::check::CheckArgs),
}
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());
}
}
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);
}