Clippy fixes

This commit is contained in:
Łukasz Wojtów 2021-11-15 12:53:31 +01:00
parent 33ffd81aa5
commit 4d436ba274
8 changed files with 25 additions and 34 deletions

View file

@ -32,7 +32,7 @@ pub fn list(file_name: &str, filter: getter::ActivityFilter, do_group_activities
&filtered_activities[first_element..],
);
} else {
let with_start_dates = !filter.date.is_some();
let with_start_dates = filter.date.is_none();
list::list_activities(
&filtered_activities[first_element..],
with_start_dates,

View file

@ -48,7 +48,7 @@ fn save_new_activity(
);
file_content.push(bartib_file::Line::for_activity(activity));
bartib_file::write_to_file(file_name, &file_content)
bartib_file::write_to_file(file_name, file_content)
.context(format!("Could not write to file: {}", file_name))
}

View file

@ -51,7 +51,7 @@ impl Activity {
impl fmt::Display for Activity {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let escaped_project_name = escape_special_chars(&format!("{}", self.project));
let escaped_project_name = escape_special_chars(self.project.as_str());
let escaped_description = escape_special_chars(&self.description);
match self.end {

View file

@ -79,13 +79,13 @@ pub fn filter_activities<'a>(
}
pub fn get_last_activity_by_end(file_content: &[bartib_file::Line]) -> Option<&activity::Activity> {
get_activities(&file_content)
get_activities(file_content)
.filter(|activity| activity.is_stopped())
.max_by_key(|activity| activity.end.unwrap_or(naive::MIN_DATE.and_hms(0, 0, 0)))
.max_by_key(|activity| activity.end.unwrap_or_else(||naive::MIN_DATE.and_hms(0, 0, 0)))
}
pub fn get_last_activity_by_start(file_content: &Vec<bartib_file::Line>) -> Option<&activity::Activity> {
get_activities(&file_content).max_by_key(|activity| activity.start)
pub fn get_last_activity_by_start(file_content: &[bartib_file::Line]) -> Option<&activity::Activity> {
get_activities(file_content).max_by_key(|activity| activity.start)
}
#[cfg(test)]

View file

@ -251,7 +251,7 @@ fn run_subcommand(matches: &ArgMatches, file_name: &str) -> Result<()> {
filter.date = Some(Local::now().naive_local().date() - Duration::days(1));
}
let do_group_activities = !sub_m.is_present("no_grouping") && !filter.date.is_some();
let do_group_activities = !sub_m.is_present("no_grouping") && filter.date.is_none();
bartib::controller::list::list(file_name, filter, do_group_activities)
}
("report", Some(sub_m)) => {

View file

@ -18,7 +18,7 @@ pub fn list_activities(activities: &[&activity::Activity], with_start_dates: boo
activities
.iter()
.map(|t| get_activity_table_row(&t, with_start_dates))
.map(|t| get_activity_table_row(t, with_start_dates))
.for_each(|row| activity_table.add_row(row));
println!("\n{}", activity_table);
@ -56,7 +56,7 @@ fn create_activity_table() -> table::Table {
fn create_activites_group(title: &str, activities: &[&activity::Activity]) -> table::Group {
let rows = activities
.iter()
.map(|a| get_activity_table_row(&a, false))
.map(|a| get_activity_table_row(a, false))
.collect();
table::Group::new(Some(title.to_string()), rows)
}
@ -163,7 +163,7 @@ fn group_activities_by_date<'a>(
for &activity in activities.iter() {
activities_by_date
.entry(activity.start.date())
.or_insert(Vec::new())
.or_insert_with(Vec::new)
.push(activity);
}

View file

@ -21,8 +21,8 @@ struct Report<'a> {
impl<'a> Report<'a> {
fn new(activities: &'a [&'a activity::Activity]) -> Report<'a> {
Report {
project_map: create_project_map(&activities),
total_duration: sum_duration(&activities)
project_map: create_project_map(activities),
total_duration: sum_duration(activities)
}
}
}
@ -30,7 +30,7 @@ impl<'a> Report<'a> {
impl<'a> fmt::Display for Report<'a> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let mut longest_line = get_longest_line(&self.project_map).unwrap_or(0);
let longest_duration_string = get_longest_duration_string(&self).unwrap_or(0);
let longest_duration_string = get_longest_duration_string(self).unwrap_or(0);
let terminal_width = term_size::dimensions_stdout().map(|d| d.0)
.unwrap_or(conf::DEFAULT_WIDTH);
@ -43,7 +43,7 @@ impl<'a> fmt::Display for Report<'a> {
print_project_heading(f, project, duration, longest_line, longest_duration_string)?;
print_descriptions_with_durations(f, activities, longest_line, longest_duration_string)?;
writeln!(f, "")?;
writeln!(f)?;
}
print_total_duration(f, self.total_duration, longest_line)?;
@ -131,7 +131,7 @@ fn print_descriptions_with_durations<'a>(f: &mut fmt::Formatter<'_>, activities:
Ok(())
}
fn print_total_duration<'a>(f: &mut fmt::Formatter<'_>, total_duration: Duration, line_width: usize) -> fmt::Result {
fn print_total_duration(f: &mut fmt::Formatter<'_>, total_duration: Duration, line_width: usize) -> fmt::Result {
writeln!(f, "{prefix}{total:.<width$} {duration}{suffix}",
prefix = Style::new().bold().prefix(),
total = "Total",
@ -166,7 +166,7 @@ fn get_longest_line(project_map: &ProjectMap) -> Option<usize> {
fn get_longest_duration_string(report: &Report) -> Option<usize> {
let longest_project_duration = report.project_map.values()
.map(|(_a, d)| format_util::format_duration(&d))
.map(|(_a, d)| format_util::format_duration(d))
.map(|s| s.chars().count())
.max();
let longest_activity_duration = report.project_map.values()
@ -225,10 +225,7 @@ mod tests {
let a2 = activity::Activity::start("p1".to_string(), "d2".to_string(), None);
let a3 = activity::Activity::start("p2".to_string(), "d1".to_string(), None);
let mut activities: Vec<&activity::Activity> = Vec::new();
activities.push(&a1);
activities.push(&a2);
activities.push(&a3);
let activities = vec![&a1, &a2, &a3];
let m = create_project_map(&activities);
assert_eq!(m.len(), 2);
@ -243,11 +240,7 @@ mod tests {
let a3 = activity::Activity::start("p2".to_string(), "d1".to_string(), None);
let a4 = activity::Activity::start("p2".to_string(), "d1".to_string(), None);
let mut activities: Vec<&activity::Activity> = Vec::new();
activities.push(&a1);
activities.push(&a2);
activities.push(&a3);
activities.push(&a4);
let activities = vec![&a1, &a2, &a3, &a4];
let m = group_activities_by_description(&activities);
assert_eq!(m.len(), 2);

View file

@ -108,7 +108,7 @@ impl Table {
// we start with a width of 0 for all the wrapable columns
let mut column_width : Vec<usize> = max_column_width.iter().zip(columns_wrap.iter())
.map(|(width, wrap)| if matches!(wrap, Wrap::NoWrap) { width.clone() } else { 0 })
.map(|(width, wrap)| if matches!(wrap, Wrap::NoWrap) { *width } else { 0 })
.collect();
// then we distribute the available width to the wrappable columns
@ -198,7 +198,7 @@ impl fmt::Display for Table {
fn write_group(
f: &mut fmt::Formatter<'_>,
group: &Group,
column_width: &Vec<usize>,
column_width: &[usize],
) -> fmt::Result {
let empty_string = "".to_string();
let title = group.title.as_ref().unwrap_or(&empty_string);
@ -207,14 +207,14 @@ fn write_group(
writeln!(f, "{}", Style::new().bold().paint(title))?;
for row in &group.rows {
write_row(f, row, &column_width)?;
write_row(f, row, column_width)?;
}
Ok(())
}
fn write_row(f: &mut fmt::Formatter<'_>, row: &Row, column_width: &Vec<usize>) -> fmt::Result {
write_cells(f, &row.content, &column_width, row.style)?;
fn write_row(f: &mut fmt::Formatter<'_>, row: &Row, column_width: &[usize]) -> fmt::Result {
write_cells(f, &row.content, column_width, row.style)?;
writeln!(f)?;
Ok(())
}
@ -232,9 +232,7 @@ fn write_cells<T: AsRef<str> + std::fmt::Display>(
.map(|(i, c)| match column_width.get(i) {
Some(s) => textwrap::wrap(c.as_ref(), textwrap::Options::new(*s)),
None => {
let mut lines = Vec::new();
lines.push(Cow::from(c.as_ref()));
lines
vec![Cow::from(c.as_ref())]
}
})
.collect();