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

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

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

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

            
43
impl FromStr for Status {
44
    type Err = anyhow::Error;
45

            
46
24528
    fn from_str(value: &str) -> anyhow::Result<Self> {
47
24528
        match value {
48
24528
            "Attacker" | "Black" => Ok(Self::AttackerWins),
49
13972
            "Draw" => Ok(Self::Draw),
50
13874
            "Ongoing" => Ok(Self::Ongoing),
51
13440
            "Defender" | "White" => Ok(Self::DefenderWins),
52
            _ => Err(anyhow::Error::msg(format!("invalid status: {value}"))),
53
        }
54
24528
    }
55
}