tf2_enum/
attribute_def.rs

1use crate::{DescriptionFormat, EffectType};
2
3/// Represents the definition of an attribute in the schema.
4#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
5pub struct AttributeDef {
6    /// The unique identifier for the attribute.
7    pub defindex: u32,
8    /// The name of the attribute.
9    pub name: &'static str,
10    /// The attribute class of the attribute.
11    pub attribute_class: Option<&'static str>,
12    /// The description string of the attribute.
13    pub description_string: Option<&'static str>,
14    /// The description format of the attribute.
15    pub description_format: Option<DescriptionFormat>,
16    /// The effect type of the description.
17    pub effect_type: EffectType,
18    /// Indicates whether the description is hidden from display.
19    pub hidden: bool,
20    /// Indicates whether the attribute's value is stored as an integer.
21    pub stored_as_integer: bool,
22}
23
24impl AttributeDef {
25    /// Returns the description of the attribute with the supplied value.
26    pub fn description<F>(&self, value: Option<F>) -> Option<String>
27    where
28        F: std::fmt::Display,
29    {
30        let description_string = self.description_string.as_ref()?;
31        
32        if let Some(value) = value {
33            return description_string
34                .replace("%s1", &value.to_string())
35                .into();
36        }
37            
38        Some(description_string.to_string())
39    }
40}
41
42#[cfg(test)]
43mod tests {
44    use crate::{Attribute, Sheen};
45    
46    #[test]
47    fn formats_description() {
48        let sheen = Sheen::TeamShine;
49        let formatted = Sheen::ATTRIBUTE.description(Some(&sheen));
50        
51        assert_eq!(formatted, Some("Sheen: Team Shine".into()));
52    }
53}