summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLaurenz <laurmaedje@gmail.com>2020-11-05 13:07:13 +0100
committerLaurenz <laurmaedje@gmail.com>2020-11-05 13:07:13 +0100
commit7ec7c49144788bd61e61b90aa1fa1a893a5962c9 (patch)
tree11979d63607599a12fa86402409c56e8d589351d
parente26b431beef22cf99a53035df030f5eaab7c65ce (diff)
Don't panic 🌶
-rw-r--r--Cargo.toml5
-rw-r--r--src/main.rs17
2 files changed, 13 insertions, 9 deletions
diff --git a/Cargo.toml b/Cargo.toml
index b31e9dbc..9d08c352 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 17dc67c4..6bd5a9b4 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(())
}