summaryrefslogtreecommitdiff
path: root/src/main.rs
diff options
context:
space:
mode:
authorLaurenz <laurmaedje@gmail.com>2021-07-20 20:21:56 +0200
committerLaurenz <laurmaedje@gmail.com>2021-07-21 11:28:29 +0200
commit9488b1b850152eb564dbfefc898c962bdac73eb4 (patch)
tree0a99487cddfee1a46b5802dc6b64c81b70e549da /src/main.rs
parent8000783f95ee007d9dda6f1dcc1c42c8e607b122 (diff)
Main context struct
Diffstat (limited to 'src/main.rs')
-rw-r--r--src/main.rs25
1 files changed, 11 insertions, 14 deletions
diff --git a/src/main.rs b/src/main.rs
index 03680204..f9da37fa 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -1,5 +1,6 @@
use std::fs;
use std::path::{Path, PathBuf};
+use std::rc::Rc;
use anyhow::{anyhow, bail, Context};
use same_file::is_same_file;
@@ -11,11 +12,6 @@ fn main() -> anyhow::Result<()> {
return Ok(());
}
- // Create a loader for fonts and files.
- let mut loader = typst::loading::FsLoader::new();
- loader.search_path("fonts");
- loader.search_system();
-
// Determine source and destination path.
let src_path = Path::new(&args[1]);
let dest_path = if let Some(arg) = args.get(2) {
@@ -33,18 +29,19 @@ fn main() -> anyhow::Result<()> {
bail!("source and destination files are the same");
}
- // Resolve the file id of the source file.
- let src_id = loader.resolve_path(src_path).context("source file not found")?;
+ // Create a loader for fonts and files.
+ let mut loader = typst::loading::FsLoader::new();
+ loader.search_path("fonts");
+ loader.search_system();
- // Read the source.
+ // Resolve the file id of the source file and read the file.
+ let src_id = loader.resolve_path(src_path).context("source file not found")?;
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_id, &src, &scope, state);
+ // Typeset.
+ let mut ctx = typst::Context::new(Rc::new(loader));
+ let pass = ctx.typeset(src_id, &src);
// Print diagnostics.
let map = typst::parse::LineMap::new(&src);
@@ -62,7 +59,7 @@ fn main() -> anyhow::Result<()> {
}
// Export the PDF.
- let buffer = typst::export::pdf(&cache, &pass.output);
+ let buffer = typst::export::pdf(&ctx, &pass.output);
fs::write(&dest_path, buffer).context("failed to write PDF file")?;
Ok(())