Skip to content

Better error handling #466

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 10 commits into from
Jul 16, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 7 additions & 17 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ priority = "optional"
section = "utility"

[dependencies]
anyhow = "1.0"
askalono = "0.4.3"
byte-unit = "4.0.12"
bytecount = "0.6.2"
Expand All @@ -28,7 +29,6 @@ chrono-humanize = "0.2.1"
clap = "2.33.3"
color_quant = "1.1.0"
colored = "2.0.0"
error-chain = "0.12"
git2 = {version = "0.13.20", default-features = false}
image = "0.23.14"
json = "0.12.4"
Expand Down
56 changes: 24 additions & 32 deletions src/cli.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
use crate::error::*;
use crate::info::deps::package_manager::PackageManager;
use crate::info::info_field::{InfoField, InfoFieldOff};
use crate::info::language::Language;
use crate::ui::image_backends;
use crate::ui::image_backends::ImageBackend;
use crate::ui::printer::SerializationFormat;
use anyhow::{Context, Result};
use clap::{crate_description, crate_name, crate_version, App, AppSettings, Arg};
use image::DynamicImage;
use regex::Regex;
Expand Down Expand Up @@ -216,10 +216,9 @@ impl Config {
.value_name("REGEX")
.help("Exclude [bot] commits. Use <REGEX> to override the default pattern.")
.validator(|p| {
if Regex::from_str(&p).is_err() {
Err(String::from("must be a valid regex pattern"))
} else {
Ok(())
match Regex::from_str(&p) {
Ok(_) => Ok(()),
Err(_) => Err(String::from("must be a valid regex pattern"))
}
}),
)
Expand Down Expand Up @@ -252,10 +251,10 @@ impl Config {
.default_value("3")
.help("NUM of authors to be shown.")
.validator(|t| {
t.parse::<u32>()
.map_err(|_t| "must be a number")
.map(|_t|())
.map_err(|e| e.to_string())
match t.parse::<u32>() {
Ok(_) => Ok(()),
Err(_) => Err(String::from("must be a number"))
}
})
)
.arg(
Expand Down Expand Up @@ -286,6 +285,7 @@ impl Config {
Some("auto") => is_truecolor_terminal(),
_ => unreachable!(),
};

let no_bold = matches.is_present("no-bold");
let no_merges = matches.is_present("no-merges");
let no_color_palette = matches.is_present("no-palette");
Expand All @@ -295,8 +295,7 @@ impl Config {
let show_email = matches.is_present("email");
let include_hidden = matches.is_present("hidden");

let output =
matches.value_of("output").map(SerializationFormat::from_str).transpose().unwrap();
let output = matches.value_of("output").map(SerializationFormat::from_str).transpose()?;

let fields_to_hide: Vec<String> = if let Some(values) = matches.values_of("disable-fields")
{
Expand All @@ -317,11 +316,11 @@ impl Config {
false
}
}
_ => unreachable!("other values for --hide-logo are not allowed"),
_ => unreachable!(),
};

let image = if let Some(image_path) = matches.value_of("image") {
Some(image::open(image_path).chain_err(|| "Could not load the specified image")?)
Some(image::open(image_path).with_context(|| "Could not load the specified image")?)
} else {
None
};
Expand All @@ -336,17 +335,16 @@ impl Config {
None
};

if image.is_some() && image_backend.is_none() {
return Err("Could not detect a supported image backend".into());
}

let image_color_resolution = if let Some(value) = matches.value_of("color-resolution") {
usize::from_str(value).unwrap()
usize::from_str(value)?
} else {
16
};

let repo_path = String::from(matches.value_of("input").unwrap());
let repo_path = matches
.value_of("input")
.map(String::from)
.with_context(|| "Failed to parse input directory")?;

let ascii_input = matches.value_of("ascii-input").map(String::from);

Expand All @@ -366,20 +364,14 @@ impl Config {
Vec::new()
};

let number_of_authors: usize = matches.value_of("authors-number").unwrap().parse().unwrap();
let number_of_authors: usize = matches.value_of("authors-number").unwrap().parse()?;

let mut ignored_directories: Vec<String> = Vec::new();
if let Some(user_ignored_directories) = matches.values_of("exclude") {
let re = Regex::new(r"((.*)+/)+(.*)").unwrap();
for user_ignored_directory in user_ignored_directories {
if re.is_match(&user_ignored_directory) {
let prefix = if user_ignored_directory.starts_with('/') { "**" } else { "**/" };
ignored_directories.push(format!("{}{}", prefix, user_ignored_directory));
} else {
ignored_directories.push(String::from(user_ignored_directory));
}
}
};
let ignored_directories =
if let Some(user_ignored_directories) = matches.values_of("exclude") {
user_ignored_directories.map(String::from).collect()
} else {
Vec::new()
};

let bot_regex_pattern = matches.is_present("no-bots").then(|| {
matches
Expand Down
26 changes: 0 additions & 26 deletions src/error.rs

This file was deleted.

17 changes: 7 additions & 10 deletions src/info/deps/mod.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
use {
crate::error::*,
std::collections::HashMap,
std::{ffi::OsStr, fs},
};
use anyhow::Result;
use std::collections::HashMap;
use std::{ffi::OsStr, fs};

pub mod package_manager;

Expand All @@ -20,8 +18,7 @@ impl DependencyDetector {
}

pub fn get_dependencies(&self, dir: &str) -> Result<String> {
let deps = fs::read_dir(dir)
.chain_err(|| "Could not read directory")?
let deps = fs::read_dir(dir)?
.filter_map(std::result::Result::ok)
.map(|entry| entry.path())
.filter(|entry| {
Expand All @@ -35,10 +32,10 @@ impl DependencyDetector {
.map(|entry| {
let (parser, found_package_manager) =
&self.package_managers[entry.file_name().unwrap().to_str().unwrap()];
let contents = fs::read_to_string(entry)?;
let number_of_deps = parser(&contents)?;
let contents = fs::read_to_string(entry).unwrap_or_default();
let number_of_deps = parser(&contents).unwrap();
match number_of_deps {
0 => Err("".into()),
0 => Err(""),
_ => Ok(format!("{} ({})", number_of_deps, found_package_manager)),
}
})
Expand Down
7 changes: 5 additions & 2 deletions src/info/deps/package_manager.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
use crate::error::*;
use anyhow::Result;
use regex::Regex;
use std::collections::HashMap;
use {regex::Regex, strum::EnumIter, toml::Value, yaml_rust::YamlLoader};
use strum::EnumIter;
use toml::Value;
use yaml_rust::YamlLoader;

macro_rules! define_package_managers {
($( { $name:ident, $display:literal, [$(($file:literal, $parser:expr))+] } ),+ ,) => {
Expand Down
2 changes: 1 addition & 1 deletion src/info/info_field.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::error::*;
use anyhow::Result;
use std::str::FromStr;
use strum::{EnumCount, EnumIter, EnumString, IntoStaticStr};

Expand Down
Loading