poke_data/models/
pokedex.rs

1use crate::data::link_context::LinkContext;
2use crate::data::linkable::Linkable;
3use crate::models::localized_name_descriptions::LocalizedNameDescriptions;
4use crate::models::region::{Region, RegionId};
5use crate::models::species::{Species, SpeciesId};
6use crate::models::version_group::{VersionGroup, VersionGroupId};
7use crate::traits::has_identifier::HasIdentifier;
8use crate::traits::has_localized_name_descriptions::HasLocalizedNameDescriptions;
9use serde::{Deserialize, Serialize};
10use std::collections::BTreeMap;
11use std::sync::Arc;
12
13pub type PokedexId = u8;
14
15#[derive(Debug)]
16pub struct Pokedex {
17    pub id: PokedexId,
18    pub identifier: String,
19    pub prose: LocalizedNameDescriptions,
20    pub region: Option<Arc<Region>>,
21    pub is_main_series: bool,
22    pub version_groups: Vec<Arc<VersionGroup>>,
23    pub species_map: BTreeMap<u16, Arc<Species>>,
24}
25
26#[derive(Debug, Clone, Serialize, Deserialize)]
27pub struct UnlinkedPokedex {
28    pub id: PokedexId,
29    pub identifier: String,
30    pub prose: LocalizedNameDescriptions,
31    pub region_id: Option<RegionId>,
32    pub is_main_series: bool,
33    pub version_group_ids: Vec<VersionGroupId>,
34    pub species_map: BTreeMap<u16, SpeciesId>,
35}
36
37impl Linkable for UnlinkedPokedex {
38    type Linked = Arc<Pokedex>;
39
40    fn link(&self, context: &LinkContext) -> Self::Linked {
41        let region = self.region_id.map(|region_id| {
42            context
43                .regions
44                .get(&region_id)
45                .unwrap_or_else(|| {
46                    panic!("No region '{}' found for pokedex '{}'", region_id, self.id)
47                })
48                .clone()
49        });
50
51        let version_groups = self
52            .version_group_ids
53            .iter()
54            .map(|version_group_id| {
55                context
56                    .version_groups
57                    .get(version_group_id)
58                    .unwrap_or_else(|| {
59                        panic!(
60                            "No version group '{}' found for pokedex '{}'",
61                            version_group_id, self.id
62                        )
63                    })
64                    .clone()
65            })
66            .collect();
67
68        let species_map = self
69            .species_map
70            .iter()
71            .map(|(id, species_id)| {
72                (
73                    *id,
74                    context
75                        .species
76                        .get(species_id)
77                        .unwrap_or_else(|| {
78                            panic!(
79                                "No species '{}' found for pokedex '{}'",
80                                species_id, self.id
81                            )
82                        })
83                        .clone(),
84                )
85            })
86            .collect();
87
88        let pokedex = Pokedex {
89            id: self.id,
90            identifier: self.identifier.clone(),
91            prose: self.prose.clone(),
92            region,
93            is_main_series: false,
94            version_groups,
95            species_map,
96        };
97
98        Arc::new(pokedex)
99    }
100}
101
102impl HasIdentifier for Pokedex {
103    fn identifier(&self) -> &str {
104        &self.identifier
105    }
106}
107
108impl HasLocalizedNameDescriptions for Pokedex {
109    fn localized_name_descriptions(&self) -> &LocalizedNameDescriptions {
110        &self.prose
111    }
112}