1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
use crate::{
    assets::AssetExt,
    comp::inventory::item::{modular, ItemDef, ItemDefinitionId, ItemDesc, ItemKind},
};
use serde::{Deserialize, Serialize};
use std::sync::Arc;

#[derive(Clone, Debug, Serialize, Deserialize, Hash, Eq, PartialEq)]
pub enum ItemKey {
    Simple(String),
    ModularWeapon(modular::ModularWeaponKey),
    ModularWeaponComponent(modular::ModularWeaponComponentKey),
    TagExamples(Vec<ItemKey>, String),
    Empty,
}

impl<T: ItemDesc + ?Sized> From<&T> for ItemKey {
    fn from(item_desc: &T) -> Self {
        let item_definition_id = item_desc.item_definition_id();

        if let ItemKind::TagExamples { item_ids } = &*item_desc.kind() {
            ItemKey::TagExamples(
                item_ids
                    .iter()
                    .map(|id| ItemKey::from(&*Arc::<ItemDef>::load_expect_cloned(id)))
                    .collect(),
                item_definition_id
                    .itemdef_id()
                    .unwrap_or("?modular?")
                    .to_owned(),
            )
        } else {
            match item_definition_id {
                ItemDefinitionId::Simple(id) => ItemKey::Simple(String::from(id)),
                ItemDefinitionId::Compound { simple_base, .. } => {
                    if let Ok(key) =
                        modular::weapon_component_to_key(simple_base, item_desc.components())
                    {
                        ItemKey::ModularWeaponComponent(key)
                    } else {
                        ItemKey::Simple(simple_base.to_owned())
                    }
                },
                ItemDefinitionId::Modular { .. } => {
                    ItemKey::ModularWeapon(modular::weapon_to_key(item_desc))
                },
            }
        }
    }
}