Skip to content

Commit

Permalink
Safely unwrap std::option
Browse files Browse the repository at this point in the history
  • Loading branch information
mxdamien committed Jun 30, 2023
1 parent 569eace commit be9bb33
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 37 deletions.
43 changes: 22 additions & 21 deletions src/timetablepresenterconsole.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
}

Expand All @@ -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");
}
}

Expand Down
37 changes: 21 additions & 16 deletions src/timetablepresenterjson.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<TimetableStop> {
Expand Down Expand Up @@ -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 {
Expand All @@ -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()
}
}

Expand Down

0 comments on commit be9bb33

Please sign in to comment.