From 1204be40fb9d1bb0f5c52dc868794edae01cf5de Mon Sep 17 00:00:00 2001 From: kageru Date: Sun, 19 Apr 2020 19:22:06 +0200 Subject: [PATCH] clippy --- src/timesheet.rs | 28 +++++++++++++--------------- src/todolist.rs | 11 +++++------ src/tracc.rs | 12 +++++------- 3 files changed, 23 insertions(+), 28 deletions(-) diff --git a/src/timesheet.rs b/src/timesheet.rs index 6232018..b4f013e 100644 --- a/src/timesheet.rs +++ b/src/timesheet.rs @@ -2,11 +2,12 @@ use super::tracc::ListView; use itertools::Itertools; use serde::{Deserialize, Serialize}; use serde_json::from_reader; +use std::collections; use std::default; use std::fmt; -use std::fs::File; -use std::io::BufReader; -use time::OffsetDateTime; +use std::fs; +use std::io; +use time::{Duration, OffsetDateTime}; pub struct TimeSheet { pub times: Vec, @@ -50,16 +51,16 @@ impl default::Default for TimePoint { } fn read_times(path: &str) -> Option> { - File::open(path) + fs::File::open(path) .ok() - .map(|f| BufReader::new(f)) + .map(io::BufReader::new) .and_then(|r| from_reader(r).ok()) } impl TimeSheet { pub fn open_or_create(path: &str) -> Self { Self { - times: read_times(path).unwrap_or(vec![TimePoint::new("Did something")]), + times: read_times(path).unwrap_or_else(|| vec![TimePoint::new("Did something")]), selected: 0, register: None, editing_time: false, @@ -79,13 +80,10 @@ impl TimeSheet { .iter() .tuple_windows() .map(|(prev, next)| (prev.text.clone(), next.time - prev.time)) - .fold( - std::collections::BTreeMap::new(), - |mut map, (text, duration)| { - *map.entry(text).or_insert(time::Duration::zero()) += duration; - map - }, - ) + .fold(collections::BTreeMap::new(), |mut map, (text, duration)| { + *map.entry(text).or_insert_with(Duration::zero) += duration; + map + }) .into_iter() .map(|(text, duration)| format!("{}: {}", text, format_duration(&duration))) .join(" | ") @@ -97,14 +95,14 @@ impl TimeSheet { .iter() .map(|tp| tp.time) .tuple_windows() - .fold(time::Duration::zero(), |total, (last, next)| { + .fold(Duration::zero(), |total, (last, next)| { total + (next - last) }); format_duration(&total) } } -fn format_duration(d: &time::Duration) -> String { +fn format_duration(d: &Duration) -> String { format!("{}:{:02}", d.whole_hours(), d.whole_minutes().max(1) % 60) } diff --git a/src/todolist.rs b/src/todolist.rs index 866f4e8..d7a57f9 100644 --- a/src/todolist.rs +++ b/src/todolist.rs @@ -2,8 +2,8 @@ use crate::tracc::ListView; use serde::{Deserialize, Serialize}; use serde_json::from_reader; use std::fmt; -use std::fs::File; -use std::io::BufReader; +use std::fs; +use std::io; pub struct TodoList { pub todos: Vec, @@ -13,7 +13,6 @@ pub struct TodoList { #[derive(Serialize, Deserialize, Default, Clone)] pub struct Todo { - // We use owned strings here because they’re easier to manipulate when editing. text: String, done: bool, } @@ -34,16 +33,16 @@ impl fmt::Display for Todo { } fn read_todos(path: &str) -> Option> { - File::open(path) + fs::File::open(path) .ok() - .map(|f| BufReader::new(f)) + .map(io::BufReader::new) .and_then(|r| from_reader(r).ok()) } impl TodoList { pub fn open_or_create(path: &str) -> Self { Self { - todos: read_todos(path).unwrap_or(vec![Todo::new("This is a list entry")]), + todos: read_todos(path).unwrap_or_else(|| vec![Todo::new("This is a list entry")]), selected: 0, register: None, } diff --git a/src/tracc.rs b/src/tracc.rs index 336d413..6aa7b34 100644 --- a/src/tracc.rs +++ b/src/tracc.rs @@ -99,7 +99,7 @@ impl Tracc { Mode::Normal => { self.todos.normal_mode(); self.times.normal_mode(); - self.terminal.hide_cursor()? + self.terminal.hide_cursor()?; } }; self.input_mode = mode; @@ -119,7 +119,7 @@ impl Tracc { .borders(Borders::TOP | Borders::RIGHT | Borders::LEFT), ) .items(content) - .select(selected.into()) + .select(selected) .highlight_style(Style::default().fg(Color::LightGreen)) .highlight_symbol(">") } @@ -150,7 +150,7 @@ impl Tracc { Paragraph::new( [ Text::raw(format!("Sum for today: {}\n", total_time)), - Text::raw(times) + Text::raw(times), ] .iter(), ) @@ -223,10 +223,8 @@ pub trait ListView { } fn paste(&mut self) { - let register = self.register().clone(); - match register { - Some(item) => self.insert(item, None), - None => (), + if let Some(item) = self.register().clone() { + self.insert(item, None); } }