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
44
45
use teo_runtime::sort::Sort;

pub trait SortExt {

    fn from_desc_bool(desc: bool) -> Sort;

    fn from_mysql_str(s: &str) -> Option<Sort>;

    fn from_str(s: &str) -> Option<Sort>;

    fn to_str(&self) -> &'static str;
}

impl SortExt for Sort {

    fn from_desc_bool(desc: bool) -> Sort {
        match desc {
            true => Sort::Desc,
            false => Sort::Asc,
        }
    }

    fn from_mysql_str(s: &str) -> Option<Sort> {
        match s {
            "A" => Some(Sort::Asc),
            "D" => Some(Sort::Desc),
            _ => None,
        }
    }

    fn from_str(s: &str) -> Option<Sort> {
        match s {
            "ASC" => Some(Sort::Asc),
            "DESC" => Some(Sort::Desc),
            _ => None,
        }
    }

    fn to_str(&self) -> &'static str {
        match self {
            Sort::Asc => "ASC",
            Sort::Desc => "DESC",
        }
    }
}