poke_data/models/
generation.rs

1use crate::data::link_context::LinkContext;
2use crate::data::linkable::Linkable;
3use crate::models::localized_names::LocalizedStrings;
4use crate::models::region::{Region, RegionId};
5use crate::traits::has_identifier::HasIdentifier;
6use crate::traits::has_localized_names::HasLocalizedNames;
7use serde::{Deserialize, Serialize};
8use std::sync::Arc;
9
10pub type GenerationId = u16;
11
12#[derive(Debug)]
13pub struct Generation {
14    pub id: GenerationId,
15    pub identifier: String,
16    pub main_region: Arc<Region>,
17    pub names: LocalizedStrings,
18}
19
20#[derive(Debug, Clone, Serialize, Deserialize)]
21pub struct UnlinkedGeneration {
22    pub id: GenerationId,
23    pub identifier: String,
24    pub main_region_id: RegionId,
25    pub names: LocalizedStrings,
26}
27
28impl Linkable for UnlinkedGeneration {
29    type Linked = Arc<Generation>;
30
31    fn link(&self, context: &LinkContext) -> Self::Linked {
32        let main_region = context
33            .regions
34            .get(&self.main_region_id)
35            .unwrap_or_else(|| {
36                panic!(
37                    "No region '{}' found for generation '{}'",
38                    self.main_region_id, self.id
39                )
40            })
41            .clone();
42
43        let generation = Generation {
44            id: self.id,
45            identifier: self.identifier.clone(),
46            main_region,
47            names: self.names.clone(),
48        };
49
50        Arc::new(generation)
51    }
52}
53
54impl HasLocalizedNames for Generation {
55    fn localized_names(&self) -> &LocalizedStrings {
56        &self.names
57    }
58}
59
60impl HasIdentifier for Generation {
61    fn identifier(&self) -> &str {
62        &self.identifier
63    }
64}