diff options
author | Kevin Gimbel <hallo@kevingimbel.com> | 2019-11-19 13:28:05 +0100 |
---|---|---|
committer | Kevin Gimbel <hallo@kevingimbel.com> | 2019-11-19 13:28:05 +0100 |
commit | 464d5ec190ce1a35f18a40a10206d3e9fb449e6d (patch) | |
tree | b78fe0ebb11192a1e1a068d5320e09898b2e73f9 | |
parent | 901fa5a9d1ad87376154ab39e3e132f463282832 (diff) |
Error handling and new test files
-rw-r--r-- | README.md | 16 | ||||
-rw-r--r-- | src/bin.rs | 33 | ||||
-rw-r--r-- | src/lib.rs | 14 | ||||
-rw-r--r-- | tests/files/README-read_no_write.md | 10 |
4 files changed, 53 insertions, 20 deletions
@@ -1,15 +1,15 @@ # `mktoc` -> +> Blazingly fast Table of Content generator <!-- BEGIN mktoc --> - [`mktoc`](#`mktoc`) -- [About](#About) -- [Installation](#Installation) - - [Cargo](#Cargo) - - [Binary](#Binary) -- [Usage](#Usage) -- [mktoc [--write] <FILE>](#mktoc-[--write]-<FILE>) -- [Performance](#Performance) +- [About](#about) +- [Installation](#installation) + - [Cargo](#cargo) + - [Binary](#binary) +- [Usage](#usage) +- [mktoc [--write] <FILE>](#mktoc-[--write]-<file>) +- [Performance](#performance) <!-- END mktoc --> ## About @@ -10,15 +10,36 @@ struct Cli { write: bool, } -fn main() -> std::io::Result<()> { +fn handle_write(new_toc: String) { let opts = Cli::from_args(); - let file = &opts.file.to_owned(); - let res = mktoc::make_toc(file.to_string()); if opts.write { - std::fs::write(file, res.as_bytes())?; + let res_write = std::fs::write(opts.file, new_toc.as_bytes()); + match res_write { + Ok(_r) => { + std::process::exit(0); + } + Err(e) => { + eprintln!("Failed to write file. Error kind: {:?}", e.kind()); + std::process::exit(1); + } + } } else { - println!("{}", res); + println!("{}", new_toc); } +} + +fn main() { + let opts = Cli::from_args(); + let res = mktoc::make_toc(opts.file.to_string()); - Ok(()) + match res { + Ok(new_toc) => { + handle_write(new_toc); + std::process::exit(0); + } + Err(e) => { + eprintln!("Error: {}", e); + std::process::exit(1); + } + }; } @@ -33,8 +33,8 @@ fn generate_toc(original_content: String) -> String { let level: usize = caps.get(2).unwrap().as_str().chars().count() - 1; let text = caps.get(3).unwrap().as_str(); // @TODO: Use real URL encoding - let link = text.replace(" ", "-"); - // let spaces = " ".repeat(level - 1); + let link = text.replace(" ", "-").to_ascii_lowercase(); + // let spaces = " ".repeat(level -1); let spaces = match level { 1 | 2 => String::from(""), 3 => String::from(" "), @@ -58,11 +58,13 @@ fn generate_toc(original_content: String) -> String { return new_toc; } -pub fn make_toc(file_path_in: String) -> String { - let content = read_file(file_path_in).unwrap(); +pub fn make_toc(file_path_in: String) -> Result<String, ::std::io::Error> { + let content = read_file(file_path_in)?; let new_toc = generate_toc(content.to_owned()); let re_toc = regex::Regex::new(r"(?ms)^(<!-- BEGIN mktoc).*(END mktoc -->)").unwrap(); - let res = re_toc.replace_all(content.as_str(), new_toc.as_str()); + let res: String = re_toc + .replace_all(content.as_str(), new_toc.as_str()) + .into_owned(); - return res.into_owned(); + Ok(res) } diff --git a/tests/files/README-read_no_write.md b/tests/files/README-read_no_write.md new file mode 100644 index 0000000..303b683 --- /dev/null +++ b/tests/files/README-read_no_write.md @@ -0,0 +1,10 @@ +# Test +<!-- BEGIN mktoc --> + +<!-- END mktoc --> + +## Hello world! + +## ¡Hola mundo! + +## Hallo Welt!
\ No newline at end of file |