1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
use super::color::Color;
use matrix_sdk_base::ruma::events::room::ImageInfo;
use serde::{Deserialize, Serialize};
use std::str::FromStr;
use strum::{Display, EnumString};

#[derive(Clone, Debug, Display, Deserialize, Serialize, Default)]
#[serde(rename_all = "kebab-case")]
pub enum Position {
    TopLeft,
    TopMiddle,
    TopRight,
    CenterLeft,
    CenterMiddle,
    CenterRight,
    BottomLeft,
    #[default]
    BottomMiddle,
    BottomRight,
}

impl FromStr for Position {
    type Err = crate::Error;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        match s {
            "top-left" => Ok(Position::TopLeft),
            "top-middle" => Ok(Position::TopMiddle),
            "top-right" => Ok(Position::TopRight),
            "center-left" => Ok(Position::CenterLeft),
            "center-middle" => Ok(Position::CenterMiddle),
            "center-right" => Ok(Position::CenterRight),
            "bottom-left" => Ok(Position::BottomLeft),
            "bottom-middle" => Ok(Position::BottomMiddle),
            "bottom-right" => Ok(Position::BottomRight),
            _ => Err(crate::Error::FailedToParse {
                model_type: "Position".to_owned(),
                msg: format!("{s} is not a valid Position"),
            }),
        }
    }
}

/// Customize the color scheme
#[derive(Clone, Debug, Eq, PartialEq, Deserialize, Serialize, Default)]
pub struct Colorize {
    /// The foreground color to be used, as HEX
    color: Option<Color>,
    /// The background color to be used, as HEX
    background: Option<Color>,
}

impl Colorize {
    pub fn color(&self) -> Option<Color> {
        self.color
    }
    pub fn background(&self) -> Option<Color> {
        self.background
    }
}

#[derive(Debug, Clone, Default)]
pub struct ColorizeBuilder {
    colorize: Colorize,
}

impl ColorizeBuilder {
    pub fn color(&mut self, color: u32) {
        self.colorize.color = Some(color);
    }

    pub fn background(&mut self, color: u32) {
        self.colorize.background = Some(color);
    }

    pub fn unset_color(&mut self) {
        self.colorize.color = None;
    }

    pub fn unset_background(&mut self) {
        self.colorize.background = None;
    }

    pub fn build(self) -> Option<Colorize> {
        let ColorizeBuilder { colorize } = self;
        if colorize.color.is_some() || colorize.background.is_some() {
            return Some(colorize);
        }
        None
    }
}

#[derive(Clone, Debug, Eq, PartialEq, Deserialize, Serialize, EnumString, Display)]
#[serde(rename_all = "kebab-case")]
#[strum(serialize_all = "kebab-case")]
pub enum BrandLogo {
    Discord,
    Email,
    Facebook,
    Ferdiverse,
    Figma,
    Github,
    Gitlab,
    Googledrive,
    Googleplay,
    Instagram,
    Jitsi,
    Linkedin,
    Matrix,
    Mastodon,
    Medium,
    Meta,
    Notion,
    Reddit,
    Slack,
    Skype,
    Snapchat,
    #[serde(alias = "stackoverflow", alias = "stack-overflow")]
    StackOverflow,
    Telegram,
    Twitter,
    Whatsapp,
    Wechat,
    Youtube,
    X,
    Zoom,
    Custom(String),
    // FIXME: support for others?
}

#[derive(Clone, Debug, Eq, PartialEq, Deserialize, Serialize, EnumString, Display)]
#[serde(rename_all = "kebab-case")]
#[strum(serialize_all = "kebab-case")]
pub enum ActerIcon {
    // subset of https://phosphoricons.com
    Acorn,
    Alien,
    Ambulance,
    Anchor,
    Archive,
    Armchair,
    Axe,
    Backpack,
    Balloon,
    Binoculars,
    Bird,
    Fire,
    Flower,
    FlowerLotus,
    FlowerTulip,
    GameController,
    Ghost,
    Globe,
    Guitar,
    Heart,
    HeartBeat,
    Home,
    Island,
    Lego,
    LegoSmile,
    Megaphone,
    Newspaper,
    Rocket,
    RocketLaunch,
    Custom(String),
    // FIXME: support for others?
}

/// Customize the color scheme
#[derive(Clone, Debug, Deserialize, Serialize)]
#[serde(tag = "type", rename_all = "kebab-case")]
pub enum Icon {
    Emoji { key: String },
    BrandLogo { icon: BrandLogo },
    ActerIcon { icon: ActerIcon },
    Image(ImageInfo),
}

impl Icon {
    pub fn parse(typ: String, key: String) -> Icon {
        match typ.to_lowercase().as_str() {
            "emoji" => Icon::Emoji { key },
            "logo" | "brand" | "brand-logo" => Icon::BrandLogo {
                icon: BrandLogo::from_str(&key).unwrap_or(BrandLogo::Custom(key)),
            },
            _ => Icon::ActerIcon {
                icon: ActerIcon::from_str(&key).unwrap_or(ActerIcon::Custom(key)),
            },
        }
    }
    pub fn icon_type_str(&self) -> String {
        match self {
            Icon::Emoji { .. } => "emoji".to_owned(),
            Icon::BrandLogo { .. } => "brand-logo".to_owned(),
            Icon::ActerIcon { .. } => "acter-icon".to_owned(),
            Icon::Image(_) => "image".to_owned(),
        }
    }
    pub fn icon_str(&self) -> String {
        match self {
            Icon::Emoji { key } => key.clone(),
            Icon::BrandLogo {
                icon: BrandLogo::Custom(inner),
            }
            | Icon::ActerIcon {
                icon: ActerIcon::Custom(inner),
            } => inner.clone(),
            Icon::BrandLogo { icon } => icon.to_string(),
            Icon::ActerIcon { icon } => icon.to_string(),
            Icon::Image(_) => "image".to_owned(),
        }
    }
}

impl PartialEq for Icon {
    fn eq(&self, other: &Self) -> bool {
        match (&self, &other) {
            (Icon::Emoji { key: a }, Icon::Emoji { key: b }) => a == b,
            (Icon::BrandLogo { icon: a }, Icon::BrandLogo { icon: b }) => a == b,
            (Icon::ActerIcon { icon: a }, Icon::ActerIcon { icon: b }) => a == b,
            _ => false, // we can’t match images unfortunately
        }
    }
}
impl Eq for Icon {}

#[cfg(test)]
mod test {
    use super::*;

    #[test]
    pub fn test_emoji() {
        let emoji = Icon::Emoji {
            key: "A".to_owned(),
        };
        assert_eq!(emoji.icon_type_str(), "emoji".to_owned());
        assert_eq!(emoji.icon_str(), "A".to_owned());

        let emoji = Icon::Emoji {
            key: "πŸš€".to_owned(),
        };
        assert_eq!(emoji.icon_type_str(), "emoji".to_owned());
        assert_eq!(emoji.icon_str(), "πŸš€".to_owned());

        let emoji = Icon::parse("emoji".to_owned(), "asdf".to_owned());
        assert_eq!(emoji.icon_type_str(), "emoji".to_owned());
        assert_eq!(emoji.icon_str(), "asdf".to_owned());
    }

    #[test]
    pub fn test_custom_brand_gives_custom() {
        let icon = Icon::parse("brand".to_owned(), "acter".to_owned());
        assert_eq!(icon.icon_type_str(), "brand-logo".to_owned());
        assert_eq!(icon.icon_str(), "acter".to_owned());

        let icon = Icon::parse("logo".to_owned(), "email".to_owned());
        assert_eq!(icon.icon_type_str(), "brand-logo".to_owned());
        assert_eq!(icon.icon_str(), "email".to_owned());

        let icon = Icon::parse("brand-logo".to_owned(), "email".to_owned());
        assert_eq!(icon.icon_type_str(), "brand-logo".to_owned());
        assert_eq!(icon.icon_str(), "email".to_owned());

        // for sure a custom one
        let icon = Icon::BrandLogo {
            icon: BrandLogo::Custom("actOR".to_owned()),
        };
        assert_eq!(icon.icon_type_str(), "brand-logo".to_owned());
        assert_eq!(icon.icon_str(), "actOR".to_owned());
    }

    #[test]
    pub fn test_custom_acter_gives_custom() {
        let icon = Icon::parse("acter-icon".to_owned(), "acorn".to_owned());
        assert_eq!(icon.icon_type_str(), "acter-icon".to_owned());
        assert_eq!(icon.icon_str(), "acorn".to_owned());

        let icon = Icon::parse("acter".to_owned(), "bird".to_owned());
        assert_eq!(icon.icon_type_str(), "acter-icon".to_owned());
        assert_eq!(icon.icon_str(), "bird".to_owned());

        // for sure a custom one
        let icon = Icon::ActerIcon {
            icon: ActerIcon::Custom("lacasa".to_owned()),
        };
        assert_eq!(icon.icon_type_str(), "acter-icon".to_owned());
        assert_eq!(icon.icon_str(), "lacasa".to_owned());
    }
}