Skip to main content

nil_core/continent/
field.rs

1// Copyright (C) Call of Nil contributors
2// SPDX-License-Identifier: AGPL-3.0-only
3
4use crate::city::{City, PublicCity};
5use serde::{Deserialize, Serialize};
6use strum::EnumIs;
7
8#[derive(Clone, Debug, Default, Deserialize, Serialize, EnumIs)]
9#[serde(tag = "kind", rename_all = "kebab-case")]
10pub enum Field {
11  #[default]
12  Empty,
13  City {
14    city: Box<City>,
15  },
16}
17
18impl Field {
19  #[inline]
20  pub fn city(&self) -> Option<&City> {
21    if let Self::City { city } = self { Some(city) } else { None }
22  }
23
24  pub(super) fn city_mut(&mut self) -> Option<&mut City> {
25    if let Self::City { city } = self { Some(city) } else { None }
26  }
27}
28
29impl From<City> for Field {
30  fn from(city: City) -> Self {
31    Self::City { city: Box::new(city) }
32  }
33}
34
35/// Public data about a field, to which any player can have access.
36#[derive(Clone, Debug, Default, Deserialize, Serialize, EnumIs)]
37#[serde(tag = "kind", rename_all = "kebab-case")]
38pub enum PublicField {
39  #[default]
40  Empty,
41  City {
42    city: PublicCity,
43  },
44}
45
46impl From<&Field> for PublicField {
47  fn from(field: &Field) -> Self {
48    match field {
49      Field::Empty => Self::Empty,
50      Field::City { city } => Self::City { city: PublicCity::from(&**city) },
51    }
52  }
53}