use std::fmt::{self, Display, Formatter};
use std::num::NonZeroUsize;
use std::ops::RangeInclusive;
use std::path::PathBuf;
use std::str::FromStr;
use chrono::{DateTime, Utc};
use clap::builder::{TypedValueParser, ValueParser};
use clap::{ArgAction, Args, ColorChoice, Parser, Subcommand, ValueEnum, ValueHint};
use semver::Version;
const ENV_PATH_SEP: char = if cfg!(windows) { ';' } else { ':' };
#[rustfmt::skip]
const HELP_TEMPLATE: &str = "\
Typst {version}
{usage-heading} {usage}
{all-args}{after-help}\
";
#[rustfmt::skip]
const AFTER_HELP: &str = color_print::cstr!("\
<s><u>Resources:</></>
<s>Tutorial:</> https://typst.app/docs/tutorial/
<s>Reference documentation:</> https://typst.app/docs/reference/
<s>Templates & Packages:</> https://typst.app/universe/
<s>Forum for questions:</> https://forum.typst.app/
");
#[derive(Debug, Clone, Parser)]
#[clap(
name = "typst",
version = crate::typst_version(),
author,
help_template = HELP_TEMPLATE,
after_help = AFTER_HELP,
max_term_width = 80,
)]
pub struct CliArguments {
#[command(subcommand)]
pub command: Command,
#[clap(long, default_value_t = ColorChoice::Auto, default_missing_value = "always")]
pub color: ColorChoice,
#[clap(long, env = "TYPST_CERT")]
pub cert: Option<PathBuf>,
}
#[derive(Debug, Clone, Subcommand)]
#[command()]
pub enum Command {
#[command(visible_alias = "c")]
Compile(CompileCommand),
#[command(visible_alias = "w")]
Watch(WatchCommand),
Init(InitCommand),
Query(QueryCommand),
Fonts(FontsCommand),
#[cfg_attr(not(feature = "self-update"), clap(hide = true))]
Update(UpdateCommand),
}
#[derive(Debug, Clone, Parser)]
pub struct CompileCommand {
#[clap(flatten)]
pub args: CompileArgs,
}
#[derive(Debug, Clone, Parser)]
pub struct WatchCommand {
#[clap(flatten)]
pub args: CompileArgs,
#[cfg(feature = "http-server")]
#[clap(flatten)]
pub server: ServerArgs,
}
#[derive(Debug, Clone, Parser)]
pub struct InitCommand {
pub template: String,
pub dir: Option<String>,
#[clap(flatten)]
pub package: PackageArgs,
}
#[derive(Debug, Clone, Parser)]
pub struct QueryCommand {
#[clap(value_parser = input_value_parser(), value_hint = ValueHint::FilePath)]
pub input: Input,
pub selector: String,
#[clap(long = "field")]
pub field: Option<String>,
#[clap(long = "one", default_value = "false")]
pub one: bool,
#[clap(long = "format", default_value_t)]
pub format: SerializationFormat,
#[clap(long)]
pub pretty: bool,
#[clap(flatten)]
pub world: WorldArgs,
#[clap(flatten)]
pub process: ProcessArgs,
}
#[derive(Debug, Clone, Parser)]
pub struct FontsCommand {
#[clap(flatten)]
pub font: FontArgs,
#[arg(long)]
pub variants: bool,
}
#[derive(Debug, Clone, Parser)]
pub struct UpdateCommand {
pub version: Option<Version>,
#[clap(long, default_value_t = false)]
pub force: bool,
#[clap(
long,
default_value_t = false,
conflicts_with = "version",
conflicts_with = "force"
)]
pub revert: bool,
#[clap(long = "backup-path", env = "TYPST_UPDATE_BACKUP_PATH", value_name = "FILE")]
pub backup_path: Option<PathBuf>,
}
#[derive(Debug, Clone, Args)]
pub struct CompileArgs {
#[clap(value_parser = input_value_parser(), value_hint = ValueHint::FilePath)]
pub input: Input,
#[clap(
required_if_eq("input", "-"),
value_parser = output_value_parser(),
value_hint = ValueHint::FilePath,
)]
pub output: Option<Output>,
#[arg(long = "format", short = 'f')]
pub format: Option<OutputFormat>,
#[clap(flatten)]
pub world: WorldArgs,
#[arg(long = "pages", value_delimiter = ',')]
pub pages: Option<Vec<Pages>>,
#[arg(long = "pdf-standard", value_delimiter = ',')]
pub pdf_standard: Vec<PdfStandard>,
#[arg(long = "ppi", default_value_t = 144.0)]
pub ppi: f32,
#[clap(long = "make-deps", value_name = "PATH")]
pub make_deps: Option<PathBuf>,
#[clap(flatten)]
pub process: ProcessArgs,
#[arg(long = "open", value_name = "VIEWER")]
pub open: Option<Option<String>>,
#[arg(long = "timings", value_name = "OUTPUT_JSON")]
pub timings: Option<Option<PathBuf>>,
}
#[derive(Debug, Clone, Args)]
pub struct WorldArgs {
#[clap(long = "root", env = "TYPST_ROOT", value_name = "DIR")]
pub root: Option<PathBuf>,
#[clap(
long = "input",
value_name = "key=value",
action = ArgAction::Append,
value_parser = ValueParser::new(parse_sys_input_pair),
)]
pub inputs: Vec<(String, String)>,
#[clap(flatten)]
pub font: FontArgs,
#[clap(flatten)]
pub package: PackageArgs,
#[clap(
long = "creation-timestamp",
env = "SOURCE_DATE_EPOCH",
value_name = "UNIX_TIMESTAMP",
value_parser = parse_source_date_epoch,
)]
pub creation_timestamp: Option<DateTime<Utc>>,
}
#[derive(Debug, Clone, Args)]
pub struct ProcessArgs {
#[clap(long, short)]
pub jobs: Option<usize>,
#[arg(long = "features", value_delimiter = ',', env = "TYPST_FEATURES")]
pub features: Vec<Feature>,
#[clap(long, default_value_t)]
pub diagnostic_format: DiagnosticFormat,
}
#[derive(Debug, Clone, Args)]
pub struct PackageArgs {
#[clap(long = "package-path", env = "TYPST_PACKAGE_PATH", value_name = "DIR")]
pub package_path: Option<PathBuf>,
#[clap(
long = "package-cache-path",
env = "TYPST_PACKAGE_CACHE_PATH",
value_name = "DIR"
)]
pub package_cache_path: Option<PathBuf>,
}
#[derive(Debug, Clone, Parser)]
pub struct FontArgs {
#[clap(
long = "font-path",
env = "TYPST_FONT_PATHS",
value_name = "DIR",
value_delimiter = ENV_PATH_SEP,
)]
pub font_paths: Vec<PathBuf>,
#[arg(long)]
pub ignore_system_fonts: bool,
}
#[cfg(feature = "http-server")]
#[derive(Debug, Clone, Parser)]
pub struct ServerArgs {
#[clap(long)]
pub no_serve: bool,
#[clap(long)]
pub no_reload: bool,
#[clap(long)]
pub port: Option<u16>,
}
macro_rules! display_possible_values {
($ty:ty) => {
impl Display for $ty {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
self.to_possible_value()
.expect("no values are skipped")
.get_name()
.fmt(f)
}
}
};
}
#[derive(Debug, Clone)]
pub enum Input {
Stdin,
Path(PathBuf),
}
impl Display for Input {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
match self {
Input::Stdin => f.pad("stdin"),
Input::Path(path) => path.display().fmt(f),
}
}
}
#[derive(Debug, Clone)]
pub enum Output {
Stdout,
Path(PathBuf),
}
impl Display for Output {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
match self {
Output::Stdout => f.pad("stdout"),
Output::Path(path) => path.display().fmt(f),
}
}
}
#[derive(Debug, Copy, Clone, Eq, PartialEq, Ord, PartialOrd, ValueEnum)]
pub enum OutputFormat {
Pdf,
Png,
Svg,
Html,
}
display_possible_values!(OutputFormat);
#[derive(Debug, Default, Copy, Clone, Eq, PartialEq, Ord, PartialOrd, ValueEnum)]
pub enum DiagnosticFormat {
#[default]
Human,
Short,
}
display_possible_values!(DiagnosticFormat);
#[derive(Debug, Copy, Clone, Eq, PartialEq, ValueEnum)]
pub enum Feature {
Html,
}
display_possible_values!(Feature);
#[derive(Debug, Copy, Clone, Eq, PartialEq, ValueEnum)]
#[allow(non_camel_case_types)]
pub enum PdfStandard {
#[value(name = "1.7")]
V_1_7,
#[value(name = "a-2b")]
A_2b,
#[value(name = "a-3b")]
A_3b,
}
display_possible_values!(PdfStandard);
#[derive(Debug, Default, Copy, Clone, Eq, PartialEq, ValueEnum)]
pub enum SerializationFormat {
#[default]
Json,
Yaml,
}
display_possible_values!(SerializationFormat);
#[derive(Debug, Clone)]
pub struct Pages(pub RangeInclusive<Option<NonZeroUsize>>);
impl FromStr for Pages {
type Err = &'static str;
fn from_str(value: &str) -> Result<Self, Self::Err> {
match value.split('-').map(str::trim).collect::<Vec<_>>().as_slice() {
[] | [""] => Err("page export range must not be empty"),
[single_page] => {
let page_number = parse_page_number(single_page)?;
Ok(Pages(Some(page_number)..=Some(page_number)))
}
["", ""] => Err("page export range must have start or end"),
[start, ""] => Ok(Pages(Some(parse_page_number(start)?)..=None)),
["", end] => Ok(Pages(None..=Some(parse_page_number(end)?))),
[start, end] => {
let start = parse_page_number(start)?;
let end = parse_page_number(end)?;
if start > end {
Err("page export range must end at a page after the start")
} else {
Ok(Pages(Some(start)..=Some(end)))
}
}
[_, _, _, ..] => Err("page export range must have a single hyphen"),
}
}
}
fn parse_page_number(value: &str) -> Result<NonZeroUsize, &'static str> {
if value == "0" {
Err("page numbers start at one")
} else {
NonZeroUsize::from_str(value).map_err(|_| "not a valid page number")
}
}
fn input_value_parser() -> impl TypedValueParser<Value = Input> {
clap::builder::OsStringValueParser::new().try_map(|value| {
if value.is_empty() {
Err(clap::Error::new(clap::error::ErrorKind::InvalidValue))
} else if value == "-" {
Ok(Input::Stdin)
} else {
Ok(Input::Path(value.into()))
}
})
}
fn output_value_parser() -> impl TypedValueParser<Value = Output> {
clap::builder::OsStringValueParser::new().try_map(|value| {
if value.is_empty() {
Err(clap::Error::new(clap::error::ErrorKind::InvalidValue))
} else if value == "-" {
Ok(Output::Stdout)
} else {
Ok(Output::Path(value.into()))
}
})
}
fn parse_sys_input_pair(raw: &str) -> Result<(String, String), String> {
let (key, val) = raw
.split_once('=')
.ok_or("input must be a key and a value separated by an equal sign")?;
let key = key.trim().to_owned();
if key.is_empty() {
return Err("the key was missing or empty".to_owned());
}
let val = val.trim().to_owned();
Ok((key, val))
}
fn parse_source_date_epoch(raw: &str) -> Result<DateTime<Utc>, String> {
let timestamp: i64 = raw
.parse()
.map_err(|err| format!("timestamp must be decimal integer ({err})"))?;
DateTime::from_timestamp(timestamp, 0)
.ok_or_else(|| "timestamp out of range".to_string())
}