#![allow(dead_code)]
#[allow(dead_code)]
#[derive(Debug, Clone, PartialEq)]
pub struct Shortcut {
pub key: u32,
pub modifiers: u8,
pub action_id: u32,
pub description: String,
}
#[allow(dead_code)]
#[derive(Debug, Clone)]
pub struct ShortcutRegistry {
pub shortcuts: Vec<Shortcut>,
}
#[allow(dead_code)]
pub fn new_shortcut_registry() -> ShortcutRegistry {
ShortcutRegistry { shortcuts: Vec::new() }
}
#[allow(dead_code)]
pub fn register_shortcut(
reg: &mut ShortcutRegistry,
key: u32,
mods: u8,
action: u32,
desc: &str,
) {
reg.shortcuts.push(Shortcut {
key,
modifiers: mods,
action_id: action,
description: desc.to_string(),
});
}
#[allow(dead_code)]
pub fn find_shortcut(reg: &ShortcutRegistry, key: u32, mods: u8) -> Option<u32> {
reg.shortcuts
.iter()
.find(|s| s.key == key && s.modifiers == mods)
.map(|s| s.action_id)
}
#[allow(dead_code)]
pub fn shortcut_count(reg: &ShortcutRegistry) -> usize {
reg.shortcuts.len()
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_new_empty() {
let reg = new_shortcut_registry();
assert_eq!(shortcut_count(®), 0);
}
#[test]
fn test_register_and_count() {
let mut reg = new_shortcut_registry();
register_shortcut(&mut reg, 83, 1, 10, "Save (Ctrl+S)");
assert_eq!(shortcut_count(®), 1);
}
#[test]
fn test_find_existing() {
let mut reg = new_shortcut_registry();
register_shortcut(&mut reg, 90, 1, 20, "Undo (Ctrl+Z)");
let action = find_shortcut(®, 90, 1);
assert_eq!(action, Some(20));
}
#[test]
fn test_find_not_found() {
let reg = new_shortcut_registry();
assert!(find_shortcut(®, 65, 0).is_none());
}
#[test]
fn test_different_mods_no_match() {
let mut reg = new_shortcut_registry();
register_shortcut(&mut reg, 65, 1, 5, "Ctrl+A");
assert!(find_shortcut(®, 65, 2).is_none());
}
#[test]
fn test_multiple_shortcuts() {
let mut reg = new_shortcut_registry();
register_shortcut(&mut reg, 1, 0, 1, "A");
register_shortcut(&mut reg, 2, 0, 2, "B");
register_shortcut(&mut reg, 3, 0, 3, "C");
assert_eq!(shortcut_count(®), 3);
}
#[test]
fn test_find_second_shortcut() {
let mut reg = new_shortcut_registry();
register_shortcut(&mut reg, 1, 0, 100, "First");
register_shortcut(&mut reg, 2, 0, 200, "Second");
assert_eq!(find_shortcut(®, 2, 0), Some(200));
}
#[test]
fn test_shortcut_description() {
let mut reg = new_shortcut_registry();
register_shortcut(&mut reg, 70, 0, 30, "Focus view");
assert_eq!(reg.shortcuts[0].description, "Focus view");
}
#[test]
fn test_no_mods_zero() {
let mut reg = new_shortcut_registry();
register_shortcut(&mut reg, 70, 0, 99, "F key");
assert_eq!(find_shortcut(®, 70, 0), Some(99));
}
}