boseiju/utils/
parsing.rs

1pub fn is_digits(input: &str) -> bool {
2    input.chars().all(|c| c.is_digit(10))
3}
4
5pub fn parse_num(input: &str) -> Option<u32> {
6    match input {
7        "one" => Some(1),
8        "two" => Some(2),
9        "three" => Some(3),
10        "four" => Some(4),
11        "five" => Some(5),
12        "six" => Some(6),
13        "seven" => Some(7),
14        "eight" => Some(8),
15        "nine" => Some(9),
16        "ten" => Some(10),
17        "thirteen" => Some(13),
18        other => {
19            /* Reject numbers with +/- signs, as we want separate tokens for those */
20            if other.starts_with('+') || other.starts_with('-') {
21                return None;
22            }
23            other.parse::<u32>().ok()
24        }
25    }
26}
27
28pub fn from_str_singular_or_plural<T: std::str::FromStr>(source: &str) -> Option<T> {
29    if let Ok(value) = T::from_str(source) {
30        return Some(value);
31    } else if let Some(singular) = source.strip_suffix('s') {
32        if let Ok(value) = T::from_str(singular) {
33            return Some(value);
34        }
35    }
36    None
37}