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
use std::fmt::{self, Display};
use serde::{Deserialize, Serialize};
use time::OffsetDateTime;
use url::Url;
pub const DEFAULT_URL: &str = "https://gitmoji.dev/api/gitmojis";
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum EmojiFormat {
UseCode,
UseEmoji,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(default)]
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 {
#[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![],
}
}
#[must_use]
pub const fn auto_add(&self) -> bool {
self.auto_add
}
#[must_use]
pub const fn format(&self) -> &EmojiFormat {
&self.format
}
#[must_use]
pub const fn signed(&self) -> bool {
self.signed
}
#[must_use]
pub const fn scope(&self) -> bool {
self.scope
}
#[must_use]
pub fn update_url(&self) -> &str {
self.update_url.as_ref()
}
#[must_use]
pub const fn last_update(&self) -> Option<OffsetDateTime> {
self.last_update
}
#[must_use]
pub fn gitmojis(&self) -> &[Gitmoji] {
self.gitmojis.as_ref()
}
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)]
pub struct Gitmoji {
emoji: String,
code: String,
name: Option<String>,
description: Option<String>,
}
impl Gitmoji {
#[must_use]
pub fn new(
emoji: String,
code: String,
name: Option<String>,
description: Option<String>,
) -> Self {
Self {
emoji,
code,
name,
description,
}
}
#[must_use]
pub fn emoji(&self) -> &str {
self.emoji.as_ref()
}
#[must_use]
pub fn code(&self) -> &str {
self.code.as_ref()
}
#[must_use]
pub fn name(&self) -> Option<&str> {
self.name.as_deref()
}
#[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)]
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")),
};
let toml = toml::to_string(&gitmoji);
let_assert!(Ok(toml) = toml);
let result = toml::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")),
});
let toml = toml::to_string(&config);
let_assert!(Ok(toml) = toml);
let result = toml::from_str::<GitmojiConfig>(&toml);
let_assert!(Ok(result) = result);
check!(result == config);
}
}