Skip to content

Commit e297b36

Browse files
committed
clippy review
1 parent 511323a commit e297b36

File tree

1 file changed

+22
-19
lines changed

1 file changed

+22
-19
lines changed

src/main.rs

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,9 @@ impl fmt::Display for Info {
6161
let mut s = String::from("");
6262
for language in self.languages.iter() {
6363
let formatted_number = format!("{:.*}", 1, language.1);
64-
s.push_str(&(language.0.to_string() + " " + &formatted_number.to_string() + "% "));
64+
s.push_str(
65+
&(language.0.to_string() + " (" + &formatted_number.to_string() + "%) "),
66+
);
6567
}
6668
writeln!(buffer, "{}{}", title.color(color).bold(), s)?;
6769
} else {
@@ -146,10 +148,10 @@ impl fmt::Display for Info {
146148
/// which comes from the added escape characters.
147149
fn colorize_str(line: &str, colors: Vec<Color>) -> (String, usize) {
148150
// Extract colors from string coded with {n}
149-
let mut colors_in_str: Vec<Color> = line.split("{").fold(Vec::new(), |mut acc, s| {
151+
let mut colors_in_str: Vec<Color> = line.split('{').fold(Vec::new(), |mut acc, s| {
150152
if s.len() > 2 {
151153
let i = s.chars().nth(0).unwrap_or('0').to_digit(10).unwrap_or(0);
152-
acc.push(*colors.iter().nth(i as usize).unwrap_or(&Color::White));
154+
acc.push(*colors.get(i as usize).unwrap_or(&Color::White));
153155
}
154156
acc
155157
});
@@ -163,7 +165,7 @@ fn colorize_str(line: &str, colors: Vec<Color>) -> (String, usize) {
163165

164166
let mut colors_iter = colors_in_str.iter();
165167

166-
let out_str = line.split("{").fold(String::new(), |mut acc, s| {
168+
let out_str = line.split('{').fold(String::new(), |mut acc, s| {
167169
if s.len() > 2 {
168170
let s: String = s.chars().skip(2).collect();
169171
let c = match colors_iter.next() {
@@ -180,7 +182,7 @@ fn colorize_str(line: &str, colors: Vec<Color>) -> (String, usize) {
180182
/// Returns the true length of a string after substracting the {n}
181183
/// color declarations.
182184
fn true_len(line: &str) -> usize {
183-
line.split("{")
185+
line.split('{')
184186
.fold(String::new(), |mut acc, s| {
185187
if s.len() > 2 {
186188
acc.push_str(&s.chars().skip(2).collect::<String>());
@@ -245,7 +247,7 @@ fn main() -> Result<()> {
245247
}
246248

247249
let tokei_langs = project_languages();
248-
let languages_stat = get_languages_stat(&tokei_langs);
250+
let languages_stat = get_languages_stat(&tokei_langs).ok_or(Error::SourceCodeNotFound)?;
249251
let mut languages_stat_vec: Vec<(_, _)> = languages_stat.into_iter().collect();
250252
languages_stat_vec.sort_by(|a, b| a.1.partial_cmp(&b.1).unwrap().reverse());
251253
let dominant_language = languages_stat_vec[0].0.clone();
@@ -278,22 +280,23 @@ fn project_languages() -> tokei::Languages {
278280
languages
279281
}
280282

281-
fn get_languages_stat(languages: &tokei::Languages) -> HashMap<Language, f64> {
283+
fn get_languages_stat(languages: &tokei::Languages) -> Option<HashMap<Language, f64>> {
282284
let mut stats = HashMap::new();
283285

284-
let sum_language_code: usize = languages
285-
.remove_empty()
286-
.iter()
287-
.map(|(_, v)| v.code).sum();
288-
289-
for (k, v) in languages.remove_empty().iter() {
290-
let code = v.code as f64;
291-
stats.insert(
292-
Language::from(**k),
293-
(code / sum_language_code as f64) * 100.00,
294-
);
286+
let sum_language_code: usize = languages.remove_empty().iter().map(|(_, v)| v.code).sum();
287+
288+
if sum_language_code == 0 {
289+
None
290+
} else {
291+
for (k, v) in languages.remove_empty().iter() {
292+
let code = v.code as f64;
293+
stats.insert(
294+
Language::from(**k),
295+
(code / sum_language_code as f64) * 100.00,
296+
);
297+
}
298+
Some(stats)
295299
}
296-
stats
297300
}
298301

299302
fn project_license() -> Result<String> {

0 commit comments

Comments
 (0)