From be9bb336a8328e0263bc394a164b2fdc6dc93c04 Mon Sep 17 00:00:00 2001 From: David Langhals Date: Fri, 30 Jun 2023 11:25:54 +0200 Subject: [PATCH] Safely unwrap std::option --- src/timetablepresenterconsole.rs | 43 ++++++++++++++++---------------- src/timetablepresenterjson.rs | 37 +++++++++++++++------------ 2 files changed, 43 insertions(+), 37 deletions(-) diff --git a/src/timetablepresenterconsole.rs b/src/timetablepresenterconsole.rs index 29922f4..414956d 100644 --- a/src/timetablepresenterconsole.rs +++ b/src/timetablepresenterconsole.rs @@ -21,22 +21,22 @@ impl TimetablePresenter for TimetablePresenterConsole { } fn print_timetablestop(timetable: &Timetable) { - timetable.s.as_ref().unwrap().iter().for_each(|s| { - print_departure(s); - }); + if let Some(s) = &timetable.s { + for item in s.iter() { + print_departure(item); + } + } else { + println!("Timetable is empty."); + } } fn print_departure(s: &crate::timetable::TimetableStop) { - match &s.dp { - Some(dp) => match &s.tl { - Some(tl) => { - print_train_info(tl, dp); - print_time_info(dp); - print_seperator_lines(1); - } - None => (), - }, - None => (), + if let Some(dp) = &s.dp { + if let Some(tl) = &s.tl { + print_train_info(tl, dp); + print_time_info(dp); + print_seperator_lines(1); + } } } @@ -46,17 +46,18 @@ fn print_time_info(dp: &ArrivalDeparture) { } fn print_planned_time(dp: &ArrivalDeparture) { - println!( - "Planned time: {}", - NaiveDateTime::parse_from_str(dp.pt.as_ref().unwrap_or(&"-".to_string()), "%y%m%d%H%M") - .unwrap() - ); + let pt = dp.pt.as_deref().unwrap_or("-"); + if let Ok(dt) = NaiveDateTime::parse_from_str(pt, "%y%m%d%H%M") { + println!("Planned time: {}", dt); + } } fn print_changed_time(dp: &crate::timetable::ArrivalDeparture) { - match NaiveDateTime::parse_from_str(dp.ct.as_ref().unwrap_or(&"-".to_string()), "%y%m%d%H%M") { - Ok(dt) => println!("Actual time: {}", dt), - _ => println!("Actual time: No delay"), + let ct = dp.ct.as_deref().unwrap_or("-"); + if let Ok(dt) = NaiveDateTime::parse_from_str(ct, "%y%m%d%H%M") { + println!("Actual time: {}", dt); + } else { + println!("Actual time: No delay"); } } diff --git a/src/timetablepresenterjson.rs b/src/timetablepresenterjson.rs index 040da58..9653639 100644 --- a/src/timetablepresenterjson.rs +++ b/src/timetablepresenterjson.rs @@ -33,11 +33,11 @@ impl TimetablePresenter for TimetablePresenterJson { } fn get_station_name(timetable: &Timetable) -> String { - timetable - .station - .as_ref() - .unwrap_or(&"Station name missing".to_string()) - .to_string() + if let Some(station) = &timetable.station { + station.to_string() + } else { + "Station name missing".to_string() + } } fn get_stops(timetable: &Timetable, station: &String) -> Vec { @@ -79,13 +79,13 @@ fn get_train_name( let l = dp.l.as_ref().unwrap_or(&"".to_string()).to_string(); let n = tl.n.as_ref().unwrap_or(&"".to_string()).to_string(); - return match (!c.is_empty(), !l.is_empty(), !n.is_empty()) { + match (!c.is_empty(), !l.is_empty(), !n.is_empty()) { (false, false, true) => n, (false, true, true) => format!("{}{}", l, n), (true, false, true) => format!("{}{}", c, n), (true, true, _) => format!("{}{}", c, l), _ => "Train name missing".to_string(), - }; + } } fn get_train_end_station(dp: &ArrivalDeparture) -> String { @@ -99,17 +99,22 @@ fn get_train_end_station(dp: &ArrivalDeparture) -> String { } fn get_planned_time(dp: &ArrivalDeparture) -> String { - format!( - "{}", - NaiveDateTime::parse_from_str(dp.pt.as_ref().unwrap_or(&"-".to_string()), "%y%m%d%H%M") - .unwrap() - ) + if let Some(pt) = &dp.pt { + NaiveDateTime::parse_from_str(pt, "%y%m%d%H%M") + .map(|dt| dt.to_string()) + .unwrap_or_else(|_| "-".to_string()) + } else { + "-".to_string() + } } -fn get_changed_time(dp: &crate::timetable::ArrivalDeparture) -> String { - match NaiveDateTime::parse_from_str(dp.ct.as_ref().unwrap_or(&"-".to_string()), "%y%m%d%H%M") { - Ok(dt) => dt.to_string(), - _ => "No delay".to_string(), +fn get_changed_time(dp: &ArrivalDeparture) -> String { + if let Some(ct) = &dp.ct { + NaiveDateTime::parse_from_str(ct, "%y%m%d%H%M") + .map(|dt| dt.to_string()) + .unwrap_or_else(|_| "No delay".to_string()) + } else { + "No delay".to_string() } }