summaryrefslogtreecommitdiffstats
path: root/lib/domain/libimagtodo/src/priority.rs
blob: e612093db783e703500b3ecfcdad51bf0b710b04 (plain)
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
//
// imag - the personal information management suite for the commandline
// Copyright (C) 2015-2019 Matthias Beyer <mail@beyermatthias.de> and contributors
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; version
// 2.1 of the License.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
//

use std::cmp::PartialOrd;
use std::cmp::Ordering;

#[derive(Serialize, Deserialize, Clone, Debug, Eq, PartialEq)]
pub enum Priority {
    #[serde(rename = "h")]
    High,

    #[serde(rename = "m")]
    Medium,

    #[serde(rename = "l")]
    Low,
}

impl Priority {
    pub fn as_str(&self) -> &str {
        match self {
            Priority::High => "h",
            Priority::Medium => "m",
            Priority::Low => "l",
        }
    }
}

impl PartialOrd for Priority {
    fn partial_cmp(&self, other: &Priority) -> Option<Ordering> {
        Some(match (self, other) {
            (Priority::Low,    Priority::Low)    => Ordering::Equal,
            (Priority::Low,    Priority::Medium) => Ordering::Less,
            (Priority::Low,    Priority::High)   => Ordering::Less,

            (Priority::Medium, Priority::Low)    => Ordering::Greater,
            (Priority::Medium, Priority::Medium) => Ordering::Equal,
            (Priority::Medium, Priority::High)   => Ordering::Less,

            (Priority::High,   Priority::Low)    => Ordering::Greater,
            (Priority::High,   Priority::Medium) => Ordering::Greater,
            (Priority::High,   Priority::High)   => Ordering::Equal,
        })
    }
}

impl Ord for Priority {
    fn cmp(&self, other: &Self) -> Ordering {
        self.partial_cmp(&other).unwrap() // save by impl above
    }
}