From 9ccd7e5c69f6f8aa5ab43f22d5953d56427d4f14 Mon Sep 17 00:00:00 2001 From: unspeaker Date: Sun, 3 Aug 2025 19:36:01 +0300 Subject: [PATCH] dsl: macro dsl_for_each -> method each --- dsl/src/dsl.rs | 22 ++++++++-------------- input/src/input.rs | 4 ---- 2 files changed, 8 insertions(+), 18 deletions(-) diff --git a/dsl/src/dsl.rs b/dsl/src/dsl.rs index c43e2f1..b2bdf99 100644 --- a/dsl/src/dsl.rs +++ b/dsl/src/dsl.rs @@ -28,6 +28,14 @@ impl DslExp for D {} pub trait DslExp: Dsl { fn exp (&self) -> DslPerhaps<&str> {ok_flat(self.src()?.map(exp_peek_inner_only))} fn head (&self) -> DslPerhaps<&str> {ok_flat(self.src()?.map(peek))} fn tail (&self) -> DslPerhaps<&str> {ok_flat(self.src()?.map(peek_tail(self.head())))} + fn each (&self, mut cb: impl FnMut(&str)->Usually<()>) -> Usually<()> { + Ok(if let Some(head) = self.head()? { + cb(head)?; + if let Some(tail) = self.tail()? { + tail.each(cb)?; + } + }) + } } impl DslText for D {} pub trait DslText: Dsl { fn text (&self) -> DslPerhaps<&str> {ok_flat(self.src()?.map(text_peek_only))} @@ -55,20 +63,6 @@ pub enum DslError { #[error("parse failed: error #{0}")] Code(u8), #[error("end reached")] End } -#[macro_export] macro_rules! dsl_for_each (($dsl:expr => |$next:ident|$body:expr)=>{ - let mut dsl: Arc = $dsl.src().into(); - let mut $next: Option> = dsl.next()?.map(Into::into); - let mut rest: Option> = dsl.rest()?.map(Into::into); - loop { - if let Some($next) = $next { $body } else { break }; - if let Some(next) = rest { - $next = next.next()?.map(Into::into); - rest = next.rest()?.map(Into::into); - } else { - break - } - } -}); fn ok_flat (x: Option>) -> DslPerhaps { Ok(x.transpose()?.flatten()) } fn peek_tail <'a> (head: DslPerhaps<&'a str>) -> impl Fn(&'a str)->DslPerhaps<&'a str> { move|src|match head { diff --git a/input/src/input.rs b/input/src/input.rs index 7b451ee..48e692f 100644 --- a/input/src/input.rs +++ b/input/src/input.rs @@ -1,5 +1,4 @@ use crate::*; - /// Event source pub trait Input: Sized { /// Type of input event @@ -13,13 +12,11 @@ pub trait Input: Sized { /// Mark component as done fn done (&self); } - flex_trait_mut!(Handle { fn handle (&mut self, _input: &E) -> Perhaps { Ok(None) } }); - pub trait Command: Send + Sync + Sized { fn execute (self, state: &mut S) -> Perhaps; fn delegate (self, state: &mut S, wrap: impl Fn(Self)->T) -> Perhaps @@ -28,7 +25,6 @@ pub trait Command: Send + Sync + Sized { Ok(self.execute(state)?.map(wrap)) } } - impl> Command for Option { fn execute (self, _: &mut S) -> Perhaps { Ok(None)