More flexible time parsing.

This commit is contained in:
shy 2021-04-18 07:13:37 +02:00
parent e9e1806aa2
commit a1ffb07125

View file

@ -139,7 +139,6 @@ impl AlarmRoster {
// Parse string and add as alarm. // Parse string and add as alarm.
pub fn add(&mut self, input: &String) -> Result<(), &'static str> { pub fn add(&mut self, input: &String) -> Result<(), &'static str> {
let mut index = 0;
let mut time: u32 = 0; let mut time: u32 = 0;
let mut label: String; let mut label: String;
let time_str: &str; let time_str: &str;
@ -155,13 +154,12 @@ impl AlarmRoster {
} }
// Parse input into seconds. // Parse input into seconds.
if time_str.contains(':') { for (i, sub) in time_str.rsplit(':').enumerate() {
for sub in time_str.rsplit(':') {
match sub.parse::<u32>() { match sub.parse::<u32>() {
// Too many segments.
Ok(_) if i > 2 => return Err("Too many segments to parse as time."),
// Valid. // Valid.
Ok(d) if d < 60 && index < 3 => time += d * 60u32.pow(index), Ok(d) => time += d * 60u32.pow(i as u32),
// Passes as u32, but does not fit into time range.
Ok(_) => return Err("Could not parse value as time."),
// Ignore failure caused by an empty string. // Ignore failure caused by an empty string.
// TODO: Match error kind when stable. See documentation // TODO: Match error kind when stable. See documentation
// for std::num::ParseIntError and // for std::num::ParseIntError and
@ -170,14 +168,6 @@ impl AlarmRoster {
// Could not parse to u32. // Could not parse to u32.
Err(_) => return Err("Could not parse value as integer."), Err(_) => return Err("Could not parse value as integer."),
} }
index += 1;
}
} else {
// Parse as seconds only.
match time_str.parse::<u32>() {
Ok(d) => time = d,
Err(_) => return Err("Could not parse as integer."),
}
} }
// Skip if time is out of boundaries. // Skip if time is out of boundaries.
@ -187,9 +177,9 @@ impl AlarmRoster {
if time >= 24 * 60 * 60 { if time >= 24 * 60 * 60 {
return Err("Values >24h not supported."); return Err("Values >24h not supported.");
}; };
// Filter out double entries. // Filter out duplicate entries.
if self.list.iter().any(|a| a.time == time) { if self.list.iter().any(|a| a.time == time) {
return Err("Already exists."); return Err("Already exists. Duplicate entries not supported.");
} }
// Label will never change from now on. // Label will never change from now on.