1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
use {
    std::{
        fmt,
        str::FromStr,
    },
};

/// one of the two sorting directions
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum Order {
    Asc,
    Desc,
}


#[derive(Debug)]
pub struct ParseOrderError {
    /// the string which couldn't be parsed
    pub raw: String,
}
impl ParseOrderError {
    pub fn new<S: Into<String>>(s: S) -> Self {
        Self { raw: s.into() }
    }
}
impl fmt::Display for ParseOrderError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{:?} can't be parsed as a sort order. Use 'asc' or 'desc' (or nothing)", self.raw)
    }
}
impl std::error::Error for ParseOrderError {}

impl FromStr for Order {
    type Err = ParseOrderError;
    fn from_str(s: &str) -> Result<Self, ParseOrderError> {
        let s = s.to_lowercase();
        match s.as_ref() {
            "a" | "asc" => Ok(Self::Asc),
            "d" | "desc" => Ok(Self::Desc),
            _ => Err(ParseOrderError::new(s))
        }
    }
}