use std::collections::HashMap;
#[derive(Debug, Clone)]
pub struct BufferMode {
pub name: String,
pub read_only: bool,
pub allow_text_input: bool,
pub inherit_normal_bindings: bool,
pub plugin_name: Option<String>,
}
impl BufferMode {
pub fn new(name: impl Into<String>) -> Self {
Self {
name: name.into(),
read_only: false,
allow_text_input: false,
inherit_normal_bindings: false,
plugin_name: None,
}
}
pub fn with_read_only(mut self, read_only: bool) -> Self {
self.read_only = read_only;
self
}
pub fn with_plugin_name(mut self, plugin_name: Option<String>) -> Self {
self.plugin_name = plugin_name;
self
}
pub fn with_allow_text_input(mut self, allow: bool) -> Self {
self.allow_text_input = allow;
self
}
pub fn with_inherit_normal_bindings(mut self, inherit: bool) -> Self {
self.inherit_normal_bindings = inherit;
self
}
}
#[derive(Debug, Clone)]
pub struct ModeRegistry {
modes: HashMap<String, BufferMode>,
}
impl ModeRegistry {
pub fn new() -> Self {
Self {
modes: HashMap::new(),
}
}
pub fn register(&mut self, mode: BufferMode) {
self.modes.insert(mode.name.clone(), mode);
}
pub fn get(&self, name: &str) -> Option<&BufferMode> {
self.modes.get(name)
}
pub fn is_read_only(&self, mode_name: &str) -> bool {
self.modes
.get(mode_name)
.map(|m| m.read_only)
.unwrap_or(false)
}
pub fn allows_text_input(&self, mode_name: &str) -> bool {
self.modes
.get(mode_name)
.map(|m| m.allow_text_input)
.unwrap_or(false)
}
pub fn inherits_normal_bindings(&self, mode_name: &str) -> bool {
self.modes
.get(mode_name)
.map(|m| m.inherit_normal_bindings)
.unwrap_or(false)
}
pub fn list_modes(&self) -> Vec<String> {
self.modes.keys().cloned().collect()
}
pub fn has_mode(&self, name: &str) -> bool {
self.modes.contains_key(name)
}
}
impl Default for ModeRegistry {
fn default() -> Self {
Self::new()
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_mode_metadata() {
let mut registry = ModeRegistry::new();
let mode = BufferMode::new("test-mode")
.with_read_only(true)
.with_allow_text_input(true)
.with_plugin_name(Some("test-plugin".to_string()));
registry.register(mode);
assert!(registry.has_mode("test-mode"));
assert!(registry.is_read_only("test-mode"));
assert!(registry.allows_text_input("test-mode"));
assert_eq!(
registry.get("test-mode").unwrap().plugin_name,
Some("test-plugin".to_string())
);
}
#[test]
fn test_mode_defaults() {
let registry = ModeRegistry::new();
assert!(!registry.is_read_only("nonexistent"));
assert!(!registry.allows_text_input("nonexistent"));
}
}