Skip to content

Commit

Permalink
feat: move bindings and definitions into their own modules
Browse files Browse the repository at this point in the history
The lib.rs file got way too big. I'm sure LSPs on low end machines will
choke on it.
  • Loading branch information
lavafroth committed Jul 30, 2024
1 parent a926d94 commit 629c81e
Show file tree
Hide file tree
Showing 4 changed files with 217 additions and 196 deletions.
42 changes: 42 additions & 0 deletions src/bindings.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
use std::fmt::Display;

use crate::{Definition, ModeInstruction};

#[derive(Debug, PartialEq, Eq)]
pub struct Binding {
pub definition: Definition,
pub command: String,
pub mode_instructions: Vec<ModeInstruction>,
}

impl Binding {
pub fn running<S: AsRef<str>>(command: S) -> BindingBuilder {
BindingBuilder {
command: command.as_ref().to_string(),
}
}
}

pub struct BindingBuilder {
pub command: String,
}

impl BindingBuilder {
pub fn on(self, definition: Definition) -> Binding {
Binding {
definition,
command: self.command,
mode_instructions: vec![],
}
}
}

impl Display for Binding {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(
f,
"Binding {} \u{2192} {} (mode instructions: {:?})",
self.definition, self.command, self.mode_instructions
)
}
}
114 changes: 114 additions & 0 deletions src/definition.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
use itertools::Itertools;
use pest::iterators::Pair;

use crate::{
pair_to_string, parse_key,
range::Bounds,
token::{Key, KeyAttribute, Modifier},
KeyRepr, ModifierRepr, ParseError, Rule,
};
use std::{collections::BTreeSet, fmt::Display};

#[derive(Debug, PartialEq, Eq)]
pub struct Definition {
pub modifiers: BTreeSet<Modifier>,
pub key: Key,
}

impl Definition {
pub fn new(key: evdev::Key) -> Self {
Self {
modifiers: BTreeSet::default(),
key: Key::new(key, KeyAttribute::None),
}
}

pub fn with_modifiers(mut self, modifiers: &[Modifier]) -> Self {
self.modifiers = modifiers.iter().cloned().collect();
self
}
}

impl Display for Definition {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "[")?;
for modifier in self.modifiers.iter() {
write!(f, "{:?}, ", modifier)?;
}
write!(f, "{:?}", self.key)?;
write!(f, "]")
}
}

#[derive(Default)]
pub struct DefinitionUncompiled {
pub modifiers: Vec<Vec<Modifier>>,
pub keys: Vec<Key>,
}

impl DefinitionUncompiled {
pub fn ingest(&mut self, component: Pair<'_, Rule>) -> Result<(), ParseError> {
match component.as_rule() {
Rule::modifier => {
self.modifiers.push(vec![
ModifierRepr(pair_to_string(component).to_lowercase()).into()
])
}
Rule::modifier_shorthand | Rule::modifier_omit_shorthand => self.modifiers.push(
component
.into_inner()
.map(|component| ModifierRepr(pair_to_string(component)).into())
.collect(),
),
Rule::shorthand => {
for shorthand_component in component.into_inner() {
match shorthand_component.as_rule() {
Rule::key_in_shorthand => {
self.keys.push(parse_key(shorthand_component).try_into()?)
}
Rule::key_range => {
let (lower_bound, upper_bound) =
Bounds::new(shorthand_component).expand_keys()?;
let keys = (lower_bound..=upper_bound)
.map(|key| {
KeyRepr {
key: key.to_string(),
attribute: KeyAttribute::None,
}
.try_into()
})
.collect::<Result<Vec<Key>, ParseError>>()?;
self.keys.extend(keys);
}
_ => {}
}
}
}
Rule::key_normal => self.keys.push(parse_key(component).try_into()?),
_ => {}
};
Ok(())
}

pub fn compile(self) -> Vec<Definition> {
if self.modifiers.is_empty() {
return self
.keys
.into_iter()
.map(|key| Definition {
modifiers: BTreeSet::default(),
key,
})
.collect();
}
self.modifiers
.into_iter()
.multi_cartesian_product()
.cartesian_product(self.keys)
.map(|(modifiers, key)| Definition {
modifiers: modifiers.into_iter().collect(),
key,
})
.collect()
}
}
Loading

0 comments on commit 629c81e

Please sign in to comment.