From 1855d94476eb1f6ad2ffe52b356b7c437a730ad5 Mon Sep 17 00:00:00 2001 From: Sean Young Date: Sat, 18 May 2024 21:21:33 +0100 Subject: [PATCH] Fix some comments Signed-off-by: Sean Young --- cir/src/bin/commands/config.rs | 2 +- cir/src/bin/commands/decode.rs | 4 ++-- cir/src/bin/commands/transmit.rs | 2 +- cir/src/keymap/decode.rs | 10 +++++----- cir/src/keymap/mod.rs | 2 ++ cir/src/keymap/parse.rs | 25 +++++++++++++------------ cir/tests/irctl_encode.rs | 2 +- 7 files changed, 25 insertions(+), 22 deletions(-) diff --git a/cir/src/bin/commands/config.rs b/cir/src/bin/commands/config.rs index 683b2d5..de039fe 100644 --- a/cir/src/bin/commands/config.rs +++ b/cir/src/bin/commands/config.rs @@ -381,7 +381,7 @@ fn load_keymap( keymap_filename: &Path, protocols: &mut Vec, ) { - let keymaps = match Keymap::parse(keymap_filename) { + let keymaps = match Keymap::parse_file(keymap_filename) { Ok(map) => map, Err(e) => { eprintln!("error: {}: {e}", keymap_filename.display()); diff --git a/cir/src/bin/commands/decode.rs b/cir/src/bin/commands/decode.rs index 6ac0145..7659e2e 100644 --- a/cir/src/bin/commands/decode.rs +++ b/cir/src/bin/commands/decode.rs @@ -256,7 +256,7 @@ pub fn decode_keymap(decode: &crate::Decode, path: &Path) { #[allow(unused_mut)] let mut max_gap = 100000; - let keymaps = match Keymap::parse(path) { + let keymaps = match Keymap::parse_file(path) { Ok(r) => r, Err(e) => { log::error!("{e}"); @@ -303,7 +303,7 @@ pub fn decode_keymap(decode: &crate::Decode, path: &Path) { for ir in raw { for decoder in &mut decoders { decoder.input(*ir, |name, _| { - println!("decoded: keymap:{} code:{}", decoder.remote.name, name); + println!("decoded: keymap:{} code:{}", decoder.keymap.name, name); }); } } diff --git a/cir/src/bin/commands/transmit.rs b/cir/src/bin/commands/transmit.rs index d70a340..9c50eeb 100644 --- a/cir/src/bin/commands/transmit.rs +++ b/cir/src/bin/commands/transmit.rs @@ -210,7 +210,7 @@ fn encode_args(transmit: &crate::Transmit) -> Message { } fn encode_keymap(args: &crate::TransmitKeymap) -> Message { - let remotes = match Keymap::parse(&args.keymap) { + let remotes = match Keymap::parse_file(&args.keymap) { Ok(r) => r, Err(e) => { log::error!("{e}"); diff --git a/cir/src/keymap/decode.rs b/cir/src/keymap/decode.rs index 03b70b6..41af10a 100644 --- a/cir/src/keymap/decode.rs +++ b/cir/src/keymap/decode.rs @@ -5,7 +5,7 @@ use irp::{Decoder, InfraredData, Irp, Options, DFA, NFA}; use log::debug; pub struct KeymapDecoder<'a> { - pub remote: &'a Keymap, + pub keymap: &'a Keymap, pub dfa: Vec<(DFA, Option)>, pub decoder: Vec>, } @@ -77,7 +77,7 @@ impl Keymap { let decoder = vec![Decoder::new(options); dfa.len()]; Ok(KeymapDecoder { - remote: self, + keymap: self, dfa, decoder, }) @@ -120,14 +120,14 @@ impl<'a> KeymapDecoder<'a> { }; if let Some(decoded) = scancode { - if self.remote.raw.is_empty() { - if let Some(key_code) = self.remote.scancodes.get(&decoded) { + if self.keymap.raw.is_empty() { + if let Some(key_code) = self.keymap.scancodes.get(&decoded) { callback(key_code, decoded); } } else { let decoded: usize = decoded as usize - u32::MAX as usize; - callback(&self.remote.raw[decoded].keycode, decoded as u64); + callback(&self.keymap.raw[decoded].keycode, decoded as u64); } } }) diff --git a/cir/src/keymap/mod.rs b/cir/src/keymap/mod.rs index f3749bf..a8bb7f7 100644 --- a/cir/src/keymap/mod.rs +++ b/cir/src/keymap/mod.rs @@ -1,3 +1,5 @@ +//! Parse, encode and decode linux rc keymaps + use irp::{Message, Pronto}; use std::collections::HashMap; diff --git a/cir/src/keymap/parse.rs b/cir/src/keymap/parse.rs index df7a541..6a51a54 100644 --- a/cir/src/keymap/parse.rs +++ b/cir/src/keymap/parse.rs @@ -70,7 +70,8 @@ fn string_to_scancode(s: &str) -> Result { } } impl Keymap { - pub fn parse(path: &Path) -> Result, String> { + /// Load a file and parse it as a Keymap + pub fn parse_file(path: &Path) -> Result, String> { let mut f = File::open(path).map_err(|e| format!("{}: {e}", path.display()))?; let mut contents = String::new(); @@ -78,11 +79,11 @@ impl Keymap { f.read_to_string(&mut contents) .map_err(|e| format!("{}: {e}", path.display()))?; - Keymap::parse_text(&contents, path) + Keymap::parse_str(&contents, path) } /// Parse a rc keymap file, either toml or old text format. No validation is done of key codes or protocol names - pub fn parse_text(contents: &str, filename: &Path) -> Result, String> { + pub fn parse_str(contents: &str, filename: &Path) -> Result, String> { if filename.extension() == Some(OsStr::new("toml")) { parse_toml(contents, filename) } else { @@ -449,7 +450,7 @@ fn parse_toml_test() { 0x1e1c = "KEY_TV" "#; - let k = Keymap::parse_text(s, Path::new("x.toml")).unwrap(); + let k = Keymap::parse_str(s, Path::new("x.toml")).unwrap(); assert_eq!(k[0].name, "hauppauge"); assert_eq!(k[0].protocol, "rc5"); @@ -472,7 +473,7 @@ fn parse_toml_test() { "#; assert_eq!( - Keymap::parse_text(s, Path::new("x.toml")), + Keymap::parse_str(s, Path::new("x.toml")), Err("x.toml: raw protocol is misssing raw entries".to_string()) ); @@ -485,7 +486,7 @@ fn parse_toml_test() { "#; assert_eq!( - Keymap::parse_text(s, Path::new("x.toml")), + Keymap::parse_str(s, Path::new("x.toml")), Err("x.toml: raw entry has neither pronto hex code nor raw".to_string()) ); @@ -499,7 +500,7 @@ fn parse_toml_test() { "#; assert_eq!( - Keymap::parse_text(s, Path::new("x.toml")), + Keymap::parse_str(s, Path::new("x.toml")), Err("x.toml: raw entry has neither pronto hex code nor raw".to_string()) ); } @@ -513,7 +514,7 @@ fn parse_text_test() { 0x1e1c KEY_TV "#; - let k = Keymap::parse_text(s, Path::new("hauppauge")).unwrap(); + let k = Keymap::parse_str(s, Path::new("hauppauge")).unwrap(); assert_eq!(k[0].name, "hauppauge"); assert_eq!(k[0].protocol, "RC5"); @@ -535,7 +536,7 @@ fn parse_text_test() { 0x800f0403 KEY_NUMERIC_3 "#; - let k = Keymap::parse_text(s, Path::new("hauppauge")).unwrap(); + let k = Keymap::parse_str(s, Path::new("hauppauge")).unwrap(); assert_eq!(k[0].name, "rc6_mce"); assert_eq!(k[0].protocol, "RC6"); @@ -558,7 +559,7 @@ fn parse_text_test() { 0x28c2 KEY_NUMERIC_2 "#; - let k = Keymap::parse_text(s, Path::new("hauppauge")).unwrap(); + let k = Keymap::parse_str(s, Path::new("hauppauge")).unwrap(); assert_eq!(k[0].name, "streamzap"); assert_eq!(k[0].protocol, "RC-5-SZ"); @@ -584,7 +585,7 @@ fn parse_bpf_toml_test() { 0x1e1c = "KEY_TV" "#; - let k = Keymap::parse_text(s, Path::new("x.toml")).unwrap(); + let k = Keymap::parse_str(s, Path::new("x.toml")).unwrap(); assert_eq!(k[0].name, "meh"); assert_eq!(k[0].protocol, "manchester"); @@ -607,7 +608,7 @@ fn parse_bpf_toml_test() { 0x1e1c = "KEY_TV" "#; - let k = Keymap::parse_text(s, Path::new("x.toml")).unwrap(); + let k = Keymap::parse_str(s, Path::new("x.toml")).unwrap(); assert_eq!(k[0].name, "meh"); assert_eq!(k[0].protocol, "manchester"); diff --git a/cir/tests/irctl_encode.rs b/cir/tests/irctl_encode.rs index ba51627..db63232 100644 --- a/cir/tests/irctl_encode.rs +++ b/cir/tests/irctl_encode.rs @@ -65,7 +65,7 @@ fn recurse(path: &Path) { if e.metadata().unwrap().file_type().is_dir() { recurse(&path); } else if path.to_string_lossy().ends_with(".toml") { - for keymap in Keymap::parse(&path).unwrap() { + for keymap in Keymap::parse_file(&path).unwrap() { if ["pulse_distance", "pulse_length", "manchester"] .contains(&keymap.protocol.as_str()) {