1use crate::Colored;
2use num_enum::{IntoPrimitive, TryFromPrimitive};
3use serde_repr::{Deserialize_repr, Serialize_repr};
4use strum::{Display, EnumCount, EnumIter, EnumString};
5
6#[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 Quality {
28 Normal = 0,
29 Genuine = 1,
30 #[strum(serialize = "rarity2")]
31 Rarity2 = 2,
32 Vintage = 3,
33 #[strum(serialize = "rarity3")]
34 Rarity3 = 4,
35 Unusual = 5,
36 Unique = 6,
37 Community = 7,
38 Valve = 8,
39 #[strum(serialize = "Self-Made")]
40 SelfMade = 9,
41 Customized = 10,
42 Strange = 11,
43 Completed = 12,
44 Haunted = 13,
45 #[strum(serialize = "Collector's")]
46 Collectors = 14,
47 #[strum(serialize = "Decorated Weapon")]
48 DecoratedWeapon = 15,
49}
50
51impl Colored for Quality {
52 fn color(&self) -> u32 {
54 match self {
55 Self::Normal => 0xB2B2B2,
56 Self::Genuine => 0x4D7455,
57 Self::Rarity2 => 0xFFFFFF,
58 Self::Vintage => 0x476291,
59 Self::Rarity3 => 0xFFFFFF,
60 Self::Unusual => 0x8650AC,
61 Self::Unique => 0xFFD700,
62 Self::Community => 0x70B04A,
63 Self::Valve => 0x56083F,
64 Self::SelfMade => 0x70B04A,
65 Self::Customized => 0xFFFFFF,
66 Self::Strange => 0xCF6A32,
67 Self::Completed => 0xFFFFFF,
68 Self::Haunted => 0x38F3AB,
69 Self::Collectors => 0xAA0000,
70 Self::DecoratedWeapon => 0xFAFAFA,
71 }
72 }
73
74 fn from_color(color: u32) -> Option<Self> {
83 match color {
84 0xB2B2B2 => Some(Self::Normal),
85 0x4D7455 => Some(Self::Genuine),
86 0x476291 => Some(Self::Vintage),
87 0x8650AC => Some(Self::Unusual),
88 0xFFD700 => Some(Self::Unique),
89 0x56083F => Some(Self::Valve),
90 0x70B04A => Some(Self::SelfMade),
91 0xCF6A32 => Some(Self::Strange),
92 0x38F3AB => Some(Self::Haunted),
93 0xAA0000 => Some(Self::Collectors),
94 0xFAFAFA => Some(Self::DecoratedWeapon),
95 _ => None,
96 }
97 }
98}
99
100#[cfg(test)]
101mod tests {
102 use super::*;
103 use std::fmt::Display;
104 use std::str::FromStr;
105
106 fn color_span<C: Colored + Display>(c: C) -> String {
107 format!("<span style=\"color: {};\">{c}</span>", c.color_string())
108 }
109
110 #[test]
111 fn get_html() {
112 let quality = Quality::Strange;
113
114 assert_eq!(color_span(quality), "<span style=\"color: #CF6A32;\">Strange</span>");
115 }
116
117 #[test]
118 fn from_color() {
119 assert_eq!(Some(Quality::Strange), Quality::from_color(0xCF6A32));
120 }
121
122 #[test]
123 fn to_color() {
124 assert_eq!(Quality::Strange.color(), 0xCF6A32);
125 }
126
127 #[test]
128 fn converts_to_primitive() {
129 assert_eq!(11_u32, Quality::Strange as u32);
130 }
131
132 #[test]
133 fn converts_string_to_quality() {
134 assert_eq!(Quality::DecoratedWeapon, Quality::from_str("Decorated Weapon").unwrap());
135 }
136
137 #[test]
138 fn displays_as_string() {
139 assert_eq!("Collector's", &format!("{}", Quality::Collectors));
140 }
141}