Don't panic 🌶

This commit is contained in:
Laurenz 2020-11-05 13:07:13 +01:00
parent e26b431bee
commit 7ec7c49144
2 changed files with 13 additions and 9 deletions

View File

@ -5,7 +5,7 @@ authors = ["The Typst Project Developers"]
edition = "2018"
[features]
default = ["fs"]
default = ["fs", "anyhow"]
fs = ["fontdock/fs"]
[dependencies]
@ -16,6 +16,7 @@ unicode-xid = "0.2"
# feature = "serde"
serde = { version = "1", features = ["derive"], optional = true }
anyhow = { version = "1", optional = true }
[dev-dependencies]
criterion = "0.3"
@ -33,7 +34,7 @@ bench = false
[[bin]]
name = "typst"
required-features = ["fs"]
required-features = ["fs", "anyhow"]
[[test]]
name = "typeset"

View File

@ -4,6 +4,7 @@ use std::io::BufWriter;
use std::path::{Path, PathBuf};
use std::rc::Rc;
use anyhow::{anyhow, bail, Context};
use fontdock::fs::{FsIndex, FsSource};
use typst::diag::{Feedback, Pass};
@ -13,28 +14,28 @@ use typst::font::FontLoader;
use typst::parse::LineMap;
use typst::typeset;
fn main() {
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]");
return;
return Ok(());
}
let src_path = Path::new(&args[1]);
let dest_path = if args.len() <= 2 {
let name = src_path
.file_name()
.expect("source path is not a file");
.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 {
panic!("source and destination path are the same");
bail!("Source and destination path are the same.");
}
let src = read_to_string(src_path).expect("failed to read from source file");
let src = read_to_string(src_path).context("Failed to read from source file.")?;
let mut index = FsIndex::new();
index.search_dir("fonts");
@ -72,7 +73,9 @@ fn main() {
}
let loader = loader.borrow();
let file = File::create(&dest_path).expect("failed to create output file");
let file = File::create(&dest_path).context("Failed to create output file.")?;
let writer = BufWriter::new(file);
pdf::export(&layouts, &loader, writer).expect("failed to export pdf");
pdf::export(&layouts, &loader, writer).context("Failed to export pdf.")?;
Ok(())
}