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, str::FromStr};
17

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

            
20
#[derive(Clone, Debug, Default, Deserialize, Eq, PartialEq, Serialize)]
21
pub enum Status {
22
    AttackerWins,
23
    Draw,
24
    #[default]
25
    Ongoing,
26
    DefenderWins,
27
}
28

            
29
impl fmt::Display for Status {
30
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
31
        match self {
32
            Self::AttackerWins => write!(f, "attacker_wins"),
33
            Self::Draw => write!(f, "draw"),
34
            Self::Ongoing => write!(f, "ongoing"),
35
            Self::DefenderWins => write!(f, "defender_wins"),
36
        }
37
    }
38
}
39

            
40
impl FromStr for Status {
41
    type Err = anyhow::Error;
42

            
43
61320
    fn from_str(value: &str) -> anyhow::Result<Self> {
44
61320
        match value {
45
61320
            "Attacker" | "Black" => Ok(Self::AttackerWins),
46
34930
            "Draw" => Ok(Self::Draw),
47
34685
            "Ongoing" => Ok(Self::Ongoing),
48
33600
            "Defender" | "White" => Ok(Self::DefenderWins),
49
            _ => Err(anyhow::Error::msg(format!("invalid status: {value}"))),
50
        }
51
61320
    }
52
}