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)?)
}
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()
}
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")
})
}
#[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(()),
}
}
}