Skip to content

Commit 76b7343

Browse files
authored
Integrate color-eyre for better error and stacktrace reporting (#3)
1 parent a26baf9 commit 76b7343

File tree

4 files changed

+142
-20
lines changed

4 files changed

+142
-20
lines changed

Cargo.lock

Lines changed: 120 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,14 @@ name = "codeowners"
33
version = "0.1.0"
44
edition = "2021"
55

6+
[profile.release]
7+
debug = true
8+
69
[dependencies]
710
clap = { version = "4.2.1", features = ["derive"] }
811
clap_derive = "4.2.0"
12+
color-eyre = "0.6.2"
13+
914
glob-match = "0.2.1"
1015
itertools = "0.10.5"
1116
jwalk = "0.8.1"

src/main.rs

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
1+
use color_eyre::{eyre::Context, Result};
12
use ownership::{Ownership, ValidationErrors};
2-
use tracing::debug;
33

44
use crate::project::Project;
55
use clap::{Parser, Subcommand};
66
use path_clean::PathClean;
77
use std::{
8-
error::Error,
98
fs::File,
109
path::{Path, PathBuf},
1110
process,
@@ -47,44 +46,42 @@ struct Args {
4746
}
4847

4948
impl Args {
50-
fn absolute_project_root(&self) -> Result<PathBuf, std::io::Error> {
51-
self.project_root.canonicalize()
49+
fn absolute_project_root(&self) -> Result<PathBuf> {
50+
self.project_root
51+
.canonicalize()
52+
.with_context(|| format!("Can't canonizalize {}", self.project_root.to_string_lossy()))
5253
}
5354

54-
fn absolute_config_path(&self) -> Result<PathBuf, std::io::Error> {
55+
fn absolute_config_path(&self) -> Result<PathBuf> {
5556
Ok(self.absolute_path(&self.config_path)?.clean())
5657
}
5758

58-
fn absolute_codeowners_path(&self) -> Result<PathBuf, std::io::Error> {
59+
fn absolute_codeowners_path(&self) -> Result<PathBuf> {
5960
Ok(self.absolute_path(&self.codeowners_file_path)?.clean())
6061
}
6162

62-
fn absolute_path(&self, path: &Path) -> Result<PathBuf, std::io::Error> {
63+
fn absolute_path(&self, path: &Path) -> Result<PathBuf> {
6364
Ok(self.absolute_project_root()?.join(path))
6465
}
6566
}
6667

67-
fn main() -> Result<(), Box<dyn Error>> {
68+
fn main() -> Result<()> {
69+
color_eyre::install()?;
6870
install_logger();
6971
print_validation_errors_to_stdout(cli())?;
7072

7173
Ok(())
7274
}
7375

74-
fn cli() -> Result<(), Box<dyn Error>> {
76+
fn cli() -> Result<()> {
7577
let args = Args::parse();
7678

7779
let config_path = args.absolute_config_path()?;
7880
let codeowners_file_path = args.absolute_codeowners_path()?;
7981
let project_root = args.absolute_project_root()?;
8082

81-
debug!(
82-
config_path = &config_path.to_str(),
83-
codeowners_file_path = &codeowners_file_path.to_str(),
84-
project_root = &project_root.to_str(),
85-
);
86-
87-
let config = serde_yaml::from_reader(File::open(config_path)?)?;
83+
let config =
84+
serde_yaml::from_reader(File::open(&config_path).with_context(|| format!("Can't open {}", config_path.to_string_lossy()))?)?;
8885
let ownership = Ownership::build(Project::build(&project_root, &codeowners_file_path, &config)?);
8986
let command = args.command;
9087

@@ -102,7 +99,7 @@ fn cli() -> Result<(), Box<dyn Error>> {
10299
Ok(())
103100
}
104101

105-
fn print_validation_errors_to_stdout(result: Result<(), Box<dyn Error>>) -> Result<(), Box<dyn Error>> {
102+
fn print_validation_errors_to_stdout(result: Result<()>) -> Result<()> {
106103
if let Err(error) = result {
107104
if let Some(validation_errors) = error.downcast_ref::<ValidationErrors>() {
108105
println!("{}", validation_errors);

src/project.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
use std::{
22
collections::HashMap,
3-
error::Error,
43
fs::File,
54
io::BufRead,
65
path::{Path, PathBuf},
76
};
87

8+
use color_eyre::Result;
99
use jwalk::WalkDir;
1010
use rayon::prelude::{IntoParallelIterator, ParallelIterator};
1111
use regex::Regex;
@@ -111,7 +111,7 @@ mod deserializers {
111111

112112
impl Project {
113113
#[instrument(level = "debug", skip_all)]
114-
pub fn build(base_path: &Path, codeowners_file_path: &Path, config: &Config) -> Result<Self, Box<dyn Error>> {
114+
pub fn build(base_path: &Path, codeowners_file_path: &Path, config: &Config) -> Result<Self> {
115115
debug!("scanning project ({})", base_path.to_string_lossy());
116116

117117
let mut owned_file_paths: Vec<PathBuf> = Vec::new();
@@ -272,7 +272,7 @@ fn owned_files(owned_file_paths: Vec<PathBuf>) -> Vec<ProjectFile> {
272272
.collect()
273273
}
274274

275-
fn package_owner(path: &Path) -> Result<Option<String>, Box<dyn Error>> {
275+
fn package_owner(path: &Path) -> Result<Option<String>> {
276276
let deserializer: deserializers::Package = serde_yaml::from_reader(File::open(path)?)?;
277277

278278
if let Some(metadata) = deserializer.metadata {

0 commit comments

Comments
 (0)