From 64a7e7f5182761882316f8515f028c202e49d4ad Mon Sep 17 00:00:00 2001 From: Joscha Date: Thu, 27 Apr 2023 21:37:48 +0200 Subject: [PATCH] Simplify KeyGroup The trait will only be used for documenting the key bindings in the F1 menu from now on. The InputEvent will be directly match-eable against KeyBinding-s, which should suffice for input event handling. --- cove-input/src/lib.rs | 4 +--- cove-macro/src/key_group.rs | 42 +++++++++++-------------------------- 2 files changed, 13 insertions(+), 33 deletions(-) diff --git a/cove-input/src/lib.rs b/cove-input/src/lib.rs index 901f48e..3221d8c 100644 --- a/cove-input/src/lib.rs +++ b/cove-input/src/lib.rs @@ -8,7 +8,5 @@ pub use keys::*; /// A group of related key bindings. pub trait KeyGroup { - type Event; - - fn match_input_event(&self, event: &mut InputEvent) -> Option; + fn bindings(&self) -> Vec<(&KeyBinding, &'static str)>; } diff --git a/cove-macro/src/key_group.rs b/cove-macro/src/key_group.rs index 3876956..757e3f0 100644 --- a/cove-macro/src/key_group.rs +++ b/cove-macro/src/key_group.rs @@ -1,6 +1,5 @@ -use case::CaseExt; use proc_macro2::TokenStream; -use quote::{format_ident, quote}; +use quote::quote; use syn::spanned::Spanned; use syn::{Data, DeriveInput}; @@ -17,46 +16,29 @@ fn decapitalize(s: &str) -> String { pub fn derive_impl(input: DeriveInput) -> syn::Result { let Data::Struct(data) = input.data else { - return util::bail(input.span(), "Must be a struct"); + return util::bail(input.span(), "must be a struct"); }; - let struct_ident = input.ident; - let enum_ident = format_ident!("{}Event", struct_ident); - - let mut enum_variants = vec![]; - let mut match_cases = vec![]; + let mut bindings = vec![]; for field in &data.fields { if let Some(field_ident) = &field.ident { let docstring = util::docstring(field)?; - let variant_ident = format_ident!("{}", field_ident.to_string().to_camel()); - - enum_variants.push(quote! { - #[doc = #docstring] - #variant_ident, - }); - let description = decapitalize(&docstring); let description = description.strip_suffix('.').unwrap_or(&description); - match_cases.push(quote!{ - () if event.matches_key_binding(&self.#field_ident, #description) => Some(Self::Event::#variant_ident), + + bindings.push(quote! { + (&self.#field_ident, #description) }); } } + let ident = input.ident; Ok(quote! { - #[derive(Debug, Clone, Copy, PartialEq, Eq)] - pub enum #enum_ident { - #( #enum_variants )* - } - - impl ::cove_input::KeyGroup for #struct_ident { - type Event = #enum_ident; - - fn match_input_event(&self, event: &mut ::cove_input::InputEvent) -> Option { - match () { - #( #match_cases )* - () => None, - } + impl ::cove_input::KeyGroup for #ident { + fn bindings(&self) -> Vec<(&::cove_input::KeyBinding, &'static str)> { + vec![ + #( #bindings, )* + ] } } })