tf2_enum/
wear.rs

1use crate::{
2    Attribute,
3    AttributeDef,
4    EffectType,
5    ItemAttribute,
6    TryFromIntAttributeValue,
7};
8use crate::error::TryFromPrimitiveError;
9use strum::{Display, EnumCount, EnumIter, EnumString};
10use num_enum::{IntoPrimitive, TryFromPrimitive};
11use serde_repr::{Deserialize_repr, Serialize_repr};
12
13/// Wear.
14#[derive(
15    Debug,
16    Clone,
17    Copy,
18    Eq,
19    PartialEq,
20    Ord,
21    PartialOrd,
22    Hash,
23    Display,
24    Serialize_repr,
25    Deserialize_repr,
26    EnumString,
27    EnumIter,
28    EnumCount,
29    TryFromPrimitive,
30    IntoPrimitive,
31)]
32#[repr(u32)]
33#[strum(serialize_all = "title_case")]
34#[allow(missing_docs)]
35pub enum Wear {
36    FactoryNew = 1,
37    MinimalWear = 2,
38    #[strum(serialize = "Field-Tested")]
39    FieldTested = 3,
40    #[strum(serialize = "Well-Worn")]
41    WellWorn = 4,
42    BattleScarred = 5,
43}
44
45impl Wear {
46    /// Converts the wear to a float value.
47    #[inline]
48    pub fn as_float(&self) -> f32 {
49        match self {
50            Self::FactoryNew => 0.2,
51            Self::MinimalWear => 0.4,
52            Self::FieldTested => 0.6,
53            Self::WellWorn => 0.8,
54            Self::BattleScarred => 1.0,
55        }
56    }
57}
58
59impl Attribute for Wear {
60    const DEFINDEX: u32 = 725;
61    const USES_FLOAT_VALUE: bool = true;
62    /// Represents the "set_item_texture_wear" attribute.
63    const ATTRIBUTE: AttributeDef = AttributeDef {
64        defindex: Self::DEFINDEX,
65        name: "set_item_texture_wear",
66        attribute_class: Some("set_item_texture_wear"),
67        description_string: None,
68        description_format: None,
69        effect_type: EffectType::Positive,
70        hidden: true,
71        stored_as_integer: false,
72    };
73    
74    /// Gets the attribute float value.
75    fn attribute_float_value(&self) -> Option<f32> {
76        // This could be done using arithmetic but this is a little more explicit.
77        Some(self.as_float())
78    }
79}
80
81impl TryFromIntAttributeValue for Wear {
82    fn try_from_attribute_float_value(v: f32) -> Option<Self> {
83        Self::try_from(v).ok()
84    }
85}
86
87impl TryFrom<f64> for Wear {
88    type Error = TryFromPrimitiveError<Self>;
89    
90    fn try_from(float_value: f64) -> Result<Wear, Self::Error> {
91        Wear::try_from((float_value * 5.0).round() as u32)
92    }
93}
94
95impl TryFrom<&f64> for Wear {
96    type Error = TryFromPrimitiveError<Self>;
97    
98    fn try_from(float_value: &f64) -> Result<Wear, Self::Error> {
99        Wear::try_from(*float_value)
100    }
101}
102
103impl TryFrom<f32> for Wear {
104    type Error = TryFromPrimitiveError<Self>;
105    
106    fn try_from(float_value: f32) -> Result<Wear, Self::Error> {
107        Wear::try_from((float_value * 5.0).round() as u32)
108    }
109}
110
111impl TryFrom<&f32> for Wear {
112    type Error = TryFromPrimitiveError<Self>;
113    
114    fn try_from(float_value: &f32) -> Result<Wear, Self::Error> {
115        Wear::try_from(*float_value)
116    }
117}
118
119impl From<Wear> for ItemAttribute {
120    fn from(val: Wear) -> Self {
121        ItemAttribute {
122            defindex: Wear::DEFINDEX,
123            value: val.attribute_value(),
124            float_value: val.attribute_float_value(),
125        }
126    }
127}