diff options
| author | Matt Fellenz <matt@felle.nz> | 2023-08-23 13:44:03 -0700 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2023-08-23 22:44:03 +0200 |
| commit | ff5dc9191e15d032c8e87065c68c232c650419a8 (patch) | |
| tree | 5a9cbbbe3070f101ed3d118ca709d1bc6370402b /crates/typst-cli/src/compile.rs | |
| parent | 3fcb5ea73cbae20500f4695685df4caf0071d45a (diff) | |
Add format arg to CLI (#1985)
Diffstat (limited to 'crates/typst-cli/src/compile.rs')
| -rw-r--r-- | crates/typst-cli/src/compile.rs | 43 |
1 files changed, 33 insertions, 10 deletions
diff --git a/crates/typst-cli/src/compile.rs b/crates/typst-cli/src/compile.rs index 5b51f422..4c4c24be 100644 --- a/crates/typst-cli/src/compile.rs +++ b/crates/typst-cli/src/compile.rs @@ -1,5 +1,5 @@ use std::fs; -use std::path::Path; +use std::path::{Path, PathBuf}; use codespan_reporting::diagnostic::{Diagnostic, Label}; use codespan_reporting::term::{self, termcolor}; @@ -11,7 +11,7 @@ use typst::geom::Color; use typst::syntax::{FileId, Source}; use typst::World; -use crate::args::{CompileCommand, DiagnosticFormat}; +use crate::args::{CompileCommand, DiagnosticFormat, OutputFormat}; use crate::watch::Status; use crate::world::SystemWorld; use crate::{color_stream, set_failed}; @@ -19,6 +19,33 @@ use crate::{color_stream, set_failed}; type CodespanResult<T> = Result<T, CodespanError>; type CodespanError = codespan_reporting::files::Error; +impl CompileCommand { + /// The output path. + pub fn output(&self) -> PathBuf { + self.output + .clone() + .unwrap_or_else(|| self.common.input.with_extension("pdf")) + } + + /// The format to use for generated output, either specified by the user or inferred from the extension. + /// + /// Will return `Err` if the format was not specified and could not be inferred. + pub fn output_format(&self) -> StrResult<OutputFormat> { + Ok(if let Some(specified) = self.format { + specified + } else if let Some(output) = &self.output { + match output.extension() { + Some(ext) if ext.eq_ignore_ascii_case("pdf") => OutputFormat::Pdf, + Some(ext) if ext.eq_ignore_ascii_case("png") => OutputFormat::Png, + Some(ext) if ext.eq_ignore_ascii_case("svg") => OutputFormat::Svg, + _ => bail!("could not infer output format for path {}.\nconsider providing the format manually with `--format/-f`", output.display()), + } + } else { + OutputFormat::Pdf + }) + } +} + /// Execute a compilation command. pub fn compile(mut command: CompileCommand) -> StrResult<()> { let mut world = SystemWorld::new(&command.common)?; @@ -97,14 +124,10 @@ pub fn compile_once( /// Export into the target format. fn export(document: &Document, command: &CompileCommand) -> StrResult<()> { - match command.output().extension() { - Some(ext) if ext.eq_ignore_ascii_case("png") => { - export_image(document, command, ImageExportFormat::Png) - } - Some(ext) if ext.eq_ignore_ascii_case("svg") => { - export_image(document, command, ImageExportFormat::Svg) - } - _ => export_pdf(document, command), + match command.output_format()? { + OutputFormat::Png => export_image(document, command, ImageExportFormat::Png), + OutputFormat::Svg => export_image(document, command, ImageExportFormat::Svg), + OutputFormat::Pdf => export_pdf(document, command), } } |
