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