veloren_common/comp/inventory/item/
item_key.rs

1use crate::{
2    assets::AssetExt,
3    comp::inventory::item::{ItemDef, ItemDefinitionId, ItemDesc, ItemKind, modular},
4};
5use serde::{Deserialize, Serialize};
6use std::sync::Arc;
7
8/// ItemKey should only be used for front-end identification purposes
9#[derive(Clone, Debug, Serialize, Deserialize, Hash, Eq, PartialEq)]
10pub enum ItemKey {
11    Simple(String),
12    ModularWeapon(modular::ModularWeaponKey),
13    ModularWeaponComponent(modular::ModularWeaponComponentKey),
14    TagExamples(Vec<ItemKey>, String),
15    Empty,
16}
17
18impl<T: ItemDesc + ?Sized> From<&T> for ItemKey {
19    fn from(item_desc: &T) -> Self {
20        let item_definition_id = item_desc.item_definition_id();
21
22        if let ItemKind::TagExamples { item_ids } = &*item_desc.kind() {
23            ItemKey::TagExamples(
24                item_ids
25                    .iter()
26                    .map(|id| ItemKey::from(&*Arc::<ItemDef>::load_expect_cloned(id)))
27                    .collect(),
28                item_definition_id
29                    .itemdef_id()
30                    .unwrap_or("?modular?")
31                    .to_owned(),
32            )
33        } else {
34            match item_definition_id {
35                ItemDefinitionId::Simple(id) => ItemKey::Simple(String::from(id)),
36                ItemDefinitionId::Compound { simple_base, .. } => {
37                    if let Ok(key) =
38                        modular::weapon_component_to_key(simple_base, item_desc.components())
39                    {
40                        ItemKey::ModularWeaponComponent(key)
41                    } else {
42                        ItemKey::Simple(simple_base.to_owned())
43                    }
44                },
45                ItemDefinitionId::Modular { .. } => {
46                    ItemKey::ModularWeapon(modular::weapon_to_key(item_desc))
47                },
48            }
49        }
50    }
51}