Skip to content

Commit

Permalink
refacto: add info builder pattern (#1047)
Browse files Browse the repository at this point in the history
* info builder pattern

* fix test

* reorganize methods

* fix bench
  • Loading branch information
o2sh authored May 11, 2023
1 parent a0fa0d7 commit 8609f92
Show file tree
Hide file tree
Showing 23 changed files with 370 additions and 340 deletions.
4 changes: 2 additions & 2 deletions benches/repo.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use criterion::{black_box, criterion_group, criterion_main, Criterion};
use gix::{open, ThreadSafeRepository};
use onefetch::{cli::CliOptions, info::Info};
use onefetch::{cli::CliOptions, info::build_info};

fn bench_repo_info(c: &mut Criterion) {
let name = "repo.sh".to_string();
Expand All @@ -13,7 +13,7 @@ fn bench_repo_info(c: &mut Criterion) {

c.bench_function("get repo information", |b| {
b.iter(|| {
let result = black_box(Info::new(&config));
let result = black_box(build_info(&config));
assert!(result.is_ok());
})
});
Expand Down
12 changes: 2 additions & 10 deletions src/info/author.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
use crate::{
cli::NumberSeparator,
info::{
format_number,
utils::git::Commits,
utils::info_field::{InfoField, InfoType},
},
info::{format_number, utils::git::Commits, utils::info_field::InfoField},
};
use owo_colors::{DynColors, OwoColorize};
use serde::Serialize;
Expand Down Expand Up @@ -76,7 +72,7 @@ pub struct AuthorsInfo {
}

impl AuthorsInfo {
pub fn new(info_color: DynColors, commits: &mut Commits) -> Self {
pub fn new(info_color: DynColors, commits: &Commits) -> Self {
let authors = commits.authors_to_display.clone();
Self {
authors,
Expand Down Expand Up @@ -119,10 +115,6 @@ impl InfoField for AuthorsInfo {
title
}

fn r#type(&self) -> InfoType {
InfoType::Authors
}

fn should_color(&self) -> bool {
false
}
Expand Down
10 changes: 1 addition & 9 deletions src/info/commits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,7 @@ use serde::Serialize;

use crate::{
cli::NumberSeparator,
info::{
format_number,
utils::git::Commits,
utils::info_field::{InfoField, InfoType},
},
info::{format_number, utils::git::Commits, utils::info_field::InfoField},
};

#[derive(Serialize)]
Expand Down Expand Up @@ -41,10 +37,6 @@ impl InfoField for CommitsInfo {
fn title(&self) -> String {
"Commits".into()
}

fn r#type(&self) -> InfoType {
InfoType::Commits
}
}

#[cfg(test)]
Expand Down
10 changes: 1 addition & 9 deletions src/info/contributors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,7 @@ use serde::Serialize;

use crate::{
cli::NumberSeparator,
info::{
format_number,
utils::git::Commits,
utils::info_field::{InfoField, InfoType},
},
info::{format_number, utils::git::Commits, utils::info_field::InfoField},
};

#[derive(Serialize)]
Expand Down Expand Up @@ -46,10 +42,6 @@ impl InfoField for ContributorsInfo {
fn title(&self) -> String {
"Contributors".into()
}

fn r#type(&self) -> InfoType {
InfoType::Contributors
}
}

#[cfg(test)]
Expand Down
9 changes: 1 addition & 8 deletions src/info/created.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
use super::utils::format_time;
use crate::info::{
utils::git::Commits,
utils::info_field::{InfoField, InfoType},
};
use crate::info::{utils::git::Commits, utils::info_field::InfoField};
use serde::Serialize;

#[derive(Serialize)]
Expand Down Expand Up @@ -31,10 +28,6 @@ impl InfoField for CreatedInfo {
fn title(&self) -> String {
"Created".into()
}

fn r#type(&self) -> InfoType {
InfoType::Created
}
}

#[cfg(test)]
Expand Down
9 changes: 1 addition & 8 deletions src/info/dependencies.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
use crate::{
cli::NumberSeparator,
info::{
format_number,
utils::info_field::{InfoField, InfoType},
},
info::{format_number, utils::info_field::InfoField},
};
use onefetch_manifest::Manifest;
use serde::Serialize;
Expand Down Expand Up @@ -40,10 +37,6 @@ impl InfoField for DependenciesInfo {
fn title(&self) -> String {
"Dependencies".into()
}

fn r#type(&self) -> InfoType {
InfoType::Dependencies
}
}

#[cfg(test)]
Expand Down
6 changes: 1 addition & 5 deletions src/info/description.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::info::utils::info_field::{InfoField, InfoType};
use crate::info::utils::info_field::InfoField;
use onefetch_manifest::Manifest;
use serde::Serialize;
use std::fmt::Write;
Expand Down Expand Up @@ -50,10 +50,6 @@ impl InfoField for DescriptionInfo {
fn title(&self) -> String {
"Description".into()
}

fn r#type(&self) -> InfoType {
InfoType::Description
}
}

#[cfg(test)]
Expand Down
6 changes: 1 addition & 5 deletions src/info/head.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::info::utils::info_field::{InfoField, InfoType};
use crate::info::utils::info_field::InfoField;
use anyhow::{Context, Result};
use gix::{reference::Category, Reference, Repository};
use serde::Serialize;
Expand Down Expand Up @@ -72,10 +72,6 @@ impl InfoField for HeadInfo {
fn title(&self) -> String {
"HEAD".into()
}

fn r#type(&self) -> InfoType {
InfoType::Head
}
}

#[cfg(test)]
Expand Down
5 changes: 1 addition & 4 deletions src/info/langs/language.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::info::utils::info_field::{InfoField, InfoType};
use crate::info::utils::info_field::InfoField;
use owo_colors::OwoColorize;
use serde::Serialize;
use tokei;
Expand Down Expand Up @@ -158,9 +158,6 @@ impl InfoField for LanguagesInfo {
title
}

fn r#type(&self) -> InfoType {
InfoType::Languages
}
fn should_color(&self) -> bool {
false
}
Expand Down
9 changes: 1 addition & 8 deletions src/info/last_change.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
use super::utils::format_time;
use crate::info::{
utils::git::Commits,
utils::info_field::{InfoField, InfoType},
};
use crate::info::{utils::git::Commits, utils::info_field::InfoField};
use serde::Serialize;

#[derive(Serialize)]
Expand Down Expand Up @@ -32,10 +29,6 @@ impl InfoField for LastChangeInfo {
fn title(&self) -> String {
"Last change".into()
}

fn r#type(&self) -> InfoType {
InfoType::LastChange
}
}

#[cfg(test)]
Expand Down
6 changes: 1 addition & 5 deletions src/info/license.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::info::utils::info_field::{InfoField, InfoType};
use crate::info::utils::info_field::InfoField;
use anyhow::{bail, Result};
use askalono::{Store, TextData};
use onefetch_manifest::Manifest;
Expand Down Expand Up @@ -95,10 +95,6 @@ impl InfoField for LicenseInfo {
fn title(&self) -> String {
"License".into()
}

fn r#type(&self) -> InfoType {
InfoType::License
}
}

#[cfg(test)]
Expand Down
9 changes: 1 addition & 8 deletions src/info/loc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,7 @@ use serde::Serialize;

use crate::{
cli::NumberSeparator,
info::{
format_number,
utils::info_field::{InfoField, InfoType},
},
info::{format_number, utils::info_field::InfoField},
};

#[derive(Serialize)]
Expand Down Expand Up @@ -37,10 +34,6 @@ impl InfoField for LocInfo {
fn title(&self) -> String {
"Lines of code".into()
}

fn r#type(&self) -> InfoType {
InfoType::LinesOfCode
}
}

#[cfg(test)]
Expand Down
Loading

0 comments on commit 8609f92

Please sign in to comment.