use crate::Key;
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct Binding {
keys: Vec<String>,
help: Help,
disabled: bool,
}
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct Help {
pub key: String,
pub desc: String,
}
impl Binding {
#[must_use]
pub fn new(keys: &[&str]) -> Self {
Self {
keys: keys.iter().map(|k| (*k).to_string()).collect(),
..Self::default()
}
}
#[must_use]
pub fn with_help(mut self, key: &str, desc: &str) -> Self {
self.set_help(key, desc);
self
}
#[must_use]
pub fn with_disabled(mut self) -> Self {
self.disabled = true;
self
}
pub fn set_keys(&mut self, keys: &[&str]) {
self.keys = keys.iter().map(|k| (*k).to_string()).collect();
}
#[must_use]
pub fn keys(&self) -> &[String] {
&self.keys
}
pub fn set_help(&mut self, key: &str, desc: &str) {
self.help = Help {
key: key.to_string(),
desc: desc.to_string(),
};
}
#[must_use]
pub const fn help(&self) -> &Help {
&self.help
}
#[must_use]
pub fn enabled(&self) -> bool {
!self.disabled && !self.keys.is_empty()
}
pub const fn set_enabled(&mut self, on: bool) {
self.disabled = !on;
}
pub fn unbind(&mut self) {
self.keys.clear();
self.help = Help::default();
}
#[must_use]
pub fn matches(&self, key: &Key) -> bool {
self.enabled() && self.keys.iter().any(|k| *key == k.as_str())
}
}
#[must_use]
pub fn matches(key: &Key, bindings: &[&Binding]) -> bool {
bindings.iter().any(|b| b.matches(key))
}
#[cfg(test)]
mod tests;