use std::collections::BTreeMap;
use crate::action::{Action, BindingContextName};
use crate::matcher::InputMatcher;
#[derive(Debug, Clone)]
pub(crate) struct ContextualBindings<A> {
pub(crate) bindings: BTreeMap<BindingContextName, Vec<Binding<A>>>,
}
impl<'text, 'raw, A: Action> TryFrom<nojson::RawJsonValue<'text, 'raw>> for ContextualBindings<A> {
type Error = nojson::JsonParseError;
fn try_from(value: nojson::RawJsonValue<'text, 'raw>) -> Result<Self, Self::Error> {
Ok(Self {
bindings: value
.to_object()?
.map(|(k, v)| {
let context_name = k.try_into()?;
let bindings = v.try_into()?;
Ok((context_name, bindings))
})
.collect::<Result<_, _>>()?,
})
}
}
#[derive(Debug, Clone)]
pub struct Binding<A> {
pub triggers: Vec<InputMatcher>,
pub label: Option<String>,
pub action: Option<A>,
pub context: Option<BindingContextName>,
}
impl<A: Action> Binding<A> {
pub fn matches(&self, input: tuinix::TerminalInput) -> bool {
self.triggers.iter().any(|t| t.matches(input))
}
}
impl<'text, 'raw, A: Action> TryFrom<nojson::RawJsonValue<'text, 'raw>> for Binding<A> {
type Error = nojson::JsonParseError;
fn try_from(value: nojson::RawJsonValue<'text, 'raw>) -> Result<Self, Self::Error> {
Ok(Self {
triggers: value
.to_member("triggers")?
.map(TryFrom::try_from)?
.unwrap_or_default(),
label: value.to_member("label")?.map(TryFrom::try_from)?,
action: value.to_member("action")?.map(TryFrom::try_from)?,
context: value.to_member("context")?.map(TryFrom::try_from)?,
})
}
}