From f58c120bcc0a84156e5e253be57c1f958ea8576e Mon Sep 17 00:00:00 2001 From: Joris Date: Sun, 27 Feb 2022 09:36:14 +0100 Subject: Don’t create an event if repetition is not valid --- src/gui/form/mod.rs | 26 +++++++++++++++----------- src/gui/form/repetition.rs | 23 ++++++++++------------- 2 files changed, 25 insertions(+), 24 deletions(-) (limited to 'src/gui/form') diff --git a/src/gui/form/mod.rs b/src/gui/form/mod.rs index 4bf6f90..57ccac7 100644 --- a/src/gui/form/mod.rs +++ b/src/gui/form/mod.rs @@ -81,19 +81,23 @@ 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); - 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) } { - Ok(_) => { - let msg = if is_new { Msg::AddEvent { new } } else { Msg::UpdateEvent { old: event.clone(), new } }; - update::send(tx.clone(), msg); - dialog.close() - }, - Err(err) => eprintln!("Error when upserting event: {err}") + match repetition::validate(&repetition_model) { + Ok(repetition) => { + 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) } { + Ok(_) => { + let msg = if is_new { Msg::AddEvent { new } } else { Msg::UpdateEvent { old: event.clone(), new } }; + update::send(tx.clone(), msg); + dialog.close() + }, + Err(err) => eprintln!("Error when upserting event: {err}") + } + } + None => eprintln!("Event is not valid: {event:?}") } }, - None => eprintln!("Event is not valid: {event:?}") + Err(message) => eprintln!("{message}") } })); diff --git a/src/gui/form/repetition.rs b/src/gui/form/repetition.rs index 87c8d84..7236016 100644 --- a/src/gui/form/repetition.rs +++ b/src/gui/form/repetition.rs @@ -126,26 +126,23 @@ fn label(text: &str) -> gtk::Label { .build() } -pub fn validate(model: &Model) -> Option { +pub fn validate(model: &Model) -> Result, String> { if model.no_radio.is_active() { - None + Ok(None) } else if model.day_interval_radio.is_active() { - repetition::validate_day(&model.day_interval_entry.buffer().text()) - .map(|d| Repetition::Daily { period: d }) + let period = repetition::validate_period(&model.day_interval_entry.buffer().text())?; + Ok(Some(Repetition::Daily { period })) } else if model.monthly_radio.is_active() { - repetition::validate_day(&model.monthly_entry.buffer().text()).map(|d| { - Repetition::Monthly { - day: DayOfMonth::Day { day: d }, - } - }) + let day = repetition::validate_day(&model.monthly_entry.buffer().text())?; + Ok(Some(Repetition::Monthly { day: DayOfMonth::Day { day } })) } else if model.first_day_radio.is_active() { let weekday = WEEKDAYS[model.first_day_dropdown.selected() as usize]; - Some(Repetition::Monthly { + Ok(Some(Repetition::Monthly { day: DayOfMonth::Weekday { weekday }, - }) + })) } else if model.yearly_radio.is_active() { - Some(Repetition::Yearly) + Ok(Some(Repetition::Yearly)) } else { - None + Err("Aucune option n’a été sélectionnée".to_string()) } } -- cgit v1.2.3