diff options
author | Joris | 2022-02-26 18:57:55 +0100 |
---|---|---|
committer | Joris | 2022-02-26 18:57:55 +0100 |
commit | f9f49285c5ecc76d3edfb0a54ffab53c2e296d7f (patch) | |
tree | f77f9b625446de7f0b9de1553fc52d702c4cbc69 | |
parent | 2d80413609130f1c121dcae39a150a27dd9f02ea (diff) |
Apply linter advices
-rw-r--r-- | flake.nix | 4 | ||||
-rw-r--r-- | src/gui/app.rs (renamed from src/app/app.rs) | 8 | ||||
-rw-r--r-- | src/gui/calendar.rs (renamed from src/app/calendar.rs) | 22 | ||||
-rw-r--r-- | src/gui/form/mod.rs (renamed from src/app/form/mod.rs) | 22 | ||||
-rw-r--r-- | src/gui/form/repetition.rs (renamed from src/app/form/repetition.rs) | 0 | ||||
-rw-r--r-- | src/gui/mod.rs (renamed from src/app/mod.rs) | 2 | ||||
-rw-r--r-- | src/gui/style.css (renamed from src/app/style.css) | 0 | ||||
-rw-r--r-- | src/gui/update.rs (renamed from src/app/update.rs) | 21 | ||||
-rw-r--r-- | src/gui/utils.rs (renamed from src/app/utils.rs) | 0 | ||||
-rw-r--r-- | src/main.rs | 5 | ||||
-rw-r--r-- | src/model/event.rs | 8 | ||||
-rw-r--r-- | src/model/repetition.rs | 2 |
12 files changed, 50 insertions, 44 deletions
@@ -19,9 +19,7 @@ { devShell = mkShell { buildInputs = [ - (rust-bin.stable."1.58.0".default.override { - extensions = [ "rust-src" ]; - }) + rust-bin.stable."1.58.0".default rust-analyzer cargo-watch pkgconfig diff --git a/src/app/app.rs b/src/gui/app.rs index 58240af..ebaceb3 100644 --- a/src/app/app.rs +++ b/src/gui/app.rs @@ -7,8 +7,8 @@ use gtk::prelude::*; use rusqlite::Connection; use std::rc::Rc; -use crate::app::calendar; -use crate::app::update::Msg; +use crate::gui::calendar; +use crate::gui::update::Msg; use crate::{db, model::event::Event}; pub struct App { @@ -40,8 +40,8 @@ impl App { NaiveDate::from_isoywd(today.year(), today.iso_week().week(), Weekday::Mon); let end_date = start_date + Duration::days(7 * 4 - 1); - let events = db::list_non_repeated_between(&conn, start_date, end_date).unwrap_or(vec![]); - let repeated_events = db::list_repeated(&conn).unwrap_or(vec![]); + let events = db::list_non_repeated_between(&conn, start_date, end_date).unwrap_or_default(); + let repeated_events = db::list_repeated(&conn).unwrap_or_default(); let grid = calendar::create( tx.clone(), diff --git a/src/app/calendar.rs b/src/gui/calendar.rs index 11eb893..918438d 100644 --- a/src/app/calendar.rs +++ b/src/gui/calendar.rs @@ -6,7 +6,7 @@ use gtk::glib; use gtk::prelude::*; use std::collections::HashMap; -use crate::{app::update, app::update::Msg, app::App, model::event, model::event::Event}; +use crate::{gui::update, gui::update::Msg, gui::App, model::event, model::event::Event}; static DAYS: [&str; 7] = ["LUN", "MAR", "MER", "JEU", "VEN", "SAM", "DIM"]; static MONTHES: [&str; 12] = [ @@ -18,8 +18,8 @@ pub fn create( today: NaiveDate, start_date: NaiveDate, end_date: NaiveDate, - events: &Vec<Event>, - repeated_events: &Vec<Event>, + events: &[Event], + repeated_events: &[Event], ) -> gtk::Grid { let grid = gtk::Grid::builder().build(); @@ -38,7 +38,7 @@ fn attach_days( grid: >k::Grid, start_date: NaiveDate, today: NaiveDate, - events: &Vec<Event>, + events: &[Event], repetitions: &HashMap<NaiveDate, Vec<Event>>, ) { let mut d = start_date; @@ -89,7 +89,7 @@ pub fn day_entry( tx: Sender<Msg>, date: NaiveDate, today: NaiveDate, - events: &Vec<Event>, + events: &[Event], repetitions: &HashMap<NaiveDate, Vec<Event>>, ) -> gtk::ScrolledWindow { let vbox = gtk::Box::builder() @@ -116,7 +116,7 @@ pub fn day_entry( .iter() .filter(|e| e.date == date) .collect::<Vec<&Event>>(); - let repeated_events = repetitions.get(&date).map(|e| e.clone()).unwrap_or(vec![]); + let repeated_events = repetitions.get(&date).cloned().unwrap_or_default(); events.extend(repeated_events.iter()); events.sort_by_key(|e| e.start); @@ -124,14 +124,12 @@ pub fn day_entry( vbox.append(&day_events(tx, events)); } - let scrolled_window = gtk::ScrolledWindow::builder() + gtk::ScrolledWindow::builder() .hscrollbar_policy(gtk::PolicyType::Never) .hexpand(true) .vexpand(true) .child(&vbox) - .build(); - - scrolled_window + .build() } fn day_label(date: NaiveDate) -> gtk::Label { @@ -175,9 +173,9 @@ fn day_events(tx: Sender<Msg>, events: Vec<&Event>) -> gtk::Box { let event_txt = &event.pprint(); let label = gtk::Label::builder() - .label(&event_txt) + .label(event_txt) .ellipsize(gtk::pango::EllipsizeMode::End) - .tooltip_text(&event_txt) + .tooltip_text(event_txt) .halign(gtk::Align::Start) .build(); diff --git a/src/app/form/mod.rs b/src/gui/form/mod.rs index 9cb6ba7..4bf6f90 100644 --- a/src/app/form/mod.rs +++ b/src/gui/form/mod.rs @@ -6,8 +6,8 @@ use gtk::glib; use gtk::prelude::*; use crate::{ - app::{update, update::Msg, App}, db, + gui::{update, update::Msg, App}, model::{event, event::Event}, }; @@ -52,12 +52,17 @@ pub async fn show(app: &App, event: Event, is_new: bool) { &event .start .map(event::pprint_time) - .unwrap_or("".to_string()), + .unwrap_or_else(|| "".to_string()), ); column1.append(&label("Début")); column1.append(&start); - let end = entry(&event.end.map(event::pprint_time).unwrap_or("".to_string())); + let end = entry( + &event + .end + .map(event::pprint_time) + .unwrap_or_else(|| "".to_string()), + ); column1.append(&label("Fin")); column1.append(&end); @@ -76,7 +81,7 @@ pub async fn show(app: &App, event: Event, is_new: bool) { let conn = app.conn.clone(); let tx = app.tx.clone(); button.connect_clicked(glib::clone!(@weak dialog, @strong event => move |_| { - let repetition = repetition::validate(&repetition_model).clone(); + let repetition = repetition::validate(&repetition_model); match event::validate(event.id, date.buffer().text(), name.buffer().text(), start.buffer().text(), end.buffer().text(), repetition) { Some(new) => { match if is_new { db::insert(&conn, &new) } else { db::update(&conn, &new) } { @@ -98,12 +103,9 @@ pub async fn show(app: &App, event: Event, is_new: bool) { let conn = app.conn.clone(); let tx = app.tx.clone(); button.connect_clicked(glib::clone!(@weak dialog => move |_| { - match db::delete(&conn, &event.id) { - Ok(_) => { - update::send(tx.clone(), Msg::DeleteEvent { event: event.clone() }); - dialog.close() - }, - Err(_) => () + if db::delete(&conn, &event.id).is_ok() { + update::send(tx.clone(), Msg::DeleteEvent { event: event.clone() }); + dialog.close() } })); } diff --git a/src/app/form/repetition.rs b/src/gui/form/repetition.rs index 87c8d84..87c8d84 100644 --- a/src/app/form/repetition.rs +++ b/src/gui/form/repetition.rs diff --git a/src/app/mod.rs b/src/gui/mod.rs index c9a7f83..a8a025c 100644 --- a/src/app/mod.rs +++ b/src/gui/mod.rs @@ -23,7 +23,7 @@ pub fn run(conn: Connection) { fn build_ui(conn: Rc<Connection>, app: >k::Application) { let (tx, rx) = async_channel::unbounded(); - let app = App::new(conn.clone(), app, tx.clone()); + let app = App::new(conn, app, tx); utils::spawn(update::event_handler(rx, app)) } diff --git a/src/app/style.css b/src/gui/style.css index 4828e41..4828e41 100644 --- a/src/app/style.css +++ b/src/gui/style.css diff --git a/src/app/update.rs b/src/gui/update.rs index 4ef1eb1..91102bf 100644 --- a/src/app/update.rs +++ b/src/gui/update.rs @@ -3,7 +3,7 @@ use chrono::NaiveDate; use std::collections::HashSet; use crate::{ - app::{calendar, form, utils, App}, + gui::{calendar, form, utils, App}, model::{event, event::Event}, }; @@ -37,7 +37,13 @@ pub async fn event_handler(rx: Receiver<Msg>, mut app: App) { Msg::UpdateEvent { old, new } => { let refresh_dates_1 = remove(&mut app, &old); let refresh_dates_2 = add(&mut app, &new); - refresh(&app, &refresh_dates_1.union(&refresh_dates_2).map(|d| *d).collect::<HashSet<NaiveDate>>()) + refresh( + &app, + &refresh_dates_1 + .union(&refresh_dates_2) + .copied() + .collect::<HashSet<NaiveDate>>(), + ) } Msg::DeleteEvent { event } => { let refresh_dates = remove(&mut app, &event); @@ -53,7 +59,7 @@ fn remove(app: &mut App, event: &Event) -> HashSet<NaiveDate> { match app.repeated_events.iter().position(|e| e.id == event.id) { Some(index) => { app.repeated_events.remove(index); - let mut dates = repetition_dates(&app, event); + let mut dates = repetition_dates(app, event); dates.insert(event.date); dates } @@ -80,7 +86,7 @@ fn remove(app: &mut App, event: &Event) -> HashSet<NaiveDate> { fn add(app: &mut App, event: &Event) -> HashSet<NaiveDate> { if event.repetition.is_some() { app.repeated_events.push(event.clone()); - let mut dates = repetition_dates(&app, event); + let mut dates = repetition_dates(app, event); dates.insert(event.date); dates } else { @@ -91,13 +97,14 @@ fn add(app: &mut App, event: &Event) -> HashSet<NaiveDate> { /// Repetition dates of a repetead event. fn repetition_dates(app: &App, event: &Event) -> HashSet<NaiveDate> { - let event_reps = event::repetitions_between(&vec!(event.clone()), app.start_date, app.end_date); - HashSet::from_iter(event_reps.keys().map(|d| *d)) + let event_reps = event::repetitions_between(&[event.clone()], app.start_date, app.end_date); + HashSet::from_iter(event_reps.keys().copied()) } /// Refresh app for the given dates. fn refresh(app: &App, dates: &HashSet<NaiveDate>) { - let repetitions = event::repetitions_between(&app.repeated_events, app.start_date, app.end_date); + let repetitions = + event::repetitions_between(&app.repeated_events, app.start_date, app.end_date); for date in dates { calendar::refresh_date(app, *date, &repetitions) diff --git a/src/app/utils.rs b/src/gui/utils.rs index 673b96e..673b96e 100644 --- a/src/app/utils.rs +++ b/src/gui/utils.rs diff --git a/src/main.rs b/src/main.rs index f5f4861..8999a52 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,10 +1,11 @@ -mod app; mod db; +mod gui; mod model; use anyhow::Result; fn main() -> Result<()> { let conn = db::init()?; - Ok(app::run(conn)) + gui::run(conn); + Ok(()) } diff --git a/src/model/event.rs b/src/model/event.rs index b18d811..249d077 100644 --- a/src/model/event.rs +++ b/src/model/event.rs @@ -46,16 +46,16 @@ impl Event { /// Repeated events in an included date range pub fn repetitions_between( - events: &Vec<Event>, + events: &[Event], start: NaiveDate, end: NaiveDate, ) -> HashMap<NaiveDate, Vec<Event>> { let mut res: HashMap<NaiveDate, Vec<Event>> = HashMap::new(); for event in events { - for repetition in event.repetition.as_ref() { + if let Some(repetition) = &event.repetition { for date in repetition.between(event.date, start, end) { - res.entry(date).or_insert(vec![]).push(event.clone()) + res.entry(date).or_insert_with(Vec::new).push(event.clone()) } } } @@ -117,7 +117,7 @@ fn validate_time(time: String) -> Option<Option<NaiveTime>> { if time.is_empty() { Some(None) } else { - parse_time(time).map(|t| Some(t)) + parse_time(time).map(Some) } } diff --git a/src/model/repetition.rs b/src/model/repetition.rs index ceb903b..d21dd70 100644 --- a/src/model/repetition.rs +++ b/src/model/repetition.rs @@ -16,7 +16,7 @@ pub enum DayOfMonth { pub fn validate_day(str: &str) -> Option<u8> { let n = str.parse::<u8>().ok()?; - if n >= 1 && n <= 31 { + if (1..=31).contains(&n) { Some(n) } else { None |