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;
20

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

            
23
use crate::role::Role;
24

            
25
#[derive(Clone, Copy, Debug, Deserialize, Eq, Hash, PartialEq, Serialize)]
26
pub enum Space {
27
    Empty,
28
    Attacker,
29
    King,
30
    Defender,
31
}
32

            
33
impl Space {
34
    #[must_use]
35
    pub fn display_ascii(&self) -> &str {
36
        match self {
37
            Self::Attacker => "A",
38
            Self::Empty => ".",
39
            Self::King => "K",
40
            Self::Defender => "D",
41
        }
42
    }
43
}
44

            
45
impl TryFrom<char> for Space {
46
    type Error = anyhow::Error;
47

            
48
6013359
    fn try_from(value: char) -> anyhow::Result<Self> {
49
6013359
        match value {
50
1186866
            'X' => Ok(Self::Attacker),
51
593496
            'O' => Ok(Self::Defender),
52
4183386
            '.' => Ok(Self::Empty),
53
49611
            'K' => Ok(Self::King),
54
            ch => Err(anyhow::Error::msg(format!(
55
                "Error trying to convert '{ch}' to a Space!"
56
            ))),
57
        }
58
6013359
    }
59
}
60

            
61
impl fmt::Display for Space {
62
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
63
        match self {
64
            Self::Attacker => write!(f, "♟"),
65
            Self::Empty => write!(f, "."),
66
            Self::King => write!(f, "♔"),
67
            Self::Defender => write!(f, "♙"),
68
        }
69
    }
70
}
71

            
72
impl From<Space> for Role {
73
146762672
    fn from(space: Space) -> Self {
74
146762672
        match space {
75
45485768
            Space::Attacker => Role::Attacker,
76
12584260
            Space::Defender | Space::King => Role::Defender,
77
88692644
            Space::Empty => Role::Roleless,
78
        }
79
146762672
    }
80
}
81

            
82
impl TryFrom<Space> for usize {
83
    type Error = anyhow::Error;
84

            
85
    fn try_from(space: Space) -> Result<usize, anyhow::Error> {
86
        match space {
87
            Space::Attacker => Ok(0),
88
            Space::Defender => Ok(1),
89
            Space::King => Ok(2),
90
            Space::Empty => Err(anyhow::Error::msg(
91
                "we should not try to get a usize for an empty space",
92
            )),
93
        }
94
    }
95
}