69 lines
2.1 KiB
Rust
69 lines
2.1 KiB
Rust
|
fn number_to_string<'a, 'b>(number: u8) -> Result<&'a str, &'b str> {
|
||
|
match number {
|
||
|
1 => Ok("one"),
|
||
|
2 => Ok("two"),
|
||
|
3 => Ok("three"),
|
||
|
4 => Ok("four"),
|
||
|
5 => Ok("five"),
|
||
|
6 => Ok("six"),
|
||
|
7 => Ok("seven"),
|
||
|
8 => Ok("eight"),
|
||
|
9 => Ok("nine"),
|
||
|
10 => Ok("ten"),
|
||
|
11 => Ok("eleven"),
|
||
|
12 => Ok("twelve"),
|
||
|
13 => Ok("thirteen"),
|
||
|
14 => Ok("fourteen"),
|
||
|
16 => Ok("sixteen"),
|
||
|
17 => Ok("seventeen"),
|
||
|
18 => Ok("eighteen"),
|
||
|
19 => Ok("nineteen"),
|
||
|
20 => Ok("twenty"),
|
||
|
21 => Ok("twentyone"),
|
||
|
22 => Ok("twentytwo"),
|
||
|
23 => Ok("twentythree"),
|
||
|
24 => Ok("twentyfour"),
|
||
|
25 => Ok("twentyfive"),
|
||
|
26 => Ok("twentysix"),
|
||
|
27 => Ok("twentyseven"),
|
||
|
28 => Ok("twentyeight"),
|
||
|
29 => Ok("twentynine"),
|
||
|
_ => Err("Number not found"),
|
||
|
}
|
||
|
}
|
||
|
|
||
|
fn minute_to_string<'a>(minutes: u8) -> Result<String, &'a str> {
|
||
|
match minutes {
|
||
|
0 => Ok(String::from("o' clock")),
|
||
|
1 => Ok(String::from("one minute past")),
|
||
|
15 => Ok(String::from("quarter past")),
|
||
|
30 => Ok(String::from("half past")),
|
||
|
45 => Ok(String::from("quarter to")),
|
||
|
59 => Ok(String::from("one minute to")),
|
||
|
2..30 => Ok(format!("{} minutes past", number_to_string(minutes)?)),
|
||
|
31..59 => Ok(format!("{} minutes to", number_to_string(60 - minutes)?)),
|
||
|
_ => Err("Minute not found"),
|
||
|
}
|
||
|
}
|
||
|
|
||
|
pub fn time_to_string<'a>(hours: u8, minutes: u8) -> Result<String, &'a str> {
|
||
|
if (1..=12).contains(&hours) && (0..60).contains(&minutes) {
|
||
|
let hours = hours + if minutes > 30 { 1 } else { 0 };
|
||
|
if minutes == 0 {
|
||
|
Ok(format!(
|
||
|
"{} {}",
|
||
|
number_to_string(hours)?,
|
||
|
minute_to_string(minutes)?
|
||
|
))
|
||
|
} else {
|
||
|
Ok(format!(
|
||
|
"{} {}",
|
||
|
minute_to_string(minutes)?,
|
||
|
number_to_string(hours)?
|
||
|
))
|
||
|
}
|
||
|
} else {
|
||
|
Err("Time not valid")
|
||
|
}
|
||
|
}
|