1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
use crate::{ events::{KeyParseAllError, KeyParser}, mode::ModeKind, platform::Key, }; pub enum MatchResult<'a> { None, Prefix, ReplaceWith(&'a [Key]), } #[derive(Debug)] pub enum ParseKeyMapError { From(KeyParseAllError), To(KeyParseAllError), } struct KeyMap { from: Vec<Key>, to: Vec<Key>, } #[derive(Default)] pub struct KeyMapCollection { maps: [Vec<KeyMap>; 5], } impl KeyMapCollection { pub fn parse_and_map( &mut self, mode_kind: ModeKind, from: &str, to: &str, ) -> Result<(), ParseKeyMapError> { fn parse_keys(text: &str) -> Result<Vec<Key>, KeyParseAllError> { let mut keys = Vec::new(); for key in KeyParser::new(text) { match key { Ok(key) => keys.push(key), Err(error) => return Err(error), } } Ok(keys) } let map = KeyMap { from: parse_keys(from).map_err(|e| ParseKeyMapError::From(e))?, to: parse_keys(to).map_err(|e| ParseKeyMapError::To(e))?, }; let maps = &mut self.maps[mode_kind as usize]; for m in maps.iter_mut() { if m.from == map.from { m.to = map.to; return Ok(()); } } maps.push(map); Ok(()) } pub fn matches<'a>(&'a self, mode_kind: ModeKind, keys: &[Key]) -> MatchResult<'a> { let maps = &self.maps[mode_kind as usize]; let mut has_prefix = false; for map in maps { if map.from.iter().zip(keys.iter()).all(|(a, b)| a == b) { has_prefix = true; if map.from.len() == keys.len() { return MatchResult::ReplaceWith(&map.to); } } } if has_prefix { MatchResult::Prefix } else { MatchResult::None } } }