poke_data/collections/
items.rs1use crate::data::link_context::LinkContext;
2use crate::data_structures::entity_collection::{EntityCollection, HasNameSearchIndex};
3use crate::data_structures::string_search_index::StringSearchIndex;
4use crate::models::item::{Item, ItemId};
5use crate::traits::has_identifier::IdentifierDictionary;
6use crate::traits::has_localized_names::LocalizedNamesDictionary;
7use std::collections::HashMap;
8use std::sync::Arc;
9
10pub struct ItemsCollection {
11 entities: HashMap<ItemId, Arc<Item>>,
12 name_search_index: StringSearchIndex<ItemId>,
13}
14
15impl EntityCollection<ItemId, Item> for ItemsCollection {
16 fn new(context: &LinkContext) -> Self {
17 let entities = context.items.clone();
18 let mut dictionary = HashMap::new();
19 dictionary.extend(entities.build_identifier_dictionary());
20 dictionary.extend(entities.build_localized_name_dictionary());
21 let name_search_index = StringSearchIndex::new(dictionary);
22 Self {
23 entities,
24 name_search_index,
25 }
26 }
27
28 fn entities(&self) -> &HashMap<ItemId, Arc<Item>> {
29 &self.entities
30 }
31}
32
33impl HasNameSearchIndex<ItemId, Item> for ItemsCollection {
34 fn name_search_index(&self) -> &StringSearchIndex<ItemId> {
35 &self.name_search_index
36 }
37}