tf2_enum/
quality.rs

1use strum_macros::{Display, EnumString, EnumIter, EnumCount};
2use num_enum::{TryFromPrimitive, IntoPrimitive};
3use serde_repr::{Serialize_repr, Deserialize_repr};
4
5/// Quality.
6#[derive(
7    Serialize_repr,
8    Deserialize_repr,
9    Debug,
10    Hash,
11    Eq,
12    PartialEq,
13    Ord,
14    PartialOrd,
15    Display,
16    EnumString,
17    EnumIter,
18    EnumCount,
19    TryFromPrimitive,
20    IntoPrimitive,
21    Clone,
22    Copy,
23)]
24#[repr(u32)]
25pub enum Quality {
26    Normal = 0,
27    Genuine = 1,
28    #[strum(serialize = "rarity2")]
29    Rarity2 = 2,
30    Vintage = 3,
31    #[strum(serialize = "rarity3")]
32    Rarity3 = 4,
33    Unusual = 5,
34    Unique = 6,
35    Community = 7,
36    Valve = 8,
37    #[strum(serialize = "Self-Made")]
38    SelfMade = 9,
39    Customized = 10,
40    Strange = 11,
41    Completed = 12,
42    Haunted = 13,
43    #[strum(serialize = "Collector's")]
44    Collectors = 14,
45    #[strum(serialize = "Decorated Weapon")]
46    DecoratedWeapon = 15,
47}
48
49impl Quality {
50    /// Gets the related color of this quality as a hexadecimal color.
51    pub fn color(&self) -> u32 {
52        match self {
53            Self::Normal => 0xB2B2B2,
54            Self::Genuine => 0x4D7455,
55            Self::Rarity2 => 0xFFFFFF,
56            Self::Vintage => 0x476291,
57            Self::Rarity3 => 0xFFFFFF,
58            Self::Unusual => 0x8650AC,
59            Self::Unique => 0xFFD700,
60            Self::Community => 0x70B04A,
61            Self::Valve => 0x56083F,
62            Self::SelfMade => 0x70B04A,
63            Self::Customized => 0xFFFFFF,
64            Self::Strange => 0xCF6A32,
65            Self::Completed => 0xFFFFFF,
66            Self::Haunted => 0x38F3AB,
67            Self::Collectors => 0xAA0000,
68            Self::DecoratedWeapon => 0xFAFAFA,
69        }
70    }
71    
72    /// Converts a hexadecimal color into a [`Quality`].
73    /// 
74    /// # Examples
75    /// ```
76    /// use tf2_enum::Quality;
77    /// 
78    /// assert_eq!(Quality::from_color(0x8650AC).unwrap(), Quality::Unusual);
79    /// ```
80    pub fn from_color(color: u32) -> Option<Self> {
81        match color {
82            0xB2B2B2 => Some(Self::Normal),
83            0x4D7455 => Some(Self::Genuine),
84            0x476291 => Some(Self::Vintage),
85            0x8650AC => Some(Self::Unusual),
86            0xFFD700 => Some(Self::Unique),
87            0x56083F => Some(Self::Valve),
88            0x70B04A => Some(Self::SelfMade),
89            0xCF6A32 => Some(Self::Strange),
90            0x38F3AB => Some(Self::Haunted),
91            0xAA0000 => Some(Self::Collectors),
92            0xFAFAFA => Some(Self::DecoratedWeapon),
93            _ => None,
94        }
95    }
96    
97    /// Converts a hexadecimal color string into a [`Quality`].
98    /// 
99    /// # Examples
100    /// ```
101    /// use tf2_enum::Quality;
102    /// 
103    /// assert_eq!(Quality::from_color_str("#8650AC").unwrap(), Quality::Unusual);
104    /// ```
105    pub fn from_color_str(color: &str) -> Option<Self> {
106        let len = color.len();
107        let mut color = color;
108        
109        if len == 7 && color.starts_with('#') {
110            color = &color[1..len];
111        } else if len != 6 {
112            return None;
113        }
114        
115        let color = u32::from_str_radix(color, 16).ok()?;
116        
117        Self::from_color(color)
118    }
119}
120
121#[cfg(test)]
122mod tests {
123    use super::*;
124    use std::str::FromStr;
125    
126    #[test]
127    fn from_color() {
128        assert_eq!(Some(Quality::Strange), Quality::from_color(0xCF6A32));
129    }
130    
131    #[test]
132    fn to_color() {
133        assert_eq!(Quality::Strange.color(), 0xCF6A32);
134    }
135
136    #[test]
137    fn converts_to_primitive() {
138        assert_eq!(11_u32, Quality::Strange as u32);
139    }
140    
141    #[test]
142    fn converts_string_to_quality() {
143        assert_eq!(Quality::DecoratedWeapon, Quality::from_str("Decorated Weapon").unwrap());
144    }
145    
146    #[test]
147    fn displays_as_string() {
148        assert_eq!("Collector's", &format!("{}", Quality::Collectors));
149    }
150}