From 7ec7c49144788bd61e61b90aa1fa1a893a5962c9 Mon Sep 17 00:00:00 2001 From: Laurenz Date: Thu, 5 Nov 2020 13:07:13 +0100 Subject: [PATCH] =?UTF-8?q?Don't=20panic=20=F0=9F=8C=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Cargo.toml | 5 +++-- src/main.rs | 17 ++++++++++------- 2 files changed, 13 insertions(+), 9 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index b31e9dbc5..9d08c3522 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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" diff --git a/src/main.rs b/src/main.rs index 17dc67c48..6bd5a9b4b 100644 --- a/src/main.rs +++ b/src/main.rs @@ -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(()) }