Skip to content

Commit e121796

Browse files
committed
refacto info_fields
1 parent 050e693 commit e121796

File tree

3 files changed

+51
-50
lines changed

3 files changed

+51
-50
lines changed

Diff for: src/onefetch/cli.rs

+5-6
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,21 @@ use {
33
cli_utils,
44
error::*,
55
image_backends,
6-
info_fields::{self, InfoFields},
6+
info_fields::{InfoField, InfoFieldOff},
77
language::Language,
88
},
99
clap::{crate_description, crate_name, crate_version, App, AppSettings, Arg},
1010
image::DynamicImage,
1111
std::{convert::From, env, str::FromStr},
12-
strum::{EnumCount, IntoEnumIterator},
12+
strum::IntoEnumIterator,
1313
};
1414

1515
pub struct Cli {
1616
pub repo_path: String,
1717
pub ascii_input: Option<String>,
1818
pub ascii_language: Option<Language>,
1919
pub ascii_colors: Vec<String>,
20-
pub disabled_fields: info_fields::InfoFieldOn,
20+
pub disabled_fields: InfoFieldOff,
2121
pub no_bold: bool,
2222
pub image: Option<DynamicImage>,
2323
pub image_backend: Option<Box<dyn image_backends::ImageBackend>>,
@@ -80,8 +80,7 @@ impl Cli {
8080
.case_insensitive(true)
8181
.help("Allows you to disable FIELD(s) from appearing in the output.")
8282
.possible_values(
83-
&InfoFields::iter()
84-
.take(InfoFields::COUNT - 1)
83+
&InfoField::iter()
8584
.map(|field| field.into())
8685
.collect::<Vec<&str>>()
8786
),
@@ -288,7 +287,7 @@ impl Cli {
288287
Vec::new()
289288
};
290289

291-
let disabled_fields = info_fields::get_disabled_fields(fields_to_hide)?;
290+
let disabled_fields = InfoFieldOff::new(fields_to_hide)?;
292291

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

Diff for: src/onefetch/error.rs

+1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ error_chain! {
1313
Regex(regex::Error);
1414
Toml(toml::de::Error);
1515
Git2(git2::Error);
16+
Strum(strum::ParseError);
1617
}
1718
}
1819

Diff for: src/onefetch/info_fields.rs

+45-44
Original file line numberDiff line numberDiff line change
@@ -4,28 +4,9 @@ use {
44
strum::{EnumCount, EnumIter, EnumString, IntoStaticStr},
55
};
66

7-
#[derive(Default)]
8-
pub struct InfoFieldOn {
9-
pub git_info: bool,
10-
pub project: bool,
11-
pub head: bool,
12-
pub version: bool,
13-
pub created: bool,
14-
pub dependencies: bool,
15-
pub languages: bool,
16-
pub authors: bool,
17-
pub last_change: bool,
18-
pub repo: bool,
19-
pub commits: bool,
20-
pub pending: bool,
21-
pub lines_of_code: bool,
22-
pub size: bool,
23-
pub license: bool,
24-
}
25-
267
#[derive(PartialEq, Eq, EnumString, EnumCount, EnumIter, IntoStaticStr)]
278
#[strum(serialize_all = "snake_case")]
28-
pub enum InfoFields {
9+
pub enum InfoField {
2910
GitInfo,
3011
Project,
3112
HEAD,
@@ -44,32 +25,52 @@ pub enum InfoFields {
4425
UnrecognizedField,
4526
}
4627

47-
pub fn get_disabled_fields(fields_to_hide: Vec<String>) -> Result<InfoFieldOn> {
48-
let mut disabled_fields = InfoFieldOn { ..Default::default() };
28+
#[derive(Default)]
29+
pub struct InfoFieldOff {
30+
pub git_info: bool,
31+
pub project: bool,
32+
pub head: bool,
33+
pub version: bool,
34+
pub created: bool,
35+
pub dependencies: bool,
36+
pub languages: bool,
37+
pub authors: bool,
38+
pub last_change: bool,
39+
pub repo: bool,
40+
pub commits: bool,
41+
pub pending: bool,
42+
pub lines_of_code: bool,
43+
pub size: bool,
44+
pub license: bool,
45+
}
46+
47+
impl InfoFieldOff {
48+
pub fn new(fields_to_hide: Vec<String>) -> Result<Self> {
49+
let mut disabled_fields = InfoFieldOff { ..Default::default() };
4950

50-
for field in fields_to_hide.iter() {
51-
let item = InfoFields::from_str(field.to_lowercase().as_str())
52-
.unwrap_or(InfoFields::UnrecognizedField);
51+
for field in fields_to_hide.iter() {
52+
let item = InfoField::from_str(field.to_lowercase().as_str())?;
5353

54-
match item {
55-
InfoFields::GitInfo => disabled_fields.git_info = true,
56-
InfoFields::Project => disabled_fields.project = true,
57-
InfoFields::HEAD => disabled_fields.head = true,
58-
InfoFields::Version => disabled_fields.version = true,
59-
InfoFields::Created => disabled_fields.created = true,
60-
InfoFields::Dependencies => disabled_fields.dependencies = true,
61-
InfoFields::Languages => disabled_fields.languages = true,
62-
InfoFields::Authors => disabled_fields.authors = true,
63-
InfoFields::LastChange => disabled_fields.last_change = true,
64-
InfoFields::Repo => disabled_fields.repo = true,
65-
InfoFields::Pending => disabled_fields.pending = true,
66-
InfoFields::Commits => disabled_fields.commits = true,
67-
InfoFields::LinesOfCode => disabled_fields.lines_of_code = true,
68-
InfoFields::Size => disabled_fields.size = true,
69-
InfoFields::License => disabled_fields.license = true,
70-
_ => (),
54+
match item {
55+
InfoField::GitInfo => disabled_fields.git_info = true,
56+
InfoField::Project => disabled_fields.project = true,
57+
InfoField::HEAD => disabled_fields.head = true,
58+
InfoField::Version => disabled_fields.version = true,
59+
InfoField::Created => disabled_fields.created = true,
60+
InfoField::Dependencies => disabled_fields.dependencies = true,
61+
InfoField::Languages => disabled_fields.languages = true,
62+
InfoField::Authors => disabled_fields.authors = true,
63+
InfoField::LastChange => disabled_fields.last_change = true,
64+
InfoField::Repo => disabled_fields.repo = true,
65+
InfoField::Pending => disabled_fields.pending = true,
66+
InfoField::Commits => disabled_fields.commits = true,
67+
InfoField::LinesOfCode => disabled_fields.lines_of_code = true,
68+
InfoField::Size => disabled_fields.size = true,
69+
InfoField::License => disabled_fields.license = true,
70+
_ => (),
71+
}
7172
}
72-
}
7373

74-
Ok(disabled_fields)
74+
Ok(disabled_fields)
75+
}
7576
}

0 commit comments

Comments
 (0)