diff options
| author | Laurenz <laurmaedje@gmail.com> | 2021-05-29 15:45:57 +0200 |
|---|---|---|
| committer | Laurenz <laurmaedje@gmail.com> | 2021-05-31 22:33:40 +0200 |
| commit | e023bf2ac9f5796355d9485afc16781196bf212b (patch) | |
| tree | 26d4487de0c4e2d0f69182483301de867cb5fa34 /src/main.rs | |
| parent | 9f77f09aacd1fb0fd6138a6d16ed2755f6bfae3f (diff) | |
Module loading system
Detects cyclic imports and loads each module only once per compilation.
Diffstat (limited to 'src/main.rs')
| -rw-r--r-- | src/main.rs | 43 |
1 files changed, 30 insertions, 13 deletions
diff --git a/src/main.rs b/src/main.rs index 5370f6a8..449cad20 100644 --- a/src/main.rs +++ b/src/main.rs @@ -3,37 +3,53 @@ use std::path::{Path, PathBuf}; use anyhow::{anyhow, bail, Context}; +use typst::loading::Loader; + fn main() -> anyhow::Result<()> { let args: Vec<_> = std::env::args().collect(); if args.len() < 2 || args.len() > 3 { - println!("Usage: typst src.typ [out.pdf]"); + println!("usage: typst src.typ [out.pdf]"); return Ok(()); } + // Create a loader for fonts and files. + let mut loader = typst::loading::FsLoader::new(); + loader.search_path("fonts"); + loader.search_system(); + + // Resolve the canonical path because the compiler needs it for module + // loading. let src_path = Path::new(&args[1]); + + // Find out the file name to create the output file. + let name = src_path + .file_name() + .ok_or_else(|| anyhow!("source path is not a file"))?; + let dest_path = if args.len() <= 2 { - let name = src_path - .file_name() - .ok_or_else(|| anyhow!("Source path is not a file."))?; Path::new(name).with_extension("pdf") } else { PathBuf::from(&args[2]) }; - if src_path == dest_path { - bail!("Source and destination path are the same."); + // Ensure that the source file is not overwritten. + let src_hash = loader.resolve(&src_path); + let dest_hash = loader.resolve(&dest_path); + if src_hash.is_some() && src_hash == dest_hash { + bail!("source and destination files are the same"); } - let src = fs::read_to_string(src_path).context("Failed to read from source file.")?; - - let mut loader = typst::loading::FsLoader::new(); - loader.search_path("fonts"); - loader.search_system(); + // Read the source. + let src = fs::read_to_string(&src_path) + .map_err(|_| anyhow!("failed to read source file"))?; + // Compile. let mut cache = typst::cache::Cache::new(&loader); let scope = typst::library::new(); let state = typst::exec::State::default(); - let pass = typst::typeset(&mut loader, &mut cache, &src, &scope, state); + let pass = typst::typeset(&mut loader, &mut cache, &src_path, &src, &scope, state); + + // Print diagnostics. let map = typst::parse::LineMap::new(&src); for diag in pass.diags { let start = map.location(diag.span.start).unwrap(); @@ -48,8 +64,9 @@ fn main() -> anyhow::Result<()> { ); } + // Export the PDF. let buffer = typst::export::pdf(&cache, &pass.output); - fs::write(&dest_path, buffer).context("Failed to write PDF file.")?; + fs::write(&dest_path, buffer).context("failed to write PDF file")?; Ok(()) } |
