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
// SPDX-License-Identifier: AGPL-3.0-or-later
17
// SPDX-FileCopyrightText: 2025 David Campbell <david@hnefatafl.org>
18

            
19
use std::{fmt, ops::Not, str::FromStr};
20

            
21
use serde::{Deserialize, Serialize};
22

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

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

            
39
impl Not for Rated {
40
    type Output = Rated;
41

            
42
    fn not(self) -> Self::Output {
43
        match self {
44
            Rated::No => Rated::Yes,
45
            Rated::Yes => Rated::No,
46
        }
47
    }
48
}
49

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

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

            
65
impl FromStr for Rated {
66
    type Err = anyhow::Error;
67

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