Skip to content

Commit

Permalink
Fix some comments
Browse files Browse the repository at this point in the history
Signed-off-by: Sean Young <[email protected]>
  • Loading branch information
seanyoung committed May 18, 2024
1 parent 692d9df commit 1855d94
Show file tree
Hide file tree
Showing 7 changed files with 25 additions and 22 deletions.
2 changes: 1 addition & 1 deletion cir/src/bin/commands/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -381,7 +381,7 @@ fn load_keymap(
keymap_filename: &Path,
protocols: &mut Vec<usize>,
) {
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());
Expand Down
4 changes: 2 additions & 2 deletions cir/src/bin/commands/decode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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}");
Expand Down Expand Up @@ -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);
});
}
}
Expand Down
2 changes: 1 addition & 1 deletion cir/src/bin/commands/transmit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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}");
Expand Down
10 changes: 5 additions & 5 deletions cir/src/keymap/decode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Irp>)>,
pub decoder: Vec<Decoder<'a>>,
}
Expand Down Expand Up @@ -77,7 +77,7 @@ impl Keymap {
let decoder = vec![Decoder::new(options); dfa.len()];

Ok(KeymapDecoder {
remote: self,
keymap: self,
dfa,
decoder,
})
Expand Down Expand Up @@ -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);
}
}
})
Expand Down
2 changes: 2 additions & 0 deletions cir/src/keymap/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
//! Parse, encode and decode linux rc keymaps
use irp::{Message, Pronto};
use std::collections::HashMap;

Expand Down
25 changes: 13 additions & 12 deletions cir/src/keymap/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,19 +70,20 @@ fn string_to_scancode(s: &str) -> Result<u64, std::num::ParseIntError> {
}
}
impl Keymap {
pub fn parse(path: &Path) -> Result<Vec<Keymap>, String> {
/// Load a file and parse it as a Keymap
pub fn parse_file(path: &Path) -> Result<Vec<Keymap>, String> {
let mut f = File::open(path).map_err(|e| format!("{}: {e}", path.display()))?;

let mut contents = String::new();

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<Vec<Keymap>, String> {
pub fn parse_str(contents: &str, filename: &Path) -> Result<Vec<Keymap>, String> {
if filename.extension() == Some(OsStr::new("toml")) {
parse_toml(contents, filename)
} else {
Expand Down Expand Up @@ -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");
Expand All @@ -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())
);

Expand All @@ -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())
);

Expand All @@ -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())
);
}
Expand All @@ -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");
Expand All @@ -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");
Expand All @@ -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");
Expand All @@ -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");
Expand All @@ -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");
Expand Down
2 changes: 1 addition & 1 deletion cir/tests/irctl_encode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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())
{
Expand Down

0 comments on commit 1855d94

Please sign in to comment.