diff options
author | Nick Gerace <nickagerace@gmail.com> | 2020-10-10 22:57:12 -0400 |
---|---|---|
committer | Nick Gerace <nickagerace@gmail.com> | 2020-10-10 23:04:31 -0400 |
commit | 13afbbf10f3b8506b4321d6026cacdea5f775215 (patch) | |
tree | d33e28e06de67981238a8ccab47f795c588e5e8b | |
parent | fe195fffdc878a94c62fb97bc64fc5a0b834e697 (diff) | |
download | gfold-13afbbf10f3b8506b4321d6026cacdea5f775215.zip |
Add eyre for simple backtrace reporting
Add eyre for simple backtrace reporting. Since gfold relies on speed and
efficiency, and sports a relatively small codebase, eyre provides a
simple backtrace report when an error is encountered. Specifically, the
filename and line are the most important parts.
Unrelated, add a cargo doc command to the release Make target.
Adjust existing docs comments to match style.
Change some private definitions to be public.
-rw-r--r-- | Cargo.lock | 23 | ||||
-rw-r--r-- | Cargo.toml | 1 | ||||
-rw-r--r-- | Makefile | 8 | ||||
-rw-r--r-- | src/lib.rs | 22 | ||||
-rw-r--r-- | src/main.rs | 10 |
5 files changed, 45 insertions, 19 deletions
@@ -166,6 +166,16 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a357d28ed41a50f9c765dbfe56cbc04a64e53e5fc58ba79fbc34c10ef3df831f" [[package]] +name = "eyre" +version = "0.6.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f4c5cb4dc433c59f09df4b4450f649cbfed61e8a3505abe32e4154066439157e" +dependencies = [ + "indenter", + "once_cell", +] + +[[package]] name = "getrandom" version = "0.1.14" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -180,6 +190,7 @@ dependencies = [ name = "gfold" version = "0.5.2" dependencies = [ + "eyre", "git2", "prettytable-rs", "structopt", @@ -230,6 +241,12 @@ dependencies = [ ] [[package]] +name = "indenter" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e0bd112d44d9d870a6819eb505d04dd92b5e4d94bb8c304924a0872ae7016fb5" + +[[package]] name = "itoa" version = "0.4.6" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -318,6 +335,12 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3728d817d99e5ac407411fa471ff9800a778d88a24685968b36824eaf4bee400" [[package]] +name = "once_cell" +version = "1.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "260e51e7efe62b592207e9e13a68e43692a7a279171d6ba57abd208bf23645ad" + +[[package]] name = "openssl-probe" version = "0.1.2" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -5,6 +5,7 @@ authors = ["Nick Gerace <nickagerace@gmail.com>"] edition = "2018"
[dependencies]
+eyre = "0.6"
structopt = "0.3"
git2 = "0.13"
prettytable-rs = "0.8"
@@ -49,12 +49,14 @@ fixme: FIXME $(MAKEPATH) release: - @printf "Change version at the following locations...\n" + @printf "[1] Change version at the following locations...\n" @printf " Makefile: $(shell grep $(VERSION) $(MAKEPATH)/Makefile)\n" @printf " README.md: $(shell grep $(VERSION) $(MAKEPATH)/README.md)\n" @printf " CHANGELOG.md: $(shell grep $(VERSION) $(MAKEPATH)/CHANGELOG.md)\n" @printf " Cargo.toml: $(shell grep $(VERSION) $(MAKEPATH)/Cargo.toml)\n" - @printf "Uncomment the unreleased string in CHANGELOG.md...\n" + @printf "[2] Uncomment the unreleased string in CHANGELOG.md...\n" @printf " <!--The latest version contains all changes.-->\n" - @printf "Then, run the following command...\n" + @printf "[3] Run the following command to check documentation...\n" + @printf " cargo doc --open\n" + @printf "[4] Then, run the following command...\n" @printf " time make build-release\n" @@ -9,18 +9,19 @@ extern crate prettytable; use std::cmp::Ordering; -use std::error::Error; use std::fs; use std::path::Path; use std::path::PathBuf; +use eyre::Result; + /// Creating a ```Results``` object requires using this ```struct``` as a pre-requisite. -struct Config { +pub struct Config { recursive: bool, skip_sort: bool, } -/// This ```struct``` is a wrapper around the ```prettytable::Table``` object. +/// This private ```struct``` is a wrapper around the ```prettytable::Table``` object. /// It exists to provide a label for the table. struct TableWrapper { path_string: String, @@ -28,11 +29,11 @@ struct TableWrapper { } /// Contains all tables with results for each directory. -struct Results(Vec<TableWrapper>); +pub struct Results(Vec<TableWrapper>); impl Results { /// Create a new ```Results``` object with a given path and config. - fn new(path: &Path, config: &Config) -> Result<Results, Box<dyn Error>> { + pub fn new(path: &Path, config: &Config) -> Result<Results> { let mut results = Results(Vec::new()); results.execute_in_directory(&config, path)?; if !&config.skip_sort { @@ -42,8 +43,8 @@ impl Results { } /// Load results into the calling ```Results``` object via a given path and config. - /// This function may be called recursively. - fn execute_in_directory(&mut self, config: &Config, dir: &Path) -> Result<(), Box<dyn Error>> { + /// This private function may be called recursively. + fn execute_in_directory(&mut self, config: &Config, dir: &Path) -> Result<()> { // FIXME: find ways to add concurrent programming (tokio, async, etc.) to this section. let path_entries = fs::read_dir(dir)?; let mut repos = Vec::new(); @@ -72,7 +73,7 @@ impl Results { /// Sort the results alphabetically using ```sort_by_key```. /// This function will only perform the sort if there are at least two ```TableWrapper``` objects. - fn sort_results(&mut self) { + pub fn sort_results(&mut self) { if self.0.len() >= 2 { // FIXME: find a way to do this without "clone()". self.0.sort_by_key(|table| table.path_string.clone()); @@ -81,7 +82,7 @@ impl Results { /// Iterate through every table and print each to STDOUT. /// If there is only one table, this function avoids using a loop. - fn print_results(self) { + pub fn print_results(self) { match self.0.len().cmp(&1) { Ordering::Greater => { for table_wrapper in self.0 { @@ -100,6 +101,7 @@ impl Results { } /// Create a ```TableWrapper``` object from a given vector of paths (```Vec<PathBuf>```). +/// This is a private helper function for ```execute_in_directory```. fn create_table_from_paths(repos: Vec<PathBuf>, path: &Path) -> Option<TableWrapper> { // For every path, we will create a mutable Table containing its results. let mut table = prettytable::Table::new(); @@ -181,7 +183,7 @@ fn create_table_from_paths(repos: Vec<PathBuf>, path: &Path) -> Option<TableWrap } /// This function is the primary driver for this file, ```lib.rs```. -pub fn run(path: &Path, recursive: bool, skip_sort: bool) -> Result<(), Box<dyn Error>> { +pub fn run(path: &Path, recursive: bool, skip_sort: bool) -> Result<()> { let config = Config { recursive, skip_sort, diff --git a/src/main.rs b/src/main.rs index 887bfc8..d13c1ff 100644 --- a/src/main.rs +++ b/src/main.rs @@ -7,8 +7,8 @@ use std::env; use std::path::PathBuf; -use std::process; +use eyre::Result; use structopt::StructOpt; #[derive(StructOpt, Debug)] @@ -30,7 +30,7 @@ struct Opt { /// This file, ```main.rs```, serves as the primary driver for the ```gfold``` library. /// It is intended to be used as a command-line interface. -fn main() { +fn main() -> Result<()> { let mut path = env::current_dir().expect("failed to get CWD"); let opt = Opt::from_args(); @@ -39,8 +39,6 @@ fn main() { }; path = path.canonicalize().expect("failed to canonicalize path"); - if let Err(error) = gfold::run(&path, opt.recursive, opt.skip_sort) { - eprintln!("Encountered fatal error: {}", error); - process::exit(1); - }; + gfold::run(&path, opt.recursive, opt.skip_sort)?; + Ok(()) } |