some_random_api/structs/
rank_card.rs1use crate::Hex;
2use anyhow::Result;
3use serde::Serialize;
4use serde_repr::Serialize_repr;
5
6#[derive(Debug, Serialize)]
7pub struct RankCard {
8    #[serde(skip_serializing)]
9    pub template: RankCardTemplate,
10
11    pub username: String,
12    pub discriminator: String,
13
14    #[serde(rename = "avatar")]
15    pub avatar_url: String,
16
17    pub level: u64,
18
19    #[serde(rename = "cxp")]
20    pub current_xp: u64,
21
22    #[serde(rename = "nxp")]
23    pub needed_xp: u64,
24
25    #[serde(rename = "bg")]
26    pub background_url: String,
27
28    #[serde(rename = "cbg")]
29    pub background_color: String,
30
31    #[serde(rename = "ctext")]
32    pub text_color: String,
33
34    #[serde(rename = "ccxp")]
35    pub current_xp_color: String,
36
37    #[serde(rename = "cbar")]
38    pub xp_bar_color: String,
39}
40
41#[derive(Debug, Serialize_repr)]
42#[repr(u8)]
43pub enum RankCardTemplate {
44    A = 1,
45    B,
46    C,
47    D,
48    E,
49    F,
50    G,
51    H,
52}
53
54impl RankCard {
55    pub fn new<T: ToString, U: ToString, V: ToString>(
73        username: T,
74        discriminator: U,
75        avatar_url: V,
76        level: u64,
77        (current_xp, needed_xp): (u64, u64),
78    ) -> Self {
79        Self {
80            template: RankCardTemplate::A,
81            username: username.to_string(),
82            discriminator: discriminator.to_string(),
83            avatar_url: avatar_url.to_string(),
84            level,
85            current_xp,
86            needed_xp,
87            background_url: "".into(),
88            background_color: "".into(),
89            text_color: "".into(),
90            current_xp_color: "".into(),
91            xp_bar_color: "".into(),
92        }
93    }
94
95    pub fn set_template(mut self, template: RankCardTemplate) -> Self {
97        self.template = template;
98        self
99    }
100
101    pub fn set_background_url<T: ToString>(mut self, background_url: T) -> Self {
103        self.background_url = background_url.to_string();
104        self
105    }
106
107    pub fn set_background_color<T: TryInto<Hex>>(mut self, hex: T) -> Result<Self, T::Error> {
109        self.background_color = hex.try_into()?.hex;
110        Ok(self)
111    }
112
113    pub fn set_text_color<T: TryInto<Hex>>(mut self, hex: T) -> Result<Self, T::Error> {
115        self.text_color = hex.try_into()?.hex;
116        Ok(self)
117    }
118
119    pub fn set_current_xp_color<T: TryInto<Hex>>(mut self, hex: T) -> Result<Self, T::Error> {
121        self.current_xp_color = hex.try_into()?.hex;
122        Ok(self)
123    }
124
125    pub fn set_xp_bar_color<T: TryInto<Hex>>(mut self, hex: T) -> Result<Self, T::Error> {
127        self.xp_bar_color = hex.try_into()?.hex;
128        Ok(self)
129    }
130}