From f714302f21d64ef5f6dcbee09aa913e8e3b9fbc5 Mon Sep 17 00:00:00 2001 From: unspeaker Date: Wed, 21 May 2025 00:06:36 +0300 Subject: [PATCH] FromDsl -> Namespace --- dsl/src/dsl_provide.rs | 14 +++++++------- input/src/input_dsl.rs | 29 +++++++++++++++-------------- output/src/ops/cond.rs | 6 +++--- output/src/ops/transform.rs | 4 ++-- output/src/view.rs | 4 ++-- proc/src/proc_command.rs | 4 ++-- proc/src/proc_expose.rs | 2 +- proc/src/proc_view.rs | 2 +- 8 files changed, 33 insertions(+), 32 deletions(-) diff --git a/dsl/src/dsl_provide.rs b/dsl/src/dsl_provide.rs index d9896de..cfb6b93 100644 --- a/dsl/src/dsl_provide.rs +++ b/dsl/src/dsl_provide.rs @@ -1,6 +1,6 @@ use crate::*; -pub trait Dsl: Sized { +pub trait Dsl { fn take <'state, 'source: 'state> ( &'state self, token: &mut TokenIter<'source> @@ -18,7 +18,7 @@ pub trait Dsl: Sized { } } } -pub trait FromDsl<'source, State>: Sized { +pub trait Namespace<'source, State>: Sized { fn take_from <'state> (state: &'state State, token: &mut TokenIter<'source>) -> Perhaps; fn take_from_or_fail <'state> ( @@ -26,14 +26,14 @@ pub trait FromDsl<'source, State>: Sized { token: &mut TokenIter<'source>, error: impl Into> ) -> Usually { - if let Some(value) = FromDsl::::take_from(state, token)? { + if let Some(value) = Namespace::::take_from(state, token)? { Ok(value) } else { Result::Err(error.into()) } } } -//impl, T> FromDsl for T { +//impl, T> Namespace for T { //fn take_from <'state, 'source: 'state> (state: &'state State, token: &mut TokenIter<'source>) //-> Perhaps //{ @@ -45,7 +45,7 @@ pub trait FromDsl<'source, State>: Sized { /// specifying two types and providing an expression. #[macro_export] macro_rules! from_dsl { (@a: $T:ty: |$state:ident, $words:ident|$expr:expr) => { - impl<'source, State: Dsl, A> FromDsl<'source, State> for $T { + impl<'source, State: Dsl, A> Namespace<'source, State> for $T { fn take_from <'state> ( $state: &'state State, $words: &mut TokenIter<'source>, @@ -55,7 +55,7 @@ pub trait FromDsl<'source, State>: Sized { } }; (@ab: $T:ty: |$state:ident, $words:ident|$expr:expr) => { - impl<'source, State: Dsl + Dsl, A, B> FromDsl<'source, State> for $T { + impl<'source, State: Dsl + Dsl, A, B> Namespace<'source, State> for $T { fn take_from <'state> ( $state: &'state State, $words: &mut TokenIter<'source>, @@ -65,7 +65,7 @@ pub trait FromDsl<'source, State>: Sized { } }; ($T:ty: |$state:ident:$S:ty, $words:ident|$expr:expr) => { - impl<'source> FromDsl<'source, $S> for $T { + impl<'source> Namespace<'source, $S> for $T { fn take_from <'state> ( $state: &'state $S, $words: &mut TokenIter<'source>, diff --git a/input/src/input_dsl.rs b/input/src/input_dsl.rs index 2856c89..29d0031 100644 --- a/input/src/input_dsl.rs +++ b/input/src/input_dsl.rs @@ -5,13 +5,13 @@ use std::marker::PhantomData; pub trait DslInput: Input { fn matches_dsl (&self, token: &str) -> bool; } /// A pre-configured mapping of input events to commands. -pub trait KeyMap<'source, S, C: FromDsl<'source, S> + Command, I: DslInput> { +pub trait KeyMap<'source, S, C: Namespace<'source, S> + Command, I: DslInput> { /// Try to find a command that matches the current input event. fn keybind_resolve (&self, state: &S, input: &I) -> Perhaps; } /// A [SourceIter] can be a [KeyMap]. -impl<'source, S, C: FromDsl<'source, S> + Command, I: DslInput> +impl<'source, S, C: Namespace<'source, S> + Command, I: DslInput> KeyMap<'source, S, C, I> for SourceIter<'source> { fn keybind_resolve (&self, state: &S, input: &I) -> Perhaps { @@ -24,7 +24,7 @@ for SourceIter<'source> { match exp_iter.next() { Some(Token { value: Value::Sym(binding), .. }) => { if input.matches_dsl(binding) { - if let Some(command) = FromDsl::take_from(state, &mut exp_iter)? { + if let Some(command) = Namespace::take_from(state, &mut exp_iter)? { return Ok(Some(command)) } } @@ -40,7 +40,7 @@ for SourceIter<'source> { } /// A [TokenIter] can be a [KeyMap]. -impl<'source, S, C: FromDsl<'source, S> + Command, I: DslInput> +impl<'source, S, C: Namespace<'source, S> + Command, I: DslInput> KeyMap<'source, S, C, I> for TokenIter<'source> { fn keybind_resolve (&self, state: &S, input: &I) -> Perhaps { @@ -52,7 +52,7 @@ for TokenIter<'source> { match e.next() { Some(Token { value: Value::Sym(binding), .. }) => { if input.matches_dsl(binding) { - if let Some(command) = FromDsl::take_from(state, &mut e)? { + if let Some(command) = Namespace::take_from(state, &mut e)? { return Ok(Some(command)) } } @@ -67,23 +67,24 @@ for TokenIter<'source> { } } -pub type InputLayerCond<'source, S> = BoxUsually + 'source>; +pub type InputCondition<'source, S> = + BoxUsually + Send + Sync + 'source>; /// A collection of pre-configured mappings of input events to commands, /// which may be made available subject to given conditions. pub struct InputMap<'source, S, - C: Command + FromDsl<'source, S>, + C: Command + Namespace<'source, S>, I: DslInput, M: KeyMap<'source, S, C, I> > { __: PhantomData<&'source (S, C, I)>, - pub layers: Vec<(InputLayerCond<'source, S>, M)>, + pub layers: Vec<(InputCondition<'source, S>, M)>, } impl<'source, S, - C: Command + FromDsl<'source, S>, + C: Command + Namespace<'source, S>, I: DslInput, M: KeyMap<'source, S, C, I> > Default for InputMap<'source, S, C, I, M>{ @@ -94,7 +95,7 @@ impl<'source, impl<'source, S, - C: Command + FromDsl<'source, S>, + C: Command + Namespace<'source, S>, I: DslInput, M: KeyMap<'source, S, C, I> > InputMap<'source, S, C, I, M> { @@ -109,11 +110,11 @@ impl<'source, self.add_layer_if(Box::new(|_|Ok(true)), keymap); self } - pub fn layer_if (mut self, condition: InputLayerCond<'source, S>, keymap: M) -> Self { + pub fn layer_if (mut self, condition: InputCondition<'source, S>, keymap: M) -> Self { self.add_layer_if(condition, keymap); self } - pub fn add_layer_if (&mut self, condition: InputLayerCond<'source, S>, keymap: M) -> &mut Self { + pub fn add_layer_if (&mut self, condition: InputCondition<'source, S>, keymap: M) -> &mut Self { self.layers.push((Box::new(condition), keymap)); self } @@ -121,7 +122,7 @@ impl<'source, impl<'source, S, - C: Command + FromDsl<'source, S>, + C: Command + Namespace<'source, S>, I: DslInput, M: KeyMap<'source, S, C, I> > std::fmt::Debug for InputMap<'source, S, C, I, M> { @@ -133,7 +134,7 @@ impl<'source, /// An [InputMap] can be a [KeyMap]. impl<'source, S, - C: Command + FromDsl<'source, S>, + C: Command + Namespace<'source, S>, I: DslInput, M: KeyMap<'source, S, C, I> > KeyMap<'source, S, C, I> for InputMap<'source, S, C, I, M> { diff --git a/output/src/ops/cond.rs b/output/src/ops/cond.rs index e0dde77..67e5e16 100644 --- a/output/src/ops/cond.rs +++ b/output/src/ops/cond.rs @@ -14,11 +14,11 @@ pub struct Either(pub bool, pub A, pub B); impl Either { /// Create a ternary condition. pub const fn new (c: bool, a: A, b: B) -> Self { - Self(c, a, b) + Self(c, a, b) } } #[cfg(feature = "dsl")] -impl<'source, A, T: Dsl + Dsl> FromDsl<'source, T> for When { +impl<'source, A, T: Dsl + Dsl> Namespace<'source, T> for When { fn take_from <'state> ( state: &'state T, token: &mut TokenIter<'source> @@ -56,7 +56,7 @@ impl> Content for When { } } #[cfg(feature = "dsl")] -impl<'source, A, B, T: Dsl + Dsl + Dsl> FromDsl<'source, T> for Either { +impl<'source, A, B, T: Dsl + Dsl + Dsl> Namespace<'source, T> for Either { fn take_from <'state> ( state: &'state T, token: &mut TokenIter<'source> diff --git a/output/src/ops/transform.rs b/output/src/ops/transform.rs index 808f56b..e5a86c4 100644 --- a/output/src/ops/transform.rs +++ b/output/src/ops/transform.rs @@ -33,7 +33,7 @@ macro_rules! transform_xy { #[inline] pub const fn xy (item: A) -> Self { Self::XY(item) } } #[cfg(feature = "dsl")] - impl<'source, A, T: Dsl> FromDsl<'source, T> for $Enum { + impl<'source, A, T: Dsl> Namespace<'source, T> for $Enum { fn take_from <'state> (state: &'state T, iter: &mut TokenIter<'source>) -> Perhaps { if let Some(Token { value: Value::Key(k), .. }) = iter.peek() { let mut base = iter.clone(); @@ -77,7 +77,7 @@ macro_rules! transform_xy_unit { #[inline] pub const fn xy (x: U, y: U, item: A) -> Self { Self::XY(x, y, item) } } #[cfg(feature = "dsl")] - impl<'source, A, U: Coordinate, T: Dsl + Dsl> FromDsl<'source, T> for $Enum { + impl<'source, A, U: Coordinate, T: Dsl + Dsl> Namespace<'source, T> for $Enum { fn take_from <'state> (state: &'state T, iter: &mut TokenIter<'source>) -> Perhaps { Ok(if let Some(Token { value: Value::Key($x|$y|$xy), .. }) = iter.peek() { let mut base = iter.clone(); diff --git a/output/src/view.rs b/output/src/view.rs index 2ae266e..ae252d9 100644 --- a/output/src/view.rs +++ b/output/src/view.rs @@ -13,7 +13,7 @@ use crate::*; //// Provides components to the view. //#[cfg(feature = "dsl")] //pub trait ViewContext<'state, E: Output + 'state>: - //FromDsl + FromDsl + FromDsl + Send + Sync + //Namespace + Namespace + Namespace + Send + Sync //{ //fn get_content_sym <'source: 'state> (&'state self, iter: &mut TokenIter<'source>) //-> Perhaps>; @@ -39,7 +39,7 @@ use crate::*; //} //#[cfg(feature = "dsl")] -//impl<'context, O: Output + 'context, T: ViewContext<'context, O>> FromDsl for RenderBox<'context, O> { +//impl<'context, O: Output + 'context, T: ViewContext<'context, O>> Namespace for RenderBox<'context, O> { //fn take_from <'state, 'source: 'state> (state: &'state T, token: &mut TokenIter<'source>) //-> Perhaps> //{ diff --git a/proc/src/proc_command.rs b/proc/src/proc_command.rs index a8ca8c9..e518462 100644 --- a/proc/src/proc_command.rs +++ b/proc/src/proc_command.rs @@ -84,7 +84,7 @@ impl ToTokens for CommandDef { let mut out = TokenStream2::new(); for (arg, ty) in arm.args() { write_quote_to(&mut out, quote! { - #arg: FromDsl::take_from_or_fail(self, words)?, + #arg: Namespace::take_from_or_fail(self, words)?, }); } out @@ -149,7 +149,7 @@ impl ToTokens for CommandDef { } } /// Generated by [tengri_proc::command]. - impl<'source> ::tengri::dsl::FromDsl<'source, #state> for #command_enum { + impl<'source> ::tengri::dsl::Namespace<'source, #state> for #command_enum { fn take_from <'state> ( state: &'state #state, words: &mut ::tengri::dsl::TokenIter<'source> diff --git a/proc/src/proc_expose.rs b/proc/src/proc_expose.rs index f7b934e..f3940e1 100644 --- a/proc/src/proc_expose.rs +++ b/proc/src/proc_expose.rs @@ -85,7 +85,7 @@ impl ToTokens for ExposeImpl { let values = variants.iter().map(ExposeArm::from); write_quote_to(out, quote! { /// Generated by [tengri_proc::expose]. - impl<'source> ::tengri::dsl::FromDsl<'source, #state> for #t { + impl<'source> ::tengri::dsl::Namespace<'source, #state> for #t { fn take_from <'state> ( state: &'state #state, words: &mut ::tengri::dsl::TokenIter<'source> diff --git a/proc/src/proc_view.rs b/proc/src/proc_view.rs index 70511eb..4e6eeb2 100644 --- a/proc/src/proc_view.rs +++ b/proc/src/proc_view.rs @@ -70,7 +70,7 @@ impl ToTokens for ViewDef { /// Gives [#view] the ability to construct the [Render]able /// which might corresponds to a given [TokenStream], /// while taking [#view]'s state into consideration. - impl<'source> ::tengri::dsl::FromDsl<'source, #view> for Box + 'source> { + impl<'source> ::tengri::dsl::Namespace<'source, #view> for Box + 'source> { fn take_from <'state> ( state: &'state #view, words: &mut ::tengri::dsl::TokenIter<'source>,