1use std::collections::HashMap;
2
3#[derive(Debug, Clone)]
4pub struct OfficialPlugin {
5 pub name: String,
6 pub description: String,
7 pub install_message: String,
8}
9
10impl OfficialPlugin {
11 pub fn new(name: &str, description: &str, install_message: &str) -> Self {
12 Self {
13 name: name.to_string(),
14 description: description.to_string(),
15 install_message: install_message.to_string(),
16 }
17 }
18}
19
20pub struct OfficialPluginRegistry {
21 plugins: HashMap<String, OfficialPlugin>,
22}
23
24impl OfficialPluginRegistry {
25 pub fn new() -> Self {
26 let mut registry = Self {
27 plugins: HashMap::new(),
28 };
29
30 registry.register_official_plugins();
32
33 registry
34 }
35
36 fn register_official_plugins(&mut self) {
37 self.plugins.insert(
39 "update".to_string(),
40 OfficialPlugin::new(
41 "update",
42 "Update atuin to the latest version",
43 "The 'atuin update' command is provided by the atuin-update plugin.\n\
44 It is only installed if you used the install script\n \
45 If you used a package manager (brew, apt, etc), please continue to use it for updates"
46 ),
47 );
48 }
49
50 pub fn get_plugin(&self, name: &str) -> Option<&OfficialPlugin> {
51 self.plugins.get(name)
52 }
53
54 pub fn is_official_plugin(&self, name: &str) -> bool {
55 self.plugins.contains_key(name)
56 }
57
58 pub fn get_install_message(&self, name: &str) -> Option<&str> {
59 self.plugins
60 .get(name)
61 .map(|plugin| plugin.install_message.as_str())
62 }
63}
64
65impl Default for OfficialPluginRegistry {
66 fn default() -> Self {
67 Self::new()
68 }
69}
70
71#[cfg(test)]
72mod tests {
73 use super::*;
74
75 #[test]
76 fn test_registry_creation() {
77 let registry = OfficialPluginRegistry::new();
78 assert!(registry.is_official_plugin("update"));
79 assert!(!registry.is_official_plugin("nonexistent"));
80 }
81
82 #[test]
83 fn test_get_plugin() {
84 let registry = OfficialPluginRegistry::new();
85 let plugin = registry.get_plugin("update");
86 assert!(plugin.is_some());
87 assert_eq!(plugin.unwrap().name, "update");
88 }
89
90 #[test]
91 fn test_get_install_message() {
92 let registry = OfficialPluginRegistry::new();
93 let message = registry.get_install_message("update");
94 assert!(message.is_some());
95 assert!(message.unwrap().contains("atuin-update"));
96 }
97}