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" edition = "2018"
[features] [features]
default = ["fs"] default = ["fs", "anyhow"]
fs = ["fontdock/fs"] fs = ["fontdock/fs"]
[dependencies] [dependencies]
@ -16,6 +16,7 @@ unicode-xid = "0.2"
# feature = "serde" # feature = "serde"
serde = { version = "1", features = ["derive"], optional = true } serde = { version = "1", features = ["derive"], optional = true }
anyhow = { version = "1", optional = true }
[dev-dependencies] [dev-dependencies]
criterion = "0.3" criterion = "0.3"
@ -33,7 +34,7 @@ bench = false
[[bin]] [[bin]]
name = "typst" name = "typst"
required-features = ["fs"] required-features = ["fs", "anyhow"]
[[test]] [[test]]
name = "typeset" name = "typeset"

View File

@ -4,6 +4,7 @@ use std::io::BufWriter;
use std::path::{Path, PathBuf}; use std::path::{Path, PathBuf};
use std::rc::Rc; use std::rc::Rc;
use anyhow::{anyhow, bail, Context};
use fontdock::fs::{FsIndex, FsSource}; use fontdock::fs::{FsIndex, FsSource};
use typst::diag::{Feedback, Pass}; use typst::diag::{Feedback, Pass};
@ -13,28 +14,28 @@ use typst::font::FontLoader;
use typst::parse::LineMap; use typst::parse::LineMap;
use typst::typeset; use typst::typeset;
fn main() { fn main() -> anyhow::Result<()> {
let args: Vec<_> = std::env::args().collect(); let args: Vec<_> = std::env::args().collect();
if args.len() < 2 || args.len() > 3 { if args.len() < 2 || args.len() > 3 {
println!("Usage: typst src.typ [out.pdf]"); println!("Usage: typst src.typ [out.pdf]");
return; return Ok(());
} }
let src_path = Path::new(&args[1]); let src_path = Path::new(&args[1]);
let dest_path = if args.len() <= 2 { let dest_path = if args.len() <= 2 {
let name = src_path let name = src_path
.file_name() .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") Path::new(name).with_extension("pdf")
} else { } else {
PathBuf::from(&args[2]) PathBuf::from(&args[2])
}; };
if src_path == dest_path { 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(); let mut index = FsIndex::new();
index.search_dir("fonts"); index.search_dir("fonts");
@ -72,7 +73,9 @@ fn main() {
} }
let loader = loader.borrow(); 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); 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(())
} }