diff options
| author | Laurenz <laurmaedje@gmail.com> | 2019-05-26 21:59:33 +0200 |
|---|---|---|
| committer | Laurenz <laurmaedje@gmail.com> | 2019-05-26 21:59:33 +0200 |
| commit | c38e17d91f81632422171b8103ce90baacfdbe22 (patch) | |
| tree | 757387b885bd5f0785dbafbf1704fb280dd4a269 /src/bin/main.rs | |
| parent | b3734bbc046fe7b14cff54e2dae7014a71014777 (diff) | |
Thoroughly improve documentation 📝
Diffstat (limited to 'src/bin/main.rs')
| -rw-r--r-- | src/bin/main.rs | 38 |
1 files changed, 23 insertions, 15 deletions
diff --git a/src/bin/main.rs b/src/bin/main.rs index 9e1403b2..6ca1ba56 100644 --- a/src/bin/main.rs +++ b/src/bin/main.rs @@ -1,16 +1,25 @@ use std::env; -use std::fs::File; use std::error::Error; -use std::process; +use std::fs::File; use std::io::Read; use std::path::{Path, PathBuf}; +use std::process; use typeset::Typesetter; use typeset::{font::FileSystemFontProvider, font_info}; use typeset::export::pdf::PdfExporter; +fn main() { + if let Err(err) = run() { + eprintln!("error: {}", err); + process::exit(1); + } +} + +/// The actual main function. fn run() -> Result<(), Box<Error>> { + // Check the command line arguments. let args: Vec<String> = env::args().collect(); if args.len() < 2 || args.len() > 3 { help_and_quit(); @@ -19,9 +28,10 @@ fn run() -> Result<(), Box<Error>> { // Open the input file. let mut file = File::open(&args[1]).map_err(|_| "failed to open source file")?; - // The output file name. - let output_filename = if args.len() <= 2 { - let source_path = Path::new(&args[1]); + let source_path = Path::new(&args[1]); + + // Compute the output filename from the input filename by replacing the extension. + let dest_path = if args.len() <= 2 { let stem = source_path.file_stem().ok_or_else(|| "missing destation file name")?; let base = source_path.parent().ok_or_else(|| "missing destation folder")?; base.join(format!("{}.pdf", stem.to_string_lossy())) @@ -29,6 +39,11 @@ fn run() -> Result<(), Box<Error>> { PathBuf::from(&args[2]) }; + // We do not want to overwrite the source file. + if dest_path == source_path { + return Err("source and destination path are the same".into()); + } + // Read the input file. let mut src = String::new(); file.read_to_string(&mut src).map_err(|_| "failed to read from source file")?; @@ -50,22 +65,15 @@ fn run() -> Result<(), Box<Error>> { // Export the document into a PDF file. let exporter = PdfExporter::new(); - let output_file = File::create(&output_filename)?; + let output_file = File::create(&dest_path)?; exporter.export(&document, output_file)?; Ok(()) } -fn main() { - if let Err(err) = run() { - eprintln!("error: {}", err); - process::exit(1); - } -} - /// Print a usage message and quit. fn help_and_quit() { - let name = env::args().next().unwrap_or("help".to_string()); - println!("usage: {} <source> [<destination>]", name); + let name = env::args().next().unwrap_or("typeset".to_string()); + println!("usage: {} source [destination]", name); process::exit(0); } |
