quillmark-cli 0.145.0

Command-line interface for Quillmark, a schema-driven document framework
pub mod check;
pub mod render;

use crate::errors::{CliError, Result};
use clap::Args;
use quillmark::{CalendarDate, Quill};
use std::path::{Path, PathBuf};

pub fn load_quill(path: &Path) -> Result<Quill> {
    if path.is_file() {
        let ext = path.extension().and_then(|e| e.to_str()).unwrap_or("");
        if ext.eq_ignore_ascii_case("typ") {
            return Err(CliError::InvalidArgument(format!(
                "{} is a Typst file, and a quill is the directory holding Quill.yaml: \
                 `npx quillkit adopt {}` makes its project one",
                path.display(),
                path.display()
            )));
        }
        if ext != "quill" {
            return Err(CliError::InvalidArgument(format!(
                "{} is neither a quill nor a .quill bundle: a quill is the directory \
                 holding Quill.yaml, and -o names the file to write",
                path.display()
            )));
        }
    }
    Ok(quillmark::quill_from_path(path)?)
}

/// The bytes of each `--font` file, in the order given.
pub fn read_fonts(paths: &[PathBuf]) -> Result<Vec<Vec<u8>>> {
    paths
        .iter()
        .map(|path| {
            std::fs::read(path).map_err(|e| {
                CliError::Io(std::io::Error::new(
                    e.kind(),
                    format!("Failed to read font '{}': {e}", path.display()),
                ))
            })
        })
        .collect()
}

/// The render date the CLI supplies: `pinned` when given, else the local date,
/// or the UTC date where the local offset cannot be read.
pub fn render_date(pinned: Option<CalendarDate>) -> CalendarDate {
    pinned.unwrap_or_else(|| {
        let now =
            time::OffsetDateTime::now_local().unwrap_or_else(|_| time::OffsetDateTime::now_utc());
        CalendarDate::new(now.year(), u8::from(now.month()), now.day())
            .expect("the clock reads a calendar date")
    })
}

/// Spellings the CLI no longer takes, parsed only to refuse each with a line
/// naming its replacement rather than clap's bare "unexpected argument".
#[derive(Args)]
pub struct Refused {
    #[arg(long, hide = true)]
    stdout: bool,
    #[arg(long, hide = true)]
    quiet: bool,
    #[arg(long, hide = true, value_name = "DATA_FILE")]
    output_data: Option<PathBuf>,
    #[arg(long, hide = true)]
    seed: bool,
}

impl Refused {
    pub fn refuse(&self) -> Result<()> {
        let gone = [
            (self.stdout, "--stdout", "give -o -"),
            (self.quiet, "--quiet", "--json prints the report alone"),
            (
                self.output_data.is_some(),
                "--output-data",
                "the compiled data is Quill::compile_data in Rust and quill.compileData in the npm package",
            ),
            (self.seed, "--seed", "the blank form is --blank"),
        ];
        match gone.into_iter().find(|(given, ..)| *given) {
            Some((_, flag, instead)) => Err(CliError::InvalidArgument(format!(
                "{flag} is gone: {instead}"
            ))),
            None => Ok(()),
        }
    }
}