diff --git a/benches/repo.rs b/benches/repo.rs index 05d552e3..25918b45 100644 --- a/benches/repo.rs +++ b/benches/repo.rs @@ -15,7 +15,7 @@ fn bench_repo_info(c: &mut Criterion) { b.iter(|| { let result = black_box(build_info(&config)); assert!(result.is_ok()); - }) + }); }); } diff --git a/src/info/author.rs b/src/info/author.rs index c9bea46b..0769178c 100644 --- a/src/info/author.rs +++ b/src/info/author.rs @@ -50,7 +50,7 @@ impl std::fmt::Display for Author { self.contribution, self.name, self.email, - format_number(self.nbr_of_commits, self.number_separator) + format_number(&self.nbr_of_commits, self.number_separator) ) } else { write!( @@ -58,7 +58,7 @@ impl std::fmt::Display for Author { "{}% {} {}", self.contribution, self.name, - format_number(self.nbr_of_commits, self.number_separator) + format_number(&self.nbr_of_commits, self.number_separator) ) } } diff --git a/src/info/commits.rs b/src/info/commits.rs index cfee20cc..4a61bd14 100644 --- a/src/info/commits.rs +++ b/src/info/commits.rs @@ -29,7 +29,7 @@ impl InfoField for CommitsInfo { fn value(&self) -> String { format!( "{}{}", - format_number(self.number_of_commits, self.number_separator), + format_number(&self.number_of_commits, self.number_separator), self.is_shallow.then_some(" (shallow)").unwrap_or_default() ) } diff --git a/src/info/contributors.rs b/src/info/contributors.rs index e5b16429..9c2f8a7f 100644 --- a/src/info/contributors.rs +++ b/src/info/contributors.rs @@ -33,7 +33,7 @@ impl ContributorsInfo { impl InfoField for ContributorsInfo { fn value(&self) -> String { if self.total_number_of_authors > self.number_of_authors_to_display { - format_number(self.total_number_of_authors, self.number_separator) + format_number(&self.total_number_of_authors, self.number_separator) } else { "".to_string() } diff --git a/src/info/dependencies.rs b/src/info/dependencies.rs index 5fb6e40a..6fd3d933 100644 --- a/src/info/dependencies.rs +++ b/src/info/dependencies.rs @@ -17,7 +17,7 @@ impl DependenciesInfo { (m.number_of_dependencies != 0).then(|| { format!( "{} ({})", - format_number(m.number_of_dependencies, number_separator), + format_number(&m.number_of_dependencies, number_separator), m.manifest_type ) }) diff --git a/src/info/langs/language.rs b/src/info/langs/language.rs index 9dcd3a44..1d1cd6e9 100644 --- a/src/info/langs/language.rs +++ b/src/info/langs/language.rs @@ -153,7 +153,7 @@ impl InfoField for LanguagesInfo { fn title(&self) -> String { let mut title: String = "Language".into(); if self.languages_with_percentage.len() > 1 { - title.push('s') + title.push('s'); } title } diff --git a/src/info/langs/mod.rs b/src/info/langs/mod.rs index f30a01e9..cd245b52 100644 --- a/src/info/langs/mod.rs +++ b/src/info/langs/mod.rs @@ -86,7 +86,7 @@ fn get_statistics( fn filter_languages_on_type(types: &[LanguageType]) -> Vec { Language::iter() .filter(|language| types.contains(&language.get_type())) - .map(|language| language.into()) + .map(std::convert::Into::into) .collect() } diff --git a/src/info/license.rs b/src/info/license.rs index 65dbc10b..cf2da4dc 100644 --- a/src/info/license.rs +++ b/src/info/license.rs @@ -113,7 +113,7 @@ mod test { #[test] fn test_is_license_file() { - for file_name in LICENSE_FILES.iter() { + for file_name in &LICENSE_FILES { assert!(is_license_file(file_name)); } assert!(!is_license_file("NOT_LICENSE")); diff --git a/src/info/loc.rs b/src/info/loc.rs index 9b47af1b..e6d713b5 100644 --- a/src/info/loc.rs +++ b/src/info/loc.rs @@ -28,7 +28,7 @@ impl LocInfo { #[typetag::serialize] impl InfoField for LocInfo { fn value(&self) -> String { - format_number(self.lines_of_code, self.number_separator) + format_number(&self.lines_of_code, self.number_separator) } fn title(&self) -> String { diff --git a/src/info/mod.rs b/src/info/mod.rs index 9adba561..bc677bd5 100644 --- a/src/info/mod.rs +++ b/src/info/mod.rs @@ -23,6 +23,7 @@ use crate::cli::{is_truecolor_terminal, CliOptions, NumberSeparator, When}; use crate::ui::get_ascii_colors; use crate::ui::text_colors::TextColors; use anyhow::{Context, Result}; +use gix::sec::trust::Mapping; use gix::Repository; use num_format::ToFormattedString; use onefetch_manifest::Manifest; @@ -77,7 +78,7 @@ impl std::fmt::Display for Info { fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { //Title if let Some(title) = &self.title { - write!(f, "{}", title)?; + write!(f, "{title}")?; } //Info lines @@ -89,7 +90,7 @@ impl std::fmt::Display for Info { info_field.should_color(), self.no_bold, &self.text_colors, - )? + )?; } //Palette @@ -119,7 +120,7 @@ pub fn build_info(cli_options: &CliOptions) -> Result { dot_git_only: true, ..Default::default() }, - Default::default(), + Mapping::default(), )? .to_thread_local(); let repo_path = get_work_dir(&repo)?; @@ -144,7 +145,7 @@ pub fn build_info(cli_options: &CliOptions) -> Result { .ok() .context("BUG: panic in language statistics thread")??; let manifest = get_manifest(&repo_path)?; - let repo_url = get_repo_url(&repo)?; + let repo_url = get_repo_url(&repo); let commits = Commits::new( &repo, @@ -174,7 +175,7 @@ pub fn build_info(cli_options: &CliOptions) -> Result { let number_of_languages = cli_options.info.number_of_languages; let number_of_authors = cli_options.info.number_of_authors; - Ok(InfoBuilder::new(cli_options)? + Ok(InfoBuilder::new(cli_options) .title(&repo, no_bold, &text_colors) .project(&repo, &repo_url, &manifest, number_separator)? .description(&manifest) @@ -201,13 +202,13 @@ pub fn build_info(cli_options: &CliOptions) -> Result { } impl InfoBuilder { - fn new(cli_options: &CliOptions) -> Result { - Ok(Self { + fn new(cli_options: &CliOptions) -> Self { + Self { title: None, info_fields: Vec::new(), disabled_fields: cli_options.info.disabled_fields.clone(), no_title: cli_options.info.no_title, - }) + } } fn title(mut self, repo: &Repository, no_bold: bool, text_colors: &TextColors) -> Self { @@ -462,7 +463,7 @@ pub fn get_work_dir(repo: &gix::Repository) -> Result { } fn format_number( - number: T, + number: &T, number_separator: NumberSeparator, ) -> String { number.to_formatted_string(&number_separator.get_format()) @@ -474,43 +475,43 @@ mod tests { use owo_colors::AnsiColors; #[test] - fn test_get_style() -> Result<()> { + fn test_get_style() { let style = get_style(true, DynColors::Ansi(AnsiColors::Cyan)); assert_eq!( style, Style::new().color(DynColors::Ansi(AnsiColors::Cyan)).bold() ); - Ok(()) } #[test] - fn test_get_style_no_bold() -> Result<()> { + fn test_get_style_no_bold() { let style = get_style(false, DynColors::Ansi(AnsiColors::Cyan)); assert_eq!(style, Style::new().color(DynColors::Ansi(AnsiColors::Cyan))); - Ok(()) } #[test] fn test_format_number() { assert_eq!( - &format_number(1_000_000, NumberSeparator::Comma), + &format_number(&1_000_000, NumberSeparator::Comma), "1,000,000" ); assert_eq!( - &format_number(1_000_000, NumberSeparator::Space), + &format_number(&1_000_000, NumberSeparator::Space), "1\u{202f}000\u{202f}000" ); assert_eq!( - &format_number(1_000_000, NumberSeparator::Underscore), + &format_number(&1_000_000, NumberSeparator::Underscore), "1_000_000" ); - assert_eq!(&format_number(1_000_000, NumberSeparator::Plain), "1000000"); + assert_eq!( + &format_number(&1_000_000, NumberSeparator::Plain), + "1000000" + ); } #[test] - fn test_info_style_info() -> Result<()> { - let text_colors = - TextColors::new(&vec![0, 0, 0, 0, 0, 0], DynColors::Ansi(AnsiColors::Blue)); + fn test_info_style_info() { + let text_colors = TextColors::new(&[0, 0, 0, 0, 0, 0], DynColors::Ansi(AnsiColors::Blue)); let info_text = style_info("foo", &text_colors, false); assert_eq!(info_text, "foo"); @@ -519,13 +520,11 @@ mod tests { let info_text = style_info("foo", &text_colors, true); // Rendered text: black `foo` assert_eq!(info_text, "\u{1b}[30mfoo\u{1b}[0m"); - Ok(()) } #[test] - fn test_info_style_subtitle() -> Result<()> { - let text_colors = - TextColors::new(&vec![0, 0, 0, 0, 15, 0], DynColors::Ansi(AnsiColors::Blue)); + fn test_info_style_subtitle() { + let text_colors = TextColors::new(&[0, 0, 0, 0, 15, 0], DynColors::Ansi(AnsiColors::Blue)); let subtitle_text = style_subtitle("foo", &text_colors, false); assert_eq!( @@ -533,6 +532,5 @@ mod tests { // Rendered text: black `foo` and bright white colon "\u{1b}[30;1mfoo\u{1b}[0m\u{1b}[97;1m:\u{1b}[0m" ); - Ok(()) } } diff --git a/src/info/pending.rs b/src/info/pending.rs index e10200b0..439b8e89 100644 --- a/src/info/pending.rs +++ b/src/info/pending.rs @@ -45,7 +45,7 @@ fn get_pending_changes(repo: &git2::Repository) -> Result { let mut result = String::new(); if modified > 0 { - result = format!("{modified}+-") + result = format!("{modified}+-"); } if added > 0 { diff --git a/src/info/project.rs b/src/info/project.rs index f5eca065..fb4be3e8 100644 --- a/src/info/project.rs +++ b/src/info/project.rs @@ -47,7 +47,7 @@ fn get_repo_name(repo_url: &str, manifest: Option<&Manifest>) -> Result .with_extension("") .file_name() .map(OsStr::to_string_lossy) - .map(|s| s.into_owned()) + .map(std::borrow::Cow::into_owned) .unwrap_or_default(); if repo_name.is_empty() { @@ -82,7 +82,7 @@ impl std::fmt::Display for ProjectInfo { 1 => "1 branch".into(), _ => format!( "{} branches", - format_number(self.number_of_branches, self.number_separator) + format_number(&self.number_of_branches, self.number_separator) ), }; @@ -91,7 +91,7 @@ impl std::fmt::Display for ProjectInfo { 1 => "1 tag".into(), _ => format!( "{} tags", - format_number(self.number_of_tags, self.number_separator) + format_number(&self.number_of_tags, self.number_separator) ), }; @@ -198,7 +198,7 @@ mod test { #[test] fn test_display_project_info_when_no_repo_name() { let project_info = ProjectInfo { - repo_name: "".to_string(), + repo_name: String::new(), number_of_branches: 0, number_of_tags: 0, number_separator: NumberSeparator::Plain, diff --git a/src/info/size.rs b/src/info/size.rs index b6bd8791..5f3be9a7 100644 --- a/src/info/size.rs +++ b/src/info/size.rs @@ -53,7 +53,7 @@ impl std::fmt::Display for SizeInfo { f, "{} ({} files)", self.repo_size, - format_number(self.file_count, self.number_separator) + format_number(&self.file_count, self.number_separator) ) } } diff --git a/src/info/title.rs b/src/info/title.rs index a6b23b6f..7eb8b9e6 100644 --- a/src/info/title.rs +++ b/src/info/title.rs @@ -124,14 +124,14 @@ mod tests { assert!(title.to_string().contains('~')); assert!(title.to_string().contains("git version 2.37.2")); - title.git_version = "".to_string(); + title.git_version = String::new(); assert!(title.to_string().contains("onefetch-committer-name")); assert!(!title.to_string().contains('~')); assert!(!title.to_string().contains("git version 2.37.2")); - title.git_username = "".to_string(); - let expected_title = "".to_string(); - assert_eq!(format!("{}", title), expected_title); + title.git_username = String::new(); + let expected_title = String::new(); + assert_eq!(format!("{title}"), expected_title); Ok(()) } diff --git a/src/info/url.rs b/src/info/url.rs index 0fe9c22e..d43237ac 100644 --- a/src/info/url.rs +++ b/src/info/url.rs @@ -1,5 +1,4 @@ use crate::info::utils::info_field::InfoField; -use anyhow::Result; use gix::Repository; use regex::Regex; use serde::Serialize; @@ -17,11 +16,11 @@ impl UrlInfo { } } -pub fn get_repo_url(repo: &Repository) -> Result { +pub fn get_repo_url(repo: &Repository) -> String { let config = repo.config_snapshot(); let remotes = match config.plumbing().sections_by_name("remote") { Some(sections) => sections, - None => return Ok(Default::default()), + None => return String::default(), }; let mut remote_url: Option = None; @@ -36,17 +35,15 @@ pub fn get_repo_url(repo: &Repository) -> Result { } } - let remote_url = match remote_url { - Some(url) => remove_token_from_url(url), - None => return Ok(Default::default()), - }; - - Ok(remote_url) + match remote_url { + Some(url) => remove_token_from_url(&url), + None => String::default(), + } } -fn remove_token_from_url(url: String) -> String { +fn remove_token_from_url(url: &str) -> String { let pattern = Regex::new(r"(https?://)([^@]+@)").unwrap(); - let replaced_url = pattern.replace(&url, "$1").to_string(); + let replaced_url = pattern.replace(url, "$1").to_string(); replaced_url } @@ -80,16 +77,14 @@ mod test { #[test] fn test_token_removal_github() { let remote_url = - "https://1234567890abcdefghijklmnopqrstuvwxyz@github.com/jim4067/onefetch.git" - .to_string(); + "https://1234567890abcdefghijklmnopqrstuvwxyz@github.com/jim4067/onefetch.git"; let res_url = remove_token_from_url(remote_url); assert_eq!("https://github.com/jim4067/onefetch.git", res_url); } #[test] fn test_token_removal_gitlab() { - let remote_url = - "https://john:abc123personaltoken@gitlab.com/jim4067/myproject.git".to_string(); + let remote_url = "https://john:abc123personaltoken@gitlab.com/jim4067/myproject.git"; let res_url = remove_token_from_url(remote_url); assert_eq!("https://gitlab.com/jim4067/myproject.git", res_url); } diff --git a/src/info/utils/git.rs b/src/info/utils/git.rs index 967c644f..1fcbc787 100644 --- a/src/info/utils/git.rs +++ b/src/info/utils/git.rs @@ -146,10 +146,9 @@ fn get_no_bots_regex(no_bots: &Option>) -> Result) -> bool { - bot_regex_pattern - .as_ref() - .map(|regex| regex.0.is_match(author_name.to_str_lossy().as_ref())) - .unwrap_or(false) + bot_regex_pattern.as_ref().map_or(false, |regex| { + regex.0.is_match(author_name.to_str_lossy().as_ref()) + }) } #[cfg(test)] diff --git a/src/ui/printer.rs b/src/ui/printer.rs index 6a634fd7..6661dbeb 100644 --- a/src/ui/printer.rs +++ b/src/ui/printer.rs @@ -65,10 +65,10 @@ impl Printer { match &self.output { Some(format) => match format { SerializationFormat::Json => { - writeln!(self.writer, "{}", serde_json::to_string_pretty(&self.info)?)? + writeln!(self.writer, "{}", serde_json::to_string_pretty(&self.info)?)?; } SerializationFormat::Yaml => { - writeln!(self.writer, "{}", serde_yaml::to_string(&self.info)?)? + writeln!(self.writer, "{}", serde_yaml::to_string(&self.info)?)?; } }, None => { @@ -104,7 +104,7 @@ impl Printer { loop { match (logo_lines.next(), info_lines.next()) { (Some(logo_line), Some(info_line)) => { - writeln!(buf, "{logo_line}{center_pad}{info_line:^}")? + writeln!(buf, "{logo_line}{center_pad}{info_line:^}")?; } (Some(logo_line), None) => writeln!(buf, "{logo_line}")?, (None, Some(info_line)) => writeln!(