From e1c3a463b25e59b2825d2c5fb37cab97934e0d48 Mon Sep 17 00:00:00 2001 From: Joscha Date: Thu, 27 Apr 2023 15:31:49 +0200 Subject: [PATCH] Move key binding groups to config crate --- Cargo.lock | 1 + cove-config/Cargo.toml | 1 + .../src/groups.rs => cove-config/src/keys.rs | 4 +--- cove-config/src/lib.rs | 2 ++ cove-input/src/keys.rs | 24 +++++++++++++++++++ cove-input/src/lib.rs | 4 ++-- cove-macro/src/group.rs | 4 ++-- 7 files changed, 33 insertions(+), 7 deletions(-) rename cove-input/src/groups.rs => cove-config/src/keys.rs (97%) diff --git a/Cargo.lock b/Cargo.lock index a9e3df1..346690d 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -281,6 +281,7 @@ dependencies = [ name = "cove-config" version = "0.6.1" dependencies = [ + "cove-input", "cove-macro", "serde", "toml", diff --git a/cove-config/Cargo.toml b/cove-config/Cargo.toml index 65c8d55..252e228 100644 --- a/cove-config/Cargo.toml +++ b/cove-config/Cargo.toml @@ -4,6 +4,7 @@ version = { workspace = true } edition = { workspace = true } [dependencies] +cove-input = { path = "../cove-input" } cove-macro = { path = "../cove-macro" } serde = { workspace = true } diff --git a/cove-input/src/groups.rs b/cove-config/src/keys.rs similarity index 97% rename from cove-input/src/groups.rs rename to cove-config/src/keys.rs index bf13abe..1823335 100644 --- a/cove-input/src/groups.rs +++ b/cove-config/src/keys.rs @@ -1,6 +1,4 @@ -use cove_macro::Group; - -use crate::KeyBinding; +use cove_input::{Group, KeyBinding}; #[derive(Debug, Group)] pub struct General { diff --git a/cove-config/src/lib.rs b/cove-config/src/lib.rs index 9b81c28..e90f05a 100644 --- a/cove-config/src/lib.rs +++ b/cove-config/src/lib.rs @@ -11,6 +11,7 @@ pub mod doc; mod euph; +mod keys; use std::fs; use std::path::{Path, PathBuf}; @@ -19,6 +20,7 @@ use doc::Document; use serde::Deserialize; pub use crate::euph::*; +pub use crate::keys::*; #[derive(Debug, Default, Deserialize, Document)] pub struct Config { diff --git a/cove-input/src/keys.rs b/cove-input/src/keys.rs index ddedcd9..da7de81 100644 --- a/cove-input/src/keys.rs +++ b/cove-input/src/keys.rs @@ -173,11 +173,35 @@ impl<'de> Deserialize<'de> for KeyPress { pub struct KeyBinding(Vec); impl KeyBinding { + pub fn new() -> Self { + Self(vec![]) + } + + pub fn with_key(self, key: &str) -> Result { + self.with_keys([key]) + } + + pub fn with_keys<'a, I>(mut self, keys: I) -> Result + where + I: IntoIterator, + { + for key in keys { + self.0.push(key.parse()?); + } + Ok(self) + } + pub fn matches(&self, event: KeyEvent) -> bool { self.0.iter().any(|kp| kp.matches(event)) } } +impl Default for KeyBinding { + fn default() -> Self { + Self::new() + } +} + impl Serialize for KeyBinding { fn serialize(&self, serializer: S) -> Result { if self.0.len() == 1 { diff --git a/cove-input/src/lib.rs b/cove-input/src/lib.rs index 1ee29d4..52dc17d 100644 --- a/cove-input/src/lib.rs +++ b/cove-input/src/lib.rs @@ -1,8 +1,8 @@ -mod groups; mod input; mod keys; -pub use groups::*; +pub use cove_macro::Group; + pub use input::*; pub use keys::*; diff --git a/cove-macro/src/group.rs b/cove-macro/src/group.rs index 3e0995c..f273ad8 100644 --- a/cove-macro/src/group.rs +++ b/cove-macro/src/group.rs @@ -49,10 +49,10 @@ pub fn derive_impl(input: DeriveInput) -> syn::Result { #( #enum_variants )* } - impl crate::Group for #struct_ident { + impl ::cove_input::Group for #struct_ident { type Action = #enum_ident; - fn action(&self, input: &mut crate::Input) -> Option { + fn action(&self, input: &mut ::cove_input::Input) -> Option { match () { #( #match_cases )* () => None,