Move key binding groups to config crate

This commit is contained in:
Joscha 2023-04-27 15:31:49 +02:00
parent 5a0efd69e4
commit e1c3a463b2
7 changed files with 33 additions and 7 deletions

1
Cargo.lock generated
View file

@ -281,6 +281,7 @@ dependencies = [
name = "cove-config" name = "cove-config"
version = "0.6.1" version = "0.6.1"
dependencies = [ dependencies = [
"cove-input",
"cove-macro", "cove-macro",
"serde", "serde",
"toml", "toml",

View file

@ -4,6 +4,7 @@ version = { workspace = true }
edition = { workspace = true } edition = { workspace = true }
[dependencies] [dependencies]
cove-input = { path = "../cove-input" }
cove-macro = { path = "../cove-macro" } cove-macro = { path = "../cove-macro" }
serde = { workspace = true } serde = { workspace = true }

View file

@ -1,6 +1,4 @@
use cove_macro::Group; use cove_input::{Group, KeyBinding};
use crate::KeyBinding;
#[derive(Debug, Group)] #[derive(Debug, Group)]
pub struct General { pub struct General {

View file

@ -11,6 +11,7 @@
pub mod doc; pub mod doc;
mod euph; mod euph;
mod keys;
use std::fs; use std::fs;
use std::path::{Path, PathBuf}; use std::path::{Path, PathBuf};
@ -19,6 +20,7 @@ use doc::Document;
use serde::Deserialize; use serde::Deserialize;
pub use crate::euph::*; pub use crate::euph::*;
pub use crate::keys::*;
#[derive(Debug, Default, Deserialize, Document)] #[derive(Debug, Default, Deserialize, Document)]
pub struct Config { pub struct Config {

View file

@ -173,11 +173,35 @@ impl<'de> Deserialize<'de> for KeyPress {
pub struct KeyBinding(Vec<KeyPress>); pub struct KeyBinding(Vec<KeyPress>);
impl KeyBinding { impl KeyBinding {
pub fn new() -> Self {
Self(vec![])
}
pub fn with_key(self, key: &str) -> Result<Self, ParseKeysError> {
self.with_keys([key])
}
pub fn with_keys<'a, I>(mut self, keys: I) -> Result<Self, ParseKeysError>
where
I: IntoIterator<Item = &'a str>,
{
for key in keys {
self.0.push(key.parse()?);
}
Ok(self)
}
pub fn matches(&self, event: KeyEvent) -> bool { pub fn matches(&self, event: KeyEvent) -> bool {
self.0.iter().any(|kp| kp.matches(event)) self.0.iter().any(|kp| kp.matches(event))
} }
} }
impl Default for KeyBinding {
fn default() -> Self {
Self::new()
}
}
impl Serialize for KeyBinding { impl Serialize for KeyBinding {
fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> { fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
if self.0.len() == 1 { if self.0.len() == 1 {

View file

@ -1,8 +1,8 @@
mod groups;
mod input; mod input;
mod keys; mod keys;
pub use groups::*; pub use cove_macro::Group;
pub use input::*; pub use input::*;
pub use keys::*; pub use keys::*;

View file

@ -49,10 +49,10 @@ pub fn derive_impl(input: DeriveInput) -> syn::Result<TokenStream> {
#( #enum_variants )* #( #enum_variants )*
} }
impl crate::Group for #struct_ident { impl ::cove_input::Group for #struct_ident {
type Action = #enum_ident; type Action = #enum_ident;
fn action(&self, input: &mut crate::Input) -> Option<Self::Action> { fn action(&self, input: &mut ::cove_input::Input) -> Option<Self::Action> {
match () { match () {
#( #match_cases )* #( #match_cases )*
() => None, () => None,