1
// This file is part of hnefatafl-copenhagen.
2
//
3
// hnefatafl-copenhagen is free software: you can redistribute it and/or modify
4
// it under the terms of the GNU Affero General Public License as published by
5
// the Free Software Foundation, either version 3 of the License, or
6
// (at your option) any later version.
7
//
8
// hnefatafl-copenhagen is distributed in the hope that it will be useful,
9
// but WITHOUT ANY WARRANTY; without even the implied warranty of
10
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11
// GNU Affero General Public License for more details.
12
//
13
// You should have received a copy of the GNU Affero General Public License
14
// along with this program.  If not, see <https://www.gnu.org/licenses/>.
15

            
16
use std::{fmt, ops::Not, str::FromStr};
17

            
18
use serde::{Deserialize, Serialize};
19

            
20
#[derive(Clone, Copy, Debug, Default, Deserialize, Eq, PartialEq, Serialize)]
21
pub enum Rated {
22
    No,
23
    #[default]
24
    Yes,
25
}
26

            
27
impl fmt::Display for Rated {
28
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
29
        match self {
30
            Rated::No => write!(f, "unrated"),
31
            Rated::Yes => write!(f, "rated"),
32
        }
33
    }
34
}
35

            
36
impl Not for Rated {
37
    type Output = Rated;
38

            
39
    fn not(self) -> Self::Output {
40
        match self {
41
            Rated::No => Rated::Yes,
42
            Rated::Yes => Rated::No,
43
        }
44
    }
45
}
46

            
47
impl From<bool> for Rated {
48
    fn from(boolean: bool) -> Self {
49
        if boolean { Self::Yes } else { Self::No }
50
    }
51
}
52

            
53
impl From<Rated> for bool {
54
    fn from(rated: Rated) -> Self {
55
        match rated {
56
            Rated::Yes => true,
57
            Rated::No => false,
58
        }
59
    }
60
}
61

            
62
impl FromStr for Rated {
63
    type Err = anyhow::Error;
64

            
65
    fn from_str(string: &str) -> anyhow::Result<Self> {
66
        match string {
67
            "rated" => Ok(Self::Yes),
68
            "unrated" => Ok(Self::No),
69
            _ => Err(anyhow::Error::msg(format!(
70
                "Error trying to convert '{string}' to Rated!"
71
            ))),
72
        }
73
    }
74
}