summaryrefslogtreecommitdiff
path: root/src/bin/main.rs
diff options
context:
space:
mode:
authorLaurenz <laurmaedje@gmail.com>2019-12-12 22:19:38 +0100
committerLaurenz <laurmaedje@gmail.com>2019-12-12 22:19:38 +0100
commitff107cf3e75acf041f8b7631337d299cdeaa1685 (patch)
tree40799f0337a5c2bc13166ff32a1f0b4f5c23bfe8 /src/bin/main.rs
parent3c0496bb6104f0e2a60520e42137ecc29f26e9fa (diff)
Tidying up 🧹
Diffstat (limited to 'src/bin/main.rs')
-rw-r--r--src/bin/main.rs56
1 files changed, 19 insertions, 37 deletions
diff --git a/src/bin/main.rs b/src/bin/main.rs
index f5e4a844..a2b63d6d 100644
--- a/src/bin/main.rs
+++ b/src/bin/main.rs
@@ -1,67 +1,49 @@
-use std::env;
-use std::error::Error;
-use std::fs::File;
-use std::io::{BufWriter, Read};
+use std::fs::{File, read_to_string};
+use std::io::BufWriter;
use std::path::{Path, PathBuf};
-use std::process;
-use typstc::export::pdf::PdfExporter;
-use typstc::toddle::query::FileSystemFontProvider;
use typstc::Typesetter;
+use typstc::toddle::query::FileSystemFontProvider;
+use typstc::export::pdf::PdfExporter;
fn main() {
if let Err(err) = run() {
eprintln!("error: {}", err);
- process::exit(1);
+ std::process::exit(1);
}
}
-fn run() -> Result<(), Box<dyn Error>> {
- let args: Vec<String> = env::args().collect();
+fn run() -> Result<(), Box<dyn std::error::Error>> {
+ let args: Vec<String> = std::env::args().collect();
if args.len() < 2 || args.len() > 3 {
- help_and_quit();
+ println!("usage: {} source [destination]",
+ args.first().map(|s| s.as_str()).unwrap_or("typst"));
+ std::process::exit(0);
}
- let source_path = Path::new(&args[1]);
- let dest_path = if args.len() <= 2 {
- source_path.with_extension("pdf")
+ let source = Path::new(&args[1]);
+ let dest = if args.len() <= 2 {
+ source.with_extension("pdf")
} else {
PathBuf::from(&args[2])
};
- if dest_path == source_path {
- return err("source and destination path are the same");
+ if source == dest {
+ Err("source and destination path are the same")?;
}
- let mut source_file = File::open(source_path)
- .map_err(|_| "failed to open source file")?;
-
- let mut src = String::new();
- source_file
- .read_to_string(&mut src)
+ let src = read_to_string(source)
.map_err(|_| "failed to read from source file")?;
let mut typesetter = Typesetter::new();
let provider = FileSystemFontProvider::from_listing("fonts/fonts.toml").unwrap();
typesetter.add_font_provider(provider);
- let document = typesetter.typeset(&src)?;
+ let layouts = typesetter.typeset(&src)?;
let exporter = PdfExporter::new();
- let dest_file = File::create(&dest_path)?;
- exporter.export(&document, typesetter.loader(), BufWriter::new(dest_file))?;
+ let writer = BufWriter::new(File::create(&dest)?);
+ exporter.export(&layouts, typesetter.loader(), writer)?;
Ok(())
}
-
-/// Construct an error `Result` from a message.
-fn err<S: Into<String>, T>(message: S) -> Result<T, Box<dyn Error>> {
- Err(message.into().into())
-}
-
-/// Print a usage message and exit the process.
-fn help_and_quit() {
- let name = env::args().next().unwrap_or("typst".to_string());
- println!("usage: {} source [destination]", name);
- process::exit(0);
-}