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
290
291
292
293
294
295
296
use std::fmt::{self, Display};

use serde::{Deserialize, Serialize};
use time::OffsetDateTime;
use url::Url;

/// The default URL used for update
pub const DEFAULT_URL: &str = "https://gitmoji.dev/api/gitmojis";

#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
/// The emoji format
pub enum EmojiFormat {
    /// Use the code mode, like ':smile:'
    UseCode,
    /// Use the emoji mode, like '😄'
    UseEmoji,
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(default)]
/// The Gitmojis configuration
pub struct GitmojiConfig {
    auto_add: bool,
    format: EmojiFormat,
    signed: bool,
    scope: bool,
    update_url: Url,
    #[serde(with = "time::serde::iso8601::option")]
    last_update: Option<OffsetDateTime>,
    gitmojis: Vec<Gitmoji>,
}

impl GitmojiConfig {
    /// Create a new `GitmojiConfig`
    #[must_use]
    pub const fn new(
        auto_add: bool,
        format: EmojiFormat,
        signed: bool,
        scope: bool,
        update_url: Url,
    ) -> Self {
        Self {
            auto_add,
            format,
            signed,
            scope,
            update_url,
            last_update: None,
            gitmojis: vec![],
        }
    }

    /// Merge with a local configuration
    pub fn merge(&mut self, local_config: &LocalGitmojiConfig) {
        if let Some(auto_add) = local_config.auto_add() {
            self.auto_add = auto_add;
        }
        if let Some(format) = local_config.format() {
            self.format = format;
        }
        if let Some(signed) = local_config.signed() {
            self.signed = signed;
        }
        if let Some(gitmojis) = local_config.gitmojis() {
            self.gitmojis = gitmojis.to_vec();
        }
    }

    /// If the "--all" is added to commit command
    #[must_use]
    pub const fn auto_add(&self) -> bool {
        self.auto_add
    }

    /// The format of gitmoji (code or emoji)
    #[must_use]
    pub const fn format(&self) -> &EmojiFormat {
        &self.format
    }

    /// If the signed commits is enabled
    #[must_use]
    pub const fn signed(&self) -> bool {
        self.signed
    }

    /// If we add a scope
    #[must_use]
    pub const fn scope(&self) -> bool {
        self.scope
    }

    /// The URL used for update
    #[must_use]
    pub fn update_url(&self) -> &str {
        self.update_url.as_ref()
    }

    /// Set the URL used for update
    pub fn set_update_url(&mut self, update_url: Url) {
        self.update_url = update_url;
    }

    /// The last time the gitmoji list was updated
    #[must_use]
    pub const fn last_update(&self) -> Option<OffsetDateTime> {
        self.last_update
    }

    /// The gitmoji list
    #[must_use]
    pub fn gitmojis(&self) -> &[Gitmoji] {
        self.gitmojis.as_ref()
    }

    /// Set the gitmojis list
    pub fn set_gitmojis(&mut self, gitmojis: Vec<Gitmoji>) {
        self.last_update = Some(OffsetDateTime::now_utc());
        self.gitmojis = gitmojis;
    }
}

impl Default for GitmojiConfig {
    fn default() -> Self {
        Self {
            auto_add: false,
            format: EmojiFormat::UseCode,
            signed: false,
            scope: false,
            update_url: DEFAULT_URL.parse().expect("It's a valid URL"),
            last_update: None,
            gitmojis: vec![],
        }
    }
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, Default)]
/// The local gitmoji configuration
pub struct LocalGitmojiConfig {
    auto_add: Option<bool>,
    format: Option<EmojiFormat>,
    signed: Option<bool>,
    scope: Option<bool>,
    gitmojis: Option<Vec<Gitmoji>>,
}

impl LocalGitmojiConfig {
    /// If the "--all" is added to commit command
    #[must_use]
    pub fn auto_add(&self) -> Option<bool> {
        self.auto_add
    }

    /// The format of gitmoji (code or emoji)
    #[must_use]
    pub fn format(&self) -> Option<EmojiFormat> {
        self.format
    }

    /// If the signed commits is enabled
    #[must_use]
    pub fn signed(&self) -> Option<bool> {
        self.signed
    }

    /// If we add a scope
    #[must_use]
    pub fn scope(&self) -> Option<bool> {
        self.scope
    }

    /// The gitmoji list
    #[must_use]
    pub fn gitmojis(&self) -> Option<&[Gitmoji]> {
        self.gitmojis.as_deref()
    }
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
/// A Gitmoji
pub struct Gitmoji {
    emoji: String,
    code: String,
    name: Option<String>,
    description: Option<String>,
}

impl Gitmoji {
    /// Create a gitmoji
    #[must_use]
    pub fn new(
        emoji: String,
        code: String,
        name: Option<String>,
        description: Option<String>,
    ) -> Self {
        Self {
            emoji,
            code,
            name,
            description,
        }
    }

    /// The emoji
    #[must_use]
    pub fn emoji(&self) -> &str {
        self.emoji.as_ref()
    }

    /// The associated code
    #[must_use]
    pub fn code(&self) -> &str {
        self.code.as_ref()
    }

    /// The name
    #[must_use]
    pub fn name(&self) -> Option<&str> {
        self.name.as_deref()
    }

    /// The description
    #[must_use]
    pub fn description(&self) -> Option<&str> {
        self.description.as_deref()
    }
}

impl Display for Gitmoji {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let Gitmoji {
            emoji,
            code,
            name,
            description,
            ..
        } = self;
        write!(
            f,
            "{emoji} {code} {} - {}",
            name.as_deref().unwrap_or_default(),
            description.as_deref().unwrap_or_default()
        )
    }
}

#[cfg(test)]
#[allow(clippy::ignored_unit_patterns)]
mod tests {
    use assert2::*;

    use super::*;

    #[test]
    fn should_serde_gitmoji() {
        let gitmoji = Gitmoji {
            emoji: String::from("🚀"),
            code: String::from("rocket"),
            name: Some(String::from("Initialize")),
            description: Some(String::from("Bla bla")),
        };

        // Serialize
        let toml = toml_edit::ser::to_string(&gitmoji);
        let_assert!(Ok(toml) = toml);

        // Deserialize
        let result = toml_edit::de::from_str::<Gitmoji>(&toml);
        let_assert!(Ok(result) = result);

        check!(result == gitmoji);
    }

    #[test]
    fn should_serde_config() {
        let mut config = GitmojiConfig::default();
        config.gitmojis.push(Gitmoji {
            emoji: String::from("🚀"),
            code: String::from("rocket"),
            name: Some(String::from("Initialize")),
            description: Some(String::from("Bla bla")),
        });

        // Serialize
        let toml = toml_edit::ser::to_string(&config);
        let_assert!(Ok(toml) = toml);

        // Deserialize
        let result = toml_edit::de::from_str::<GitmojiConfig>(&toml);
        let_assert!(Ok(result) = result);

        check!(result == config);
    }
}