From 22d63eed9c9cb5bed5016d851f90773e0f60280d Mon Sep 17 00:00:00 2001 From: unspeaker Date: Fri, 9 May 2025 01:38:18 +0300 Subject: [PATCH] input, dsl: cleanup --- dsl/src/dsl_iter.rs | 2 +- input/src/_input.rs | 36 ----------------- input/src/_input_event_map.rs | 73 ----------------------------------- input/src/input_macros.rs | 2 - 4 files changed, 1 insertion(+), 112 deletions(-) delete mode 100644 input/src/_input.rs delete mode 100644 input/src/_input_event_map.rs diff --git a/dsl/src/dsl_iter.rs b/dsl/src/dsl_iter.rs index 269bc55..acdbe4b 100644 --- a/dsl/src/dsl_iter.rs +++ b/dsl/src/dsl_iter.rs @@ -105,7 +105,7 @@ pub const fn peek_src <'a> (source: &'a str) -> Option> { }), _ => token.error(Unexpected(c)) }, - Str(s) => match c { + Str(_) => match c { '"' => return Some(token), _ => token.grow_str(), }, diff --git a/input/src/_input.rs b/input/src/_input.rs deleted file mode 100644 index 24dd927..0000000 --- a/input/src/_input.rs +++ /dev/null @@ -1,36 +0,0 @@ -use crate::*; -use std::time::Duration; -use std::thread::JoinHandle; - -/// Event source -pub trait Input: Send + Sync + Sized { - /// Type of input event - type Event; - /// Result of handling input - type Handled; // TODO: make this an Option> containing the undo - /// Currently handled event - fn event (&self) -> &Self::Event; - /// Whether component should exit - fn is_done (&self) -> bool; - /// Mark component as done - fn done (&self); -} - -/// Input thread entrypoint. -pub trait InputRun { - fn run_input (engine: T, state: Self, timer: Duration) -> JoinHandle<()>; -} - -/// Handle input through a mutable reference. -pub trait Handle: Send + Sync { - fn handle (&mut self, _input: &E) -> Perhaps { - Ok(None) - } -} - -/// Handle input through an immutable reference (e.g. [Arc] or [Arc]) -pub trait HandleRef: Send + Sync { - fn handle (&self, _input: &E) -> Perhaps { - Ok(None) - } -} diff --git a/input/src/_input_event_map.rs b/input/src/_input_event_map.rs deleted file mode 100644 index fc9644d..0000000 --- a/input/src/_input_event_map.rs +++ /dev/null @@ -1,73 +0,0 @@ -use crate::*; - -pub struct EventMap<'a, S, I: PartialEq, C> { - pub bindings: &'a [(I, &'a dyn Fn(&S) -> Option)], - pub fallback: Option<&'a dyn Fn(&S, &I) -> Option> -} - -impl<'a, S, I: PartialEq, C> EventMap<'a, S, I, C> { - pub fn handle (&self, state: &S, input: &I) -> Option { - for (binding, handler) in self.bindings.iter() { - if input == binding { - return handler(state) - } - } - if let Some(fallback) = self.fallback { - fallback(state, input) - } else { - None - } - } -} - -#[macro_export] macro_rules! keymap { - ( - $(<$lt:lifetime>)? $KEYS:ident = |$state:ident: $State:ty, $input:ident: $Input:ty| $Command:ty - { $($key:expr => $handler:expr),* $(,)? } $(,)? - ) => { - pub const $KEYS: EventMap<'static, $State, $Input, $Command> = EventMap { - fallback: None, - bindings: &[ $(($key, &|$state|Some($handler)),)* ] - }; - input_to_command!($(<$lt>)? $Command: |$state: $State, input: $Input|$KEYS.handle($state, input)?); - }; - ( - $(<$lt:lifetime>)? $KEYS:ident = |$state:ident: $State:ty, $input:ident: $Input:ty| $Command:ty - { $($key:expr => $handler:expr),* $(,)? }, $default:expr - ) => { - pub const $KEYS: EventMap<'static, $State, $Input, $Command> = EventMap { - fallback: Some(&|$state, $input|Some($default)), - bindings: &[ $(($key, &|$state|Some($handler)),)* ] - }; - input_to_command!($(<$lt>)? $Command: |$state: $State, input: $Input|$KEYS.handle($state, input)?); - }; -} - -#[macro_export] macro_rules! input_to_command { - (<$($l:lifetime),+> $Command:ty: |$state:ident:$State:ty, $input:ident:$Input:ty| $handler:expr) => { - impl<$($l),+> InputToCommand<$Input, $State> for $Command { - fn input_to_command ($state: &$State, $input: &$Input) -> Option { - Some($handler) - } - } - }; - ($Command:ty: |$state:ident:$State:ty, $input:ident:$Input:ty| $handler:expr) => { - impl InputToCommand<$Input, $State> for $Command { - fn input_to_command ($state: &$State, $input: &$Input) -> Option { - Some($handler) - } - } - } -} - -pub trait InputToCommand: Command + Sized { - fn input_to_command (state: &S, input: &I) -> Option; - fn execute_with_state (state: &mut S, input: &I) -> Perhaps { - Ok(if let Some(command) = Self::input_to_command(state, input) { - let _undo = command.execute(state)?; - Some(true) - } else { - None - }) - } -} diff --git a/input/src/input_macros.rs b/input/src/input_macros.rs index 689c94e..ae5758c 100644 --- a/input/src/input_macros.rs +++ b/input/src/input_macros.rs @@ -1,5 +1,3 @@ -use crate::*; - /** Implement `Command` for given `State` and `handler` */ #[macro_export] macro_rules! command { ($(<$($l:lifetime),+>)?|$self:ident:$Command:ty,$state:ident:$State:ty|$handler:expr) => {