fresh/input/
buffer_mode.rs1use std::collections::HashMap;
7
8#[derive(Debug, Clone)]
10pub struct BufferMode {
11 pub name: String,
13
14 pub read_only: bool,
16
17 pub allow_text_input: bool,
22
23 pub plugin_name: Option<String>,
25}
26
27impl BufferMode {
28 pub fn new(name: impl Into<String>) -> Self {
30 Self {
31 name: name.into(),
32 read_only: false,
33 allow_text_input: false,
34 plugin_name: None,
35 }
36 }
37
38 pub fn with_read_only(mut self, read_only: bool) -> Self {
40 self.read_only = read_only;
41 self
42 }
43
44 pub fn with_plugin_name(mut self, plugin_name: Option<String>) -> Self {
46 self.plugin_name = plugin_name;
47 self
48 }
49
50 pub fn with_allow_text_input(mut self, allow: bool) -> Self {
52 self.allow_text_input = allow;
53 self
54 }
55}
56
57#[derive(Debug, Clone)]
61pub struct ModeRegistry {
62 modes: HashMap<String, BufferMode>,
64}
65
66impl ModeRegistry {
67 pub fn new() -> Self {
69 Self {
70 modes: HashMap::new(),
71 }
72 }
73
74 pub fn register(&mut self, mode: BufferMode) {
76 self.modes.insert(mode.name.clone(), mode);
77 }
78
79 pub fn get(&self, name: &str) -> Option<&BufferMode> {
81 self.modes.get(name)
82 }
83
84 pub fn is_read_only(&self, mode_name: &str) -> bool {
86 self.modes
87 .get(mode_name)
88 .map(|m| m.read_only)
89 .unwrap_or(false)
90 }
91
92 pub fn allows_text_input(&self, mode_name: &str) -> bool {
94 self.modes
95 .get(mode_name)
96 .map(|m| m.allow_text_input)
97 .unwrap_or(false)
98 }
99
100 pub fn list_modes(&self) -> Vec<String> {
102 self.modes.keys().cloned().collect()
103 }
104
105 pub fn has_mode(&self, name: &str) -> bool {
107 self.modes.contains_key(name)
108 }
109}
110
111impl Default for ModeRegistry {
112 fn default() -> Self {
113 Self::new()
114 }
115}
116
117#[cfg(test)]
118mod tests {
119 use super::*;
120
121 #[test]
122 fn test_mode_metadata() {
123 let mut registry = ModeRegistry::new();
124
125 let mode = BufferMode::new("test-mode")
126 .with_read_only(true)
127 .with_allow_text_input(true)
128 .with_plugin_name(Some("test-plugin".to_string()));
129
130 registry.register(mode);
131
132 assert!(registry.has_mode("test-mode"));
133 assert!(registry.is_read_only("test-mode"));
134 assert!(registry.allows_text_input("test-mode"));
135 assert_eq!(
136 registry.get("test-mode").unwrap().plugin_name,
137 Some("test-plugin".to_string())
138 );
139 }
140
141 #[test]
142 fn test_mode_defaults() {
143 let registry = ModeRegistry::new();
144 assert!(!registry.is_read_only("nonexistent"));
145 assert!(!registry.allows_text_input("nonexistent"));
146 }
147}