Skip to content

Commit

Permalink
refactor: validate defcfg values in parser (#624)
Browse files Browse the repository at this point in the history
1. Move validation of cfg values to `parse_defcfg` instead of validating
them all over the codebase. Reason: before kanata didn't show location
when parsing of a cfg value failed, now it does.

2. Replace `HashMap<String, String>` with `CfgOptions`, which holds only
validated cfg values. Reason: Allows to easily parse lists as values,
since now the actual value parsing is no longer delayed after parser
function exited. This was done with #121 in mind.

3. Move cfg options defaults to a `CfgOptions::default`. Reason: it's
good to have similar code in one place.
  • Loading branch information
rszyma authored Nov 22, 2023
1 parent 54daa6b commit 1f7af8f
Show file tree
Hide file tree
Showing 10 changed files with 623 additions and 542 deletions.
2 changes: 1 addition & 1 deletion docs/config.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -707,7 +707,7 @@ a non-applicable operating system.
linux-dev /dev/input/dev1:/dev/input/dev2
linux-dev-names-include "Name 1:Name 2"
linux-dev-names-exclude "Name 3:Name 4"
linux-continue-if-no-dev-found yes
linux-continue-if-no-devs-found yes
linux-unicode-u-code v
linux-unicode-termination space
linux-x11-repeat-delay-rate 400,50
Expand Down
234 changes: 90 additions & 144 deletions keyberon/src/action/switch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -278,14 +278,11 @@ fn bool_evaluation_test_0() {
OpCode::new_key(KeyCode::F),
];
let keycodes = [KeyCode::A, KeyCode::B, KeyCode::D, KeyCode::F];
assert_eq!(
evaluate_boolean(
opcodes.as_slice(),
keycodes.iter().copied(),
[].iter().copied()
),
true
);
assert!(evaluate_boolean(
opcodes.as_slice(),
keycodes.iter().copied(),
[].iter().copied()
));
}

#[test]
Expand All @@ -309,14 +306,11 @@ fn bool_evaluation_test_1() {
KeyCode::E,
KeyCode::F,
];
assert_eq!(
evaluate_boolean(
opcodes.as_slice(),
keycodes.iter().copied(),
[].iter().copied()
),
true
);
assert!(evaluate_boolean(
opcodes.as_slice(),
keycodes.iter().copied(),
[].iter().copied()
));
}

#[test]
Expand All @@ -333,14 +327,11 @@ fn bool_evaluation_test_2() {
OpCode(KeyCode::F as u16),
];
let keycodes = [KeyCode::A, KeyCode::B, KeyCode::E, KeyCode::F];
assert_eq!(
evaluate_boolean(
opcodes.as_slice(),
keycodes.iter().copied(),
[].iter().copied()
),
false
);
assert!(!evaluate_boolean(
opcodes.as_slice(),
keycodes.iter().copied(),
[].iter().copied()
));
}

#[test]
Expand All @@ -357,28 +348,22 @@ fn bool_evaluation_test_3() {
OpCode(KeyCode::F as u16),
];
let keycodes = [KeyCode::B, KeyCode::C, KeyCode::D, KeyCode::E, KeyCode::F];
assert_eq!(
evaluate_boolean(
opcodes.as_slice(),
keycodes.iter().copied(),
[].iter().copied()
),
false
);
assert!(!evaluate_boolean(
opcodes.as_slice(),
keycodes.iter().copied(),
[].iter().copied()
));
}

#[test]
fn bool_evaluation_test_4() {
let opcodes = [];
let keycodes = [];
assert_eq!(
evaluate_boolean(
opcodes.as_slice(),
keycodes.iter().copied(),
[].iter().copied()
),
true
);
assert!(evaluate_boolean(
opcodes.as_slice(),
keycodes.iter().copied(),
[].iter().copied()
));
}

#[test]
Expand All @@ -392,14 +377,11 @@ fn bool_evaluation_test_5() {
KeyCode::E,
KeyCode::F,
];
assert_eq!(
evaluate_boolean(
opcodes.as_slice(),
keycodes.iter().copied(),
[].iter().copied()
),
true
);
assert!(evaluate_boolean(
opcodes.as_slice(),
keycodes.iter().copied(),
[].iter().copied()
));
}

#[test]
Expand All @@ -413,28 +395,22 @@ fn bool_evaluation_test_6() {
KeyCode::E,
KeyCode::F,
];
assert_eq!(
evaluate_boolean(
opcodes.as_slice(),
keycodes.iter().copied(),
[].iter().copied()
),
true
);
assert!(evaluate_boolean(
opcodes.as_slice(),
keycodes.iter().copied(),
[].iter().copied()
));
}

#[test]
fn bool_evaluation_test_7() {
let opcodes = [OpCode(KeyCode::A as u16), OpCode(KeyCode::B as u16)];
let keycodes = [KeyCode::C, KeyCode::D, KeyCode::E, KeyCode::F];
assert_eq!(
evaluate_boolean(
opcodes.as_slice(),
keycodes.iter().copied(),
[].iter().copied()
),
false
);
assert!(!evaluate_boolean(
opcodes.as_slice(),
keycodes.iter().copied(),
[].iter().copied()
));
}

#[test]
Expand All @@ -446,14 +422,11 @@ fn bool_evaluation_test_9() {
OpCode(KeyCode::C as u16),
];
let keycodes = [KeyCode::C, KeyCode::D, KeyCode::E, KeyCode::F];
assert_eq!(
evaluate_boolean(
opcodes.as_slice(),
keycodes.iter().copied(),
[].iter().copied()
),
true
);
assert!(evaluate_boolean(
opcodes.as_slice(),
keycodes.iter().copied(),
[].iter().copied()
));
}

#[test]
Expand All @@ -465,14 +438,11 @@ fn bool_evaluation_test_10() {
OpCode(KeyCode::C as u16),
];
let keycodes = [KeyCode::C, KeyCode::D, KeyCode::E, KeyCode::F];
assert_eq!(
evaluate_boolean(
opcodes.as_slice(),
keycodes.iter().copied(),
[].iter().copied()
),
false
);
assert!(!evaluate_boolean(
opcodes.as_slice(),
keycodes.iter().copied(),
[].iter().copied()
));
}

#[test]
Expand All @@ -483,14 +453,11 @@ fn bool_evaluation_test_11() {
OpCode(KeyCode::B as u16),
];
let keycodes = [KeyCode::C, KeyCode::D, KeyCode::E, KeyCode::F];
assert_eq!(
evaluate_boolean(
opcodes.as_slice(),
keycodes.iter().copied(),
[].iter().copied()
),
false
);
assert!(!evaluate_boolean(
opcodes.as_slice(),
keycodes.iter().copied(),
[].iter().copied()
));
}

#[test]
Expand All @@ -503,14 +470,11 @@ fn bool_evaluation_test_12() {
OpCode(KeyCode::C as u16),
];
let keycodes = [KeyCode::C, KeyCode::D, KeyCode::E, KeyCode::F];
assert_eq!(
evaluate_boolean(
opcodes.as_slice(),
keycodes.iter().copied(),
[].iter().copied()
),
true
);
assert!(evaluate_boolean(
opcodes.as_slice(),
keycodes.iter().copied(),
[].iter().copied()
));
}

#[test]
Expand All @@ -526,14 +490,11 @@ fn bool_evaluation_test_max_depth_does_not_panic() {
OpCode(0x1008),
];
let keycodes = [];
assert_eq!(
evaluate_boolean(
opcodes.as_slice(),
keycodes.iter().copied(),
[].iter().copied()
),
true
);
assert!(evaluate_boolean(
opcodes.as_slice(),
keycodes.iter().copied(),
[].iter().copied()
));
}

#[test]
Expand All @@ -551,14 +512,11 @@ fn bool_evaluation_test_more_than_max_depth_panics() {
OpCode(0x1009),
];
let keycodes = [];
assert_eq!(
evaluate_boolean(
opcodes.as_slice(),
keycodes.iter().copied(),
[].iter().copied()
),
true
);
assert!(evaluate_boolean(
opcodes.as_slice(),
keycodes.iter().copied(),
[].iter().copied()
));
}

#[test]
Expand Down Expand Up @@ -632,38 +590,26 @@ fn switch_historical_1() {
KeyCode::G,
KeyCode::H,
];
assert_eq!(
evaluate_boolean(
opcode_true.as_slice(),
[].iter().copied(),
hist_keycodes.iter().copied(),
),
true
);
assert_eq!(
evaluate_boolean(
opcode_true2.as_slice(),
[].iter().copied(),
hist_keycodes.iter().copied(),
),
true
);
assert_eq!(
evaluate_boolean(
opcode_false.as_slice(),
[].iter().copied(),
hist_keycodes.iter().copied(),
),
false
);
assert_eq!(
evaluate_boolean(
opcode_false2.as_slice(),
[].iter().copied(),
hist_keycodes.iter().copied(),
),
false
);
assert!(evaluate_boolean(
opcode_true.as_slice(),
[].iter().copied(),
hist_keycodes.iter().copied(),
));
assert!(evaluate_boolean(
opcode_true2.as_slice(),
[].iter().copied(),
hist_keycodes.iter().copied(),
));
assert!(!evaluate_boolean(
opcode_false.as_slice(),
[].iter().copied(),
hist_keycodes.iter().copied(),
));
assert!(!evaluate_boolean(
opcode_false2.as_slice(),
[].iter().copied(),
hist_keycodes.iter().copied(),
));
}

#[test]
Expand Down
Loading

0 comments on commit 1f7af8f

Please sign in to comment.