1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
use crate::Nation;
use crate::ShortName;
use std::borrow::Cow;

/// The supply-center nature of a province. This information is used in the build phase
/// to determine how many units a nation can sustain and where new units can be built.
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub enum SupplyCenter {
    /// The province does not grant a build to whoever controls it.
    None,
    /// The province grants a build to its controller, but cannot be used as a build target.
    Neutral,
    /// The province grants a build to its controller, and can be used as a build target by the
    /// specified nation.
    Home(Nation),
}

/// A controllable area of the environment.
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct Province {
    pub short_name: String,
    pub supply_center: SupplyCenter,
}

impl Province {
    /// Get if the province is a supply center for whoever controls it.
    pub fn is_supply_center(&self) -> bool {
        self.supply_center != SupplyCenter::None
    }
}

impl ShortName for Province {
    fn short_name(&self) -> Cow<'_, str> {
        Cow::Borrowed(&self.short_name)
    }
}

impl PartialEq<ProvinceKey> for Province {
    fn eq(&self, other: &ProvinceKey) -> bool {
        self.short_name == other.short_name()
    }
}

/// An identifier that can be resolved to a province
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct ProvinceKey(String);

impl ProvinceKey {
    pub fn new(short_name: impl Into<String>) -> Self {
        ProvinceKey(short_name.into())
    }
}

impl<'a> From<&'a Province> for ProvinceKey {
    fn from(p: &Province) -> Self {
        ProvinceKey(p.short_name().into_owned())
    }
}

impl From<String> for ProvinceKey {
    fn from(s: String) -> Self {
        ProvinceKey(s)
    }
}

impl<'a> From<&'a str> for ProvinceKey {
    fn from(s: &str) -> Self {
        ProvinceKey(String::from(s))
    }
}

impl ShortName for ProvinceKey {
    fn short_name(&self) -> Cow<'_, str> {
        Cow::Borrowed(&self.0)
    }
}

impl<'a> From<&'a ProvinceKey> for &'a str {
    fn from(pk: &'a ProvinceKey) -> Self {
        &pk.0
    }
}

impl PartialEq<Province> for ProvinceKey {
    fn eq(&self, other: &Province) -> bool {
        self.0 == other.short_name
    }
}