reddit_rs/models/
flair.rs1use serde::{Deserialize, Serialize};
2
3use crate::{
4 models::{
5 link::{LinkFlair, LinkFlairTemplateId},
6 author::{AuthorFlair, AuthorFlairTemplateId},
7 color::Color,
8 flair::template_id::TemplateId,
9 private::Sealed,
10 richtext::Richtext,
11 },
12};
13
14pub mod template_id;
15
16pub trait Flair: Sealed {
17 type TemplateId: TemplateId;
18
19 fn background_color(&self) -> Option<Color>;
20
21 fn template_id(&self) -> Option<Self::TemplateId>;
22
23 fn css_class(&self) -> Option<&str>;
24
25 fn richtext(&self) -> Option<&[Richtext]>;
26
27 fn ty(&self) -> Option<FlairType>;
28
29 fn text(&self) -> Option<&str>;
30}
31
32#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq)]
33#[serde(rename_all = "snake_case")]
34pub enum FlairType {
35 Text,
36 Richtext,
37}
38
39#[derive(
40 Debug, Clone, Copy, Serialize, Deserialize, PartialEq, strum::AsRefStr, strum::EnumString,
41)]
42#[serde(rename_all = "snake_case")]
43#[strum(serialize_all = "snake_case")]
44pub enum FlairTextColor {
45 Light,
46 Dark,
47}
48
49impl Sealed for AuthorFlair {}
50
51impl Flair for AuthorFlair {
52 type TemplateId = AuthorFlairTemplateId;
53
54 fn background_color(&self) -> Option<Color> {
55 self.background_color
56 }
57
58 fn template_id(&self) -> Option<Self::TemplateId> {
59 self.template_id
60 }
61
62 fn css_class(&self) -> Option<&str> {
63 self.css_class.as_deref()
64 }
65
66 fn richtext(&self) -> Option<&[Richtext]> {
67 self.richtext.as_deref()
68 }
69
70 fn ty(&self) -> Option<FlairType> {
71 self.ty
72 }
73
74 fn text(&self) -> Option<&str> {
75 self.text.as_deref()
76 }
77}
78
79impl Sealed for LinkFlair {}
80
81impl Flair for LinkFlair {
82 type TemplateId = LinkFlairTemplateId;
83
84 fn background_color(&self) -> Option<Color> {
85 self.background_color
86 }
87
88 fn template_id(&self) -> Option<Self::TemplateId> {
89 self.template_id
90 }
91
92 fn css_class(&self) -> Option<&str> {
93 self.css_class.as_deref()
94 }
95
96 fn richtext(&self) -> Option<&[Richtext]> {
97 Some(&self.richtext)
98 }
99
100 fn ty(&self) -> Option<FlairType> {
101 Some(self.ty)
102 }
103
104 fn text(&self) -> Option<&str> {
105 self.text.as_deref()
106 }
107}