tf2_enum/
item_slot.rs

1use crate::StockWeapon;
2use serde::{Deserialize, Serialize};
3use strum::{Display, EnumCount, EnumIter, EnumString};
4
5/// Item slot.
6#[derive(
7    Debug,
8    Clone,
9    Copy,
10    Eq,
11    PartialEq,
12    Ord,
13    PartialOrd,
14    Hash,
15    Display,
16    Deserialize,
17    Serialize,
18    EnumString,
19    EnumIter,
20    EnumCount,
21)]
22#[strum(serialize_all = "snake_case")]
23#[serde(rename_all = "snake_case")]
24#[allow(missing_docs)]
25pub enum ItemSlot {
26    Melee,
27    Primary,
28    Secondary,
29    #[strum(serialize = "pda")]
30    #[serde(rename = "pda")]
31    PDA,
32    #[strum(serialize = "pda2")]
33    #[serde(rename = "pda2")]
34    PDA2,
35    Building,
36    Misc,
37    Taunt,
38    Action,
39    Utility,
40    Quest,
41}
42
43impl ItemSlot {
44    /// Gets the stock weapons available for this item slot.
45    pub fn stock_weapons(&self) -> &'static [StockWeapon] {
46        match self {
47            ItemSlot::Primary => &[
48                StockWeapon::Scattergun,
49                StockWeapon::FlameThrower,
50                StockWeapon::SyringeGun,
51                StockWeapon::RocketLauncher,
52                StockWeapon::GrenadeLauncher,
53                StockWeapon::SniperRifle,
54                StockWeapon::Minigun,
55                StockWeapon::Revolver,
56                // The Shotgun is both a primary and secondary weapon.
57                StockWeapon::Shotgun,
58            ],
59            ItemSlot::Secondary => &[
60                StockWeapon::SMG,
61                StockWeapon::StickybombLauncher,
62                StockWeapon::Pistol,
63                StockWeapon::MediGun,
64                StockWeapon::Shotgun,
65            ],
66            ItemSlot::Melee => &[
67                StockWeapon::Bat,
68                StockWeapon::Bottle,
69                StockWeapon::FireAxe,
70                StockWeapon::Kukri,
71                StockWeapon::Knife,
72                StockWeapon::Fists,
73                StockWeapon::Shovel,
74                StockWeapon::Wrench,
75                StockWeapon::Bonesaw,
76                StockWeapon::Bottle,
77            ],
78            ItemSlot::PDA => &[
79                StockWeapon::DisguiseKit,
80                StockWeapon::ConstructionPDA,
81            ],
82            ItemSlot::PDA2 => &[
83                StockWeapon::InvisWatch,
84                StockWeapon::DestructionPDA,
85            ],
86            ItemSlot::Building => &[
87                StockWeapon::PDA,
88            ],
89            // No stock weapons for these slots
90            _ => &[],
91        }
92    }
93}
94
95#[cfg(test)]
96mod tests {
97    use super::*;
98
99    #[test]
100    fn test_item_slot_stock_weapons() {
101        let primary_weapons = ItemSlot::Primary.stock_weapons();
102        assert!(primary_weapons.contains(&StockWeapon::Scattergun));
103        assert!(primary_weapons.contains(&StockWeapon::RocketLauncher));
104    }
105    
106    #[test]
107    fn serializes() {
108        let json = serde_json::to_string(&ItemSlot::Primary).unwrap();
109        assert_eq!(json, "\"primary\"");
110        
111        
112        let json = serde_json::to_string(&ItemSlot::PDA).unwrap();
113        assert_eq!(json, "\"pda\"");
114        let json = serde_json::to_string(&ItemSlot::PDA2).unwrap();
115        assert_eq!(json, "\"pda2\"");
116    }
117}