From 9909988d690e210751b2102c16bf419665e8bc01 Mon Sep 17 00:00:00 2001 From: Matan Zruya Date: Sat, 1 Apr 2023 14:59:06 -0400 Subject: [PATCH 1/2] Integrate anyhow --- Cargo.lock | 7 +++++++ Cargo.toml | 1 + src/main.rs | 30 +++++++++++++----------------- src/project.rs | 6 +++--- 4 files changed, 24 insertions(+), 20 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 769d8d7..106997b 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -51,6 +51,12 @@ dependencies = [ "windows-sys", ] +[[package]] +name = "anyhow" +version = "1.0.70" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7de8ce5e0f9f8d88245311066a578d72b7af3e7088f32783804676302df237e4" + [[package]] name = "assert_cmd" version = "2.0.10" @@ -157,6 +163,7 @@ checksum = "8a2dd5a6fe8c6e3502f568a6353e5273bbb15193ad9a89e457b9970798efbea1" name = "codeowners" version = "0.1.0" dependencies = [ + "anyhow", "assert_cmd", "clap", "clap_derive", diff --git a/Cargo.toml b/Cargo.toml index 56b7814..131fd33 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -4,6 +4,7 @@ version = "0.1.0" edition = "2021" [dependencies] +anyhow = "1.0.70" clap = { version = "4.2.1", features = ["derive"] } clap_derive = "4.2.0" glob-match = "0.2.1" diff --git a/src/main.rs b/src/main.rs index d53d563..9a58af9 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,11 +1,10 @@ +use anyhow::{Context, Result}; use ownership::{Ownership, ValidationErrors}; -use tracing::debug; use crate::project::Project; use clap::{Parser, Subcommand}; use path_clean::PathClean; use std::{ - error::Error, fs::File, path::{Path, PathBuf}, process, @@ -47,44 +46,41 @@ struct Args { } impl Args { - fn absolute_project_root(&self) -> Result { - self.project_root.canonicalize() + fn absolute_project_root(&self) -> Result { + self.project_root + .canonicalize() + .with_context(|| format!("Can't canonizalize {}", self.project_root.to_string_lossy())) } - fn absolute_config_path(&self) -> Result { + fn absolute_config_path(&self) -> Result { Ok(self.absolute_path(&self.config_path)?.clean()) } - fn absolute_codeowners_path(&self) -> Result { + fn absolute_codeowners_path(&self) -> Result { Ok(self.absolute_path(&self.codeowners_file_path)?.clean()) } - fn absolute_path(&self, path: &Path) -> Result { + fn absolute_path(&self, path: &Path) -> Result { Ok(self.absolute_project_root()?.join(path)) } } -fn main() -> Result<(), Box> { +fn main() -> Result<()> { install_logger(); print_validation_errors_to_stdout(cli())?; Ok(()) } -fn cli() -> Result<(), Box> { +fn cli() -> Result<()> { let args = Args::parse(); let config_path = args.absolute_config_path()?; let codeowners_file_path = args.absolute_codeowners_path()?; let project_root = args.absolute_project_root()?; - debug!( - config_path = &config_path.to_str(), - codeowners_file_path = &codeowners_file_path.to_str(), - project_root = &project_root.to_str(), - ); - - let config = serde_yaml::from_reader(File::open(config_path)?)?; + let config = + serde_yaml::from_reader(File::open(&config_path).with_context(|| format!("Can't open {}", config_path.to_string_lossy()))?)?; let ownership = Ownership::build(Project::build(&project_root, &codeowners_file_path, &config)?); let command = args.command; @@ -102,7 +98,7 @@ fn cli() -> Result<(), Box> { Ok(()) } -fn print_validation_errors_to_stdout(result: Result<(), Box>) -> Result<(), Box> { +fn print_validation_errors_to_stdout(result: Result<()>) -> Result<()> { if let Err(error) = result { if let Some(validation_errors) = error.downcast_ref::() { println!("{}", validation_errors); diff --git a/src/project.rs b/src/project.rs index aa06929..25911c4 100644 --- a/src/project.rs +++ b/src/project.rs @@ -1,11 +1,11 @@ use std::{ collections::HashMap, - error::Error, fs::File, io::BufRead, path::{Path, PathBuf}, }; +use anyhow::Result; use jwalk::WalkDir; use rayon::prelude::{IntoParallelIterator, ParallelIterator}; use regex::Regex; @@ -111,7 +111,7 @@ mod deserializers { impl Project { #[instrument(level = "debug", skip_all)] - pub fn build(base_path: &Path, codeowners_file_path: &Path, config: &Config) -> Result> { + pub fn build(base_path: &Path, codeowners_file_path: &Path, config: &Config) -> Result { debug!("scanning project ({})", base_path.to_string_lossy()); let mut owned_file_paths: Vec = Vec::new(); @@ -272,7 +272,7 @@ fn owned_files(owned_file_paths: Vec) -> Vec { .collect() } -fn package_owner(path: &Path) -> Result, Box> { +fn package_owner(path: &Path) -> Result> { let deserializer: deserializers::Package = serde_yaml::from_reader(File::open(path)?)?; if let Some(metadata) = deserializer.metadata { From 3a849479e66ca68109e0e7fab8e7ed2292164f3f Mon Sep 17 00:00:00 2001 From: Matan Zruya Date: Sat, 1 Apr 2023 15:19:43 -0400 Subject: [PATCH 2/2] Switch to color-eyre --- Cargo.lock | 127 ++++++++++++++++++++++++++++++++++++++++++++++--- Cargo.toml | 6 ++- src/main.rs | 3 +- src/project.rs | 2 +- 4 files changed, 128 insertions(+), 10 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 106997b..d16f578 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2,6 +2,21 @@ # It is not intended for manual editing. version = 3 +[[package]] +name = "addr2line" +version = "0.19.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a76fd60b23679b7d19bd066031410fb7e458ccc5e958eb5c325888ce4baedc97" +dependencies = [ + "gimli", +] + +[[package]] +name = "adler" +version = "1.0.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f26201604c87b1e01bd3d98f8d5d9a8fcbb815e8cedb41ffccbeb4bf593a35fe" + [[package]] name = "aho-corasick" version = "0.7.20" @@ -51,12 +66,6 @@ dependencies = [ "windows-sys", ] -[[package]] -name = "anyhow" -version = "1.0.70" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7de8ce5e0f9f8d88245311066a578d72b7af3e7088f32783804676302df237e4" - [[package]] name = "assert_cmd" version = "2.0.10" @@ -78,6 +87,21 @@ version = "1.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d468802bab17cbc0cc575e9b053f41e72aa36bfa6b7f55e3529ffa43161b97fa" +[[package]] +name = "backtrace" +version = "0.3.67" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "233d376d6d185f2a3093e58f283f60f880315b6c60075b01f36b3b85154564ca" +dependencies = [ + "addr2line", + "cc", + "cfg-if", + "libc", + "miniz_oxide", + "object", + "rustc-demangle", +] + [[package]] name = "bitflags" version = "1.3.2" @@ -163,10 +187,10 @@ checksum = "8a2dd5a6fe8c6e3502f568a6353e5273bbb15193ad9a89e457b9970798efbea1" name = "codeowners" version = "0.1.0" dependencies = [ - "anyhow", "assert_cmd", "clap", "clap_derive", + "color-eyre", "glob-match", "itertools", "jwalk", @@ -180,6 +204,33 @@ dependencies = [ "tracing-subscriber", ] +[[package]] +name = "color-eyre" +version = "0.6.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5a667583cca8c4f8436db8de46ea8233c42a7d9ae424a82d338f2e4675229204" +dependencies = [ + "backtrace", + "color-spantrace", + "eyre", + "indenter", + "once_cell", + "owo-colors", + "tracing-error", +] + +[[package]] +name = "color-spantrace" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1ba75b3d9449ecdccb27ecbc479fdc0b87fa2dd43d2f8298f9bf0e59aacc8dce" +dependencies = [ + "once_cell", + "owo-colors", + "tracing-core", + "tracing-error", +] + [[package]] name = "concolor-override" version = "1.0.0" @@ -311,6 +362,16 @@ dependencies = [ "libc", ] +[[package]] +name = "eyre" +version = "0.6.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4c2b6b5a29c02cdc822728b7d7b8ae1bab3e3b05d44522770ddd49722eeac7eb" +dependencies = [ + "indenter", + "once_cell", +] + [[package]] name = "fsio" version = "0.1.3" @@ -326,6 +387,12 @@ dependencies = [ "unicode-width", ] +[[package]] +name = "gimli" +version = "0.27.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ad0a93d233ebf96623465aad4046a8d3aa4da22d4f4beba5388838c8a434bbb4" + [[package]] name = "glob-match" version = "0.2.1" @@ -359,6 +426,12 @@ version = "0.3.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "fed44880c466736ef9a5c5b5facefb5ed0785676d0c02d612db14e54f0d84286" +[[package]] +name = "indenter" +version = "0.3.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ce23b50ad8242c51a442f3ff322d56b02f08852c77e4c0b4d3fd684abc89c683" + [[package]] name = "indexmap" version = "1.9.3" @@ -468,6 +541,15 @@ dependencies = [ "autocfg", ] +[[package]] +name = "miniz_oxide" +version = "0.6.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b275950c28b37e794e8c55d88aeb5e139d0ce23fdbbeda68f8d7174abdf9e8fa" +dependencies = [ + "adler", +] + [[package]] name = "nias" version = "0.5.0" @@ -494,6 +576,15 @@ dependencies = [ "libc", ] +[[package]] +name = "object" +version = "0.30.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ea86265d3d3dcb6a27fc51bd29a4bf387fae9d2986b823079d4986af253eb439" +dependencies = [ + "memchr", +] + [[package]] name = "once_cell" version = "1.17.1" @@ -506,6 +597,12 @@ version = "0.1.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b15813163c1d831bf4a13c3610c05c0d03b39feb07f7e09fa234dac9b15aaf39" +[[package]] +name = "owo-colors" +version = "3.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c1b04fb49957986fdce4d6ee7a65027d55d4b6d2265e5848bbb507b58ccfdb6f" + [[package]] name = "path-clean" version = "1.0.1" @@ -612,6 +709,12 @@ version = "0.6.29" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f162c6dd7b008981e4d40210aca20b4bd0f9b60ca9271061b07f78537722f2e1" +[[package]] +name = "rustc-demangle" +version = "0.1.22" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d4a36c42d1873f9a77c53bde094f9664d9891bc604a45b4798fd2c389ed12e5b" + [[package]] name = "rustix" version = "0.37.5" @@ -784,6 +887,16 @@ dependencies = [ "valuable", ] +[[package]] +name = "tracing-error" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d686ec1c0f384b1277f097b2f279a2ecc11afe8c133c1aabf036a27cb4cd206e" +dependencies = [ + "tracing", + "tracing-subscriber", +] + [[package]] name = "tracing-log" version = "0.1.3" diff --git a/Cargo.toml b/Cargo.toml index 131fd33..e4067a8 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -3,10 +3,14 @@ name = "codeowners" version = "0.1.0" edition = "2021" +[profile.release] +debug = true + [dependencies] -anyhow = "1.0.70" clap = { version = "4.2.1", features = ["derive"] } clap_derive = "4.2.0" +color-eyre = "0.6.2" + glob-match = "0.2.1" itertools = "0.10.5" jwalk = "0.8.1" diff --git a/src/main.rs b/src/main.rs index 9a58af9..3c4c69d 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,4 +1,4 @@ -use anyhow::{Context, Result}; +use color_eyre::{eyre::Context, Result}; use ownership::{Ownership, ValidationErrors}; use crate::project::Project; @@ -66,6 +66,7 @@ impl Args { } fn main() -> Result<()> { + color_eyre::install()?; install_logger(); print_validation_errors_to_stdout(cli())?; diff --git a/src/project.rs b/src/project.rs index 25911c4..d4db98e 100644 --- a/src/project.rs +++ b/src/project.rs @@ -5,7 +5,7 @@ use std::{ path::{Path, PathBuf}, }; -use anyhow::Result; +use color_eyre::Result; use jwalk::WalkDir; use rayon::prelude::{IntoParallelIterator, ParallelIterator}; use regex::Regex;