use egaku::{KeyCombo, KeyMap};
use pauta_spec::{Catalog, Effect, Gate};
#[must_use]
pub fn keymap(catalog: &Catalog) -> KeyMap<Effect> {
let mut m = KeyMap::new();
for a in catalog.actions() {
m.bind(KeyCombo::key(&a.binding), a.effect);
}
m
}
#[must_use]
pub fn legend_parts(catalog: &Catalog) -> Vec<String> {
catalog
.actions()
.iter()
.map(|a| {
let mut s = String::from(&a.binding);
s.push(':');
s.push_str(&a.name);
if a.gate == Gate::Binding {
s.push('!');
}
s
})
.collect()
}
#[cfg(test)]
mod tests {
use super::*;
fn catalog() -> Catalog {
Catalog::load().expect("the shipped vocabulary must compile")
}
#[test]
fn every_authored_action_reaches_the_keymap() {
let c = catalog();
let m = keymap(&c);
assert_eq!(
m.len(),
c.actions().len(),
"a chord was swallowed between the catalog and the keymap"
);
for a in c.actions() {
assert_eq!(
m.lookup(&KeyCombo::key(&a.binding)),
Some(&a.effect),
"chord `{}` did not resolve to its authored effect",
a.binding
);
}
}
#[test]
fn the_legend_describes_exactly_what_is_bound() {
let c = catalog();
let legend = legend_parts(&c);
assert_eq!(legend.len(), c.actions().len());
for (part, action) in legend.iter().zip(c.actions()) {
let mut want = String::from(&action.binding);
want.push(':');
assert!(
part.starts_with(&want),
"legend entry {part:?} does not name the chord it is bound to"
);
}
}
#[test]
fn the_legend_marks_the_gated_actions() {
let c = catalog();
let legend = legend_parts(&c);
let marked: Vec<&String> = legend.iter().filter(|p| p.ends_with('!')).collect();
assert_eq!(
marked.len(),
2,
"approve and request-changes must be visibly marked as gated: {legend:?}"
);
}
#[test]
fn the_gate_comes_from_the_catalog() {
let c = catalog();
assert!(c.is_binding(Effect::Approve));
assert!(!c.is_binding(Effect::SelectNext));
}
}