git_iris/
gitmoji.rs

1use std::collections::HashMap;
2use std::sync::LazyLock;
3
4fn create_gitmoji_map() -> HashMap<&'static str, (&'static str, &'static str)> {
5    let mut m = HashMap::new();
6
7    m.insert("feat", ("✨", "Introduce new features"));
8    m.insert("fix", ("🐛", "Fix a bug"));
9    m.insert("docs", ("📝", "Add or update documentation"));
10    m.insert("style", ("💄", "Add or update the UI and style files"));
11    m.insert("refactor", ("♻️", "Refactor code"));
12    m.insert("perf", ("⚡️", "Improve performance"));
13    m.insert("test", ("✅", "Add or update tests"));
14    m.insert("build", ("👷", "Add or update build scripts"));
15    m.insert("ci", ("🔧", "Add or update CI configuration"));
16    m.insert(
17        "chore",
18        ("🔨", "Other changes that don't modify src or test files"),
19    );
20    m.insert("revert", ("⏪️", "Revert changes"));
21    m.insert("wip", ("🚧", "Work in progress"));
22    m.insert("dependencies", ("⬆️", "Update dependencies"));
23    m.insert("remove", ("🔥", "Remove code or files"));
24    m.insert("i18n", ("🌐", "Internationalization and localization"));
25    m.insert("security", ("🔒️", "Fix security issues"));
26    m.insert("debug", ("🐛", "Add or update debugging code"));
27    m.insert("deployment", ("🚀", "Deploy stuff"));
28    m.insert("hotfix", ("🚑", "Critical hotfix"));
29    m.insert("accessibility", ("♿", "Improve accessibility"));
30    m.insert("analytics", ("📈", "Add or update analytics"));
31    m.insert("seo", ("🔍️", "Improve SEO"));
32    m.insert("config", ("🔧", "Add or update configuration files"));
33    m.insert("tracking", ("📈", "Add or update tracking code"));
34    m.insert("design", ("🎨", "Improve structure / format of the code"));
35    m.insert("error", ("🚨", "Fix compiler / linter warnings"));
36    m.insert("test_failure", ("💥", "Fix tests or CI failures"));
37    m.insert("data", ("📊", "Add or update data"));
38    m.insert("content", ("📝", "Add or update content"));
39    m.insert("linter", ("👕", "Add or update linters"));
40    m.insert("initial", ("🎉", "Begin a project"));
41
42    m
43}
44
45static GITMOJI_MAP: LazyLock<HashMap<&'static str, (&'static str, &'static str)>> =
46    LazyLock::new(create_gitmoji_map);
47
48pub fn get_gitmoji(commit_type: &str) -> Option<&'static str> {
49    GITMOJI_MAP.get(commit_type).map(|&(emoji, _)| emoji)
50}
51
52pub fn apply_gitmoji(commit_message: &str) -> String {
53    let parts: Vec<&str> = commit_message.splitn(2, ':').collect();
54    if parts.len() == 2
55        && let Some((gitmoji, _)) = GITMOJI_MAP.get(parts[0].trim())
56    {
57        return format!("{} {}: {}", gitmoji, parts[0].trim(), parts[1].trim());
58    }
59    commit_message.to_string()
60}
61
62pub fn get_gitmoji_list() -> String {
63    let mut entries: Vec<_> = GITMOJI_MAP.iter().collect();
64    entries.sort_by_key(|(key, _)| *key);
65
66    let emoji_list = entries
67        .iter()
68        .map(|(key, (emoji, description))| format!("{emoji} - :{key}: - {description}"))
69        .collect::<Vec<String>>();
70
71    emoji_list.join("\n")
72}
73
74/// Post-processes a commit message, applying gitmoji if enabled
75pub fn process_commit_message(message: String, use_gitmoji: bool) -> String {
76    if use_gitmoji {
77        apply_gitmoji(&message)
78    } else {
79        message
80    }
81}