tf2_enum/
grade.rs

1use crate::Colored;
2use num_enum::{IntoPrimitive, TryFromPrimitive};
3use serde_repr::{Deserialize_repr, Serialize_repr};
4use strum::{Display, EnumCount, EnumIter, EnumString};
5
6/// Grade.
7#[derive(
8    Debug,
9    Clone,
10    Copy,
11    Eq,
12    PartialEq,
13    Ord,
14    PartialOrd,
15    Hash,
16    Display,
17    Serialize_repr,
18    Deserialize_repr,
19    EnumString,
20    EnumIter,
21    EnumCount,
22    TryFromPrimitive,
23    IntoPrimitive,
24)]
25#[repr(u32)]
26#[allow(missing_docs)]
27pub enum Grade {
28    Civilian = 1,
29    Freelance = 2,
30    Mercenary = 3,
31    Commando = 4,
32    Assassin = 5,
33    Elite = 6,
34}
35
36impl Colored for Grade {
37    /// Gets the related color of this grade as a hexadecimal color.
38    fn color(&self) -> u32 {
39        match self {
40            Self::Civilian => 0xB0C3D9,
41            Self::Freelance => 0x5E98D9,
42            Self::Mercenary => 0x4B69FF,
43            Self::Commando => 0x8847FF,
44            Self::Assassin => 0xD32CE6,
45            Self::Elite => 0xEB4B4B,
46        }
47    }
48    
49    /// Converts a hexadecimal color into a [`Grade`].
50    /// 
51    /// # Examples
52    /// ```
53    /// use tf2_enum::{Grade, Colored};
54    /// 
55    /// assert_eq!(Grade::from_color(0xEB4B4B).unwrap(), Grade::Elite);
56    /// ```
57    fn from_color(color: u32) -> Option<Self> {
58        match color {
59            0xB0C3D9 => Some(Self::Civilian),
60            0x5E98D9 => Some(Self::Freelance),
61            0x4B69FF => Some(Self::Mercenary),
62            0x8847FF => Some(Self::Commando),
63            0xD32CE6 => Some(Self::Assassin),
64            0xEB4B4B => Some(Self::Elite),
65            _ => None,
66        }
67    }
68}