diff --git a/dsl/src/dsl_provide.rs b/dsl/src/dsl_provide.rs index a0130f7..66e18d8 100644 --- a/dsl/src/dsl_provide.rs +++ b/dsl/src/dsl_provide.rs @@ -1,64 +1,52 @@ use crate::*; -// maybe their names should be switched around? - -/// [Take]s instances of [Type] given [TokenIter]. -pub trait Give { - /// Implement this to be able to [Give] [Type] from the [TokenIter]. - fn give <'source> (&self, words: &mut TokenIter<'source>) -> Perhaps; - /// Return custom error on [None]. - fn give_or_fail <'source, E: Into>, F: Fn()->E> ( +pub trait Receive { + fn receive <'source> (&self, words: &mut TokenIter<'source>) -> Perhaps; + fn receive_or_fail <'source, E: Into>, F: Fn()->E> ( &self, words: &mut TokenIter<'source>, error: F ) -> Usually { - if let Some(value) = Give::::give(self, words)? { + if let Some(value) = Receive::::receive(self, words)? { Ok(value) } else { - Result::Err(format!("give: {}: {:?}", error().into(), words.peek().map(|x|x.value)).into()) + Result::Err(format!("receive: {}: {:?}", error().into(), words.peek().map(|x|x.value)).into()) } } } -impl<'n, T: Take<'n, U>, U> Give for U { - fn give <'source> (&self, words: &mut TokenIter<'source>) -> Perhaps { - T::take(self, words) - } -} - -/// [Give]s instances of [Self] given [TokenIter]. -pub trait Take<'n, State>: Sized + 'n { - fn take <'source> (state: &State, words: &mut TokenIter<'source>) -> Perhaps; - /// Return custom error on [None]. - fn take_or_fail <'source, E: Into>, F: Fn()->E> ( +pub trait Provide<'n, State>: Sized + 'n { + fn provide <'source> (state: &State, words: &mut TokenIter<'source>) + -> Perhaps; + fn provide_or_fail <'source, E: Into>, F: Fn()->E> ( state: &State, words: &mut TokenIter<'source>, error: F ) -> Usually { - if let Some(value) = Take::::take(state, words)? { + if let Some(value) = Provide::::provide(state, words)? { Ok(value) } else { - Result::Err(format!("take: {}: {:?}", error().into(), words.peek().map(|x|x.value)).into()) + Result::Err(format!("provide: {}: {:?}", error().into(), words.peek().map(|x|x.value)).into()) } } } -/// Implement the [Give] trait, which boils down to +/// Implement the [Receive] trait, which boils down to /// specifying two types and providing an expression. #[macro_export] macro_rules! from_dsl { (@a: $T:ty: |$state:ident, $words:ident|$expr:expr) => { - impl<'n, State, A: Take<'n, State>> Take<'n, State> for $T { - fn take <'source> ($state: &State, $words: &mut TokenIter<'source>) -> Perhaps<$T> { + impl<'n, State, A: Provide<'n, State>> Provide<'n, State> for $T { + fn provide <'source> ($state: &State, $words: &mut TokenIter<'source>) -> Perhaps<$T> { $expr } } }; (@ab: $T:ty: |$state:ident, $words:ident|$expr:expr) => { - impl<'n, State, A: Take<'n, State>, B: Take<'n, State>> Take<'n, State> for $T { - fn take <'source> ($state: &State, $words: &mut TokenIter<'source>) -> Perhaps<$T> { + impl<'n, State, A: Provide<'n, State>, B: Provide<'n, State>> Provide<'n, State> for $T { + fn provide <'source> ($state: &State, $words: &mut TokenIter<'source>) -> Perhaps<$T> { $expr } } }; ($T:ty: |$state:ident:$S:ty, $words:ident|$expr:expr) => { - impl<'n> Take<'n, $S> for $T { - fn take <'source> ($state: &$S, $words: &mut TokenIter<'source>) -> Perhaps<$T> { + impl<'n> Provide<'n, $S> for $T { + fn provide <'source> ($state: &$S, $words: &mut TokenIter<'source>) -> Perhaps<$T> { $expr } } @@ -67,16 +55,16 @@ pub trait Take<'n, State>: Sized + 'n { // auto impl graveyard: -//impl<'n, State: Give, Type: 'n> Take<'n, State> for Type { - //fn take <'source> (state: &State, words: &mut TokenIter<'source>) +//impl<'n, State: Receive, Type: 'n> Provide<'n, State> for Type { + //fn provide <'source> (state: &State, words: &mut TokenIter<'source>) //-> Perhaps //{ //state.take(words) //} //} -//impl<'n, Type: Take<'n, State>, State> Give for State { +//impl<'n, Type: Provide<'n, State>, State> Receive for State { //fn take <'source> (&self, words: &mut TokenIter<'source>) -> Perhaps { - //Type::take(self, words) + //Type::provide(self, words) //} //} diff --git a/input/src/input_dsl.rs b/input/src/input_dsl.rs index 79334f4..5df10cb 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<'k, S, C: Take<'k, S> + Command, I: DslInput> { +pub trait KeyMap<'k, S, C: Provide<'k, 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<'k, S, C: Take<'k, S> + Command, I: DslInput> KeyMap<'k, S, C, I> for SourceIter<'k> { +impl<'k, S, C: Provide<'k, S> + Command, I: DslInput> KeyMap<'k, S, C, I> for SourceIter<'k> { fn keybind_resolve (&self, state: &S, input: &I) -> Perhaps { let mut iter = self.clone(); while let Some((token, rest)) = iter.next() { @@ -22,7 +22,7 @@ impl<'k, S, C: Take<'k, S> + Command, I: DslInput> KeyMap<'k, S, C, I> for So match exp_iter.next() { Some(Token { value: Value::Sym(binding), .. }) => { if input.matches_dsl(binding) { - if let Some(command) = Take::take(state, &mut exp_iter)? { + if let Some(command) = Provide::provide(state, &mut exp_iter)? { return Ok(Some(command)) } } @@ -38,7 +38,7 @@ impl<'k, S, C: Take<'k, S> + Command, I: DslInput> KeyMap<'k, S, C, I> for So } /// A [TokenIter] can be a [KeyMap]. -impl<'k, S, C: Take<'k, S> + Command, I: DslInput> KeyMap<'k, S, C, I> for TokenIter<'k> { +impl<'k, S, C: Provide<'k, S> + Command, I: DslInput> KeyMap<'k, S, C, I> for TokenIter<'k> { fn keybind_resolve (&self, state: &S, input: &I) -> Perhaps { let mut iter = self.clone(); while let Some(next) = iter.next() { @@ -48,7 +48,7 @@ impl<'k, S, C: Take<'k, S> + Command, I: DslInput> KeyMap<'k, S, C, I> for To match e.next() { Some(Token { value: Value::Sym(binding), .. }) => { if input.matches_dsl(binding) { - if let Some(command) = Take::take(state, &mut e)? { + if let Some(command) = Provide::provide(state, &mut e)? { return Ok(Some(command)) } } @@ -69,7 +69,7 @@ pub type InputCondition<'k, S> = BoxUsually + Send + Sync + ' /// which may be made available subject to given conditions. pub struct InputMap<'k, S, - C: Command + Take<'k, S>, + C: Command + Provide<'k, S>, I: DslInput, M: KeyMap<'k, S, C, I> > { @@ -79,7 +79,7 @@ pub struct InputMap<'k, impl<'k, S, - C: Command + Take<'k, S>, + C: Command + Provide<'k, S>, I: DslInput, M: KeyMap<'k, S, C, I> > Default for InputMap<'k, S, C, I, M>{ @@ -88,7 +88,7 @@ impl<'k, } } -impl<'k, S, C: Command + Take<'k, S>, I: DslInput, M: KeyMap<'k, S, C, I>> +impl<'k, S, C: Command + Provide<'k, S>, I: DslInput, M: KeyMap<'k, S, C, I>> InputMap<'k, S, C, I, M> { pub fn new (keymap: M) -> Self { Self::default().layer(keymap) @@ -111,7 +111,7 @@ InputMap<'k, S, C, I, M> { } } -impl<'k, S, C: Command + Take<'k, S>, I: DslInput, M: KeyMap<'k, S, C, I>> +impl<'k, S, C: Command + Provide<'k, S>, I: DslInput, M: KeyMap<'k, S, C, I>> std::fmt::Debug for InputMap<'k, S, C, I, M> { fn fmt (&self, f: &mut std::fmt::Formatter) -> Result<(), std::fmt::Error> { write!(f, "[InputMap: {} layer(s)]", self.layers.len()) @@ -121,7 +121,7 @@ std::fmt::Debug for InputMap<'k, S, C, I, M> { /// An [InputMap] can be a [KeyMap]. impl<'k, S, - C: Command + Take<'k, S>, + C: Command + Provide<'k, S>, I: DslInput, M: KeyMap<'k, S, C, I> > KeyMap<'k, S, C, I> for InputMap<'k, S, C, I, M> { diff --git a/output/src/ops/align.rs b/output/src/ops/align.rs index 8031e0e..ee168ea 100644 --- a/output/src/ops/align.rs +++ b/output/src/ops/align.rs @@ -36,15 +36,15 @@ pub enum Alignment { #[default] Center, X, Y, NW, N, NE, E, SE, S, SW, W } pub struct Align(Alignment, A); #[cfg(feature = "dsl")] -impl<'n, State: Give, A: 'n> Take<'n, State> for Align { - fn take <'source> (state: &State, words: &mut TokenIter<'source>) -> Perhaps { +impl<'n, State: Receive, A: 'n> Provide<'n, State> for Align { + fn provide <'source> (state: &State, words: &mut TokenIter<'source>) -> Perhaps { if let Some(Token { value: Value::Key(key), .. }) = words.peek() { match key { "align/c"|"align/x"|"align/y"| "align/n"|"align/s"|"align/e"|"align/w"| "align/nw"|"align/sw"|"align/ne"|"align/se" => { let _ = words.next().unwrap(); - let content = state.give_or_fail(&mut words.clone(), ||"expected content")?; + let content = state.receive_or_fail(&mut words.clone(), ||"expected content")?; return Ok(Some(match key { "align/c" => Self::c(content), "align/x" => Self::x(content), diff --git a/output/src/ops/bsp.rs b/output/src/ops/bsp.rs index 9ecd6d7..cb38197 100644 --- a/output/src/ops/bsp.rs +++ b/output/src/ops/bsp.rs @@ -21,16 +21,16 @@ impl, B: Content> Content for Bsp { } } #[cfg(feature = "dsl")] -impl<'n, State: Give + Give, A: 'n, B: 'n> Take<'n, State> for Bsp { - fn take <'source> (state: &State, words: &mut TokenIter<'source>) -> Perhaps { +impl<'n, State: Receive + Receive, A: 'n, B: 'n> Provide<'n, State> for Bsp { + fn provide <'source> (state: &State, words: &mut TokenIter<'source>) -> Perhaps { Ok(if let Some(Token { value: Value::Key("bsp/n"|"bsp/s"|"bsp/e"|"bsp/w"|"bsp/a"|"bsp/b"), .. }) = words.peek() { if let Value::Key(key) = words.next().unwrap().value() { let base = words.clone(); - let a: A = state.give_or_fail(words, ||"bsp: expected content 1")?; - let b: B = state.give_or_fail(words, ||"bsp: expected content 2")?; + let a: A = state.receive_or_fail(words, ||"bsp: expected content 1")?; + let b: B = state.receive_or_fail(words, ||"bsp: expected content 2")?; return Ok(Some(match key { "bsp/n" => Self::n(a, b), "bsp/s" => Self::s(a, b), diff --git a/output/src/ops/either.rs b/output/src/ops/either.rs index a35d555..d3813c1 100644 --- a/output/src/ops/either.rs +++ b/output/src/ops/either.rs @@ -9,15 +9,15 @@ impl Either { } } #[cfg(feature = "dsl")] -impl<'n, State: Give + Give + Give, A: 'n, B: 'n> Take<'n, State> for Either { - fn take <'source> (state: &State, token: &mut TokenIter<'source>) -> Perhaps { +impl<'n, State: Receive + Receive + Receive, A: 'n, B: 'n> Provide<'n, State> for Either { + fn provide <'source> (state: &State, token: &mut TokenIter<'source>) -> Perhaps { if let Some(Token { value: Value::Key("either"), .. }) = token.peek() { let base = token.clone(); let _ = token.next().unwrap(); return Ok(Some(Self( - state.give_or_fail(token, ||"either: no condition")?, - state.give_or_fail(token, ||"either: no content 1")?, - state.give_or_fail(token, ||"either: no content 2")?, + state.receive_or_fail(token, ||"either: no condition")?, + state.receive_or_fail(token, ||"either: no content 1")?, + state.receive_or_fail(token, ||"either: no content 2")?, ))) } Ok(None) diff --git a/output/src/ops/transform.rs b/output/src/ops/transform.rs index 5e0ec04..2715bde 100644 --- a/output/src/ops/transform.rs +++ b/output/src/ops/transform.rs @@ -1,7 +1,7 @@ //! [Content] items that modify the inherent //! dimensions of their inner [Render]ables. //! -//! Transform may also react to the [Area] taked. +//! Transform may also react to the [Area] provided. //! ``` //! use ::tengri::{output::*, tui::*}; //! let area: [u16;4] = [10, 10, 20, 20]; @@ -33,19 +33,19 @@ macro_rules! transform_xy { #[inline] pub const fn xy (item: A) -> Self { Self::XY(item) } } #[cfg(feature = "dsl")] - impl<'n, A: 'n, T: Give> Take<'n, T> for $Enum { - fn take <'source> (state: &T, token: &mut TokenIter<'source>) + impl<'n, A: 'n, T: Receive> Provide<'n, T> for $Enum { + fn provide <'source> (state: &T, token: &mut TokenIter<'source>) -> Perhaps { if let Some(Token { value: Value::Key(k), .. }) = token.peek() { let mut base = token.clone(); return Ok(Some(match token.next() { Some(Token{value:Value::Key($x),..}) => - Self::x(state.give_or_fail(token, ||"x: no content")?), + Self::x(state.receive_or_fail(token, ||"x: no content")?), Some(Token{value:Value::Key($y),..}) => - Self::y(state.give_or_fail(token, ||"y: no content")?), + Self::y(state.receive_or_fail(token, ||"y: no content")?), Some(Token{value:Value::Key($xy),..}) => - Self::xy(state.give_or_fail(token, ||"xy: no content")?), + Self::xy(state.receive_or_fail(token, ||"xy: no content")?), _ => unreachable!() })) } @@ -79,25 +79,25 @@ 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<'n, A: 'n, U: Coordinate + 'n, T: Give + Give> Take<'n, T> for $Enum { - fn take <'source> ( + impl<'n, A: 'n, U: Coordinate + 'n, T: Receive + Receive> Provide<'n, T> for $Enum { + fn provide <'source> ( state: &T, token: &mut TokenIter<'source> ) -> Perhaps { Ok(if let Some(Token { value: Value::Key($x|$y|$xy), .. }) = token.peek() { let mut base = token.clone(); Some(match token.next() { Some(Token { value: Value::Key($x), .. }) => Self::x( - state.give_or_fail(token, ||"x: no unit")?, - state.give_or_fail(token, ||"x: no content")?, + state.receive_or_fail(token, ||"x: no unit")?, + state.receive_or_fail(token, ||"x: no content")?, ), Some(Token { value: Value::Key($y), .. }) => Self::y( - state.give_or_fail(token, ||"y: no unit")?, - state.give_or_fail(token, ||"y: no content")?, + state.receive_or_fail(token, ||"y: no unit")?, + state.receive_or_fail(token, ||"y: no content")?, ), Some(Token { value: Value::Key($x), .. }) => Self::xy( - state.give_or_fail(token, ||"xy: no unit x")?, - state.give_or_fail(token, ||"xy: no unit y")?, - state.give_or_fail(token, ||"xy: no content")? + state.receive_or_fail(token, ||"xy: no unit x")?, + state.receive_or_fail(token, ||"xy: no unit y")?, + state.receive_or_fail(token, ||"xy: no content")? ), _ => unreachable!(), }) diff --git a/output/src/ops/when.rs b/output/src/ops/when.rs index db295ab..c2c0b1a 100644 --- a/output/src/ops/when.rs +++ b/output/src/ops/when.rs @@ -2,17 +2,15 @@ use crate::*; /// Show an item only when a condition is true. pub struct When(pub bool, pub A); - impl When { /// Create a binary condition. pub const fn new (c: bool, a: A) -> Self { Self(c, a) } } - #[cfg(feature = "dsl")] -impl<'n, State: Give, A: Take<'n, State>> Take<'n, State> for When { - fn take <'source> (state: &State, words: &mut TokenIter<'source>) -> Perhaps { +impl<'n, State: Receive + Receive, A: 'n> Provide<'n, State> for When { + fn provide <'source> (state: &State, words: &mut TokenIter<'source>) -> Perhaps { Ok(if let Some(Token { value: Value::Key("when"), .. @@ -20,15 +18,14 @@ impl<'n, State: Give, A: Take<'n, State>> Take<'n, State> for When { let _ = words.next(); let base = words.clone(); return Ok(Some(Self( - state.give_or_fail(words, ||"cond: no condition")?, - A::take_or_fail(state, words, ||"cond: no content")?, + state.receive_or_fail(words, ||"cond: no condition")?, + state.receive_or_fail(words, ||"cond: no content")?, ))) } else { None }) } } - impl> Content for When { fn layout (&self, to: E::Area) -> E::Area { let Self(cond, item) = self; diff --git a/proc/src/proc_command.rs b/proc/src/proc_command.rs index b8cf812..e16146d 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: Take::take_or_fail(self, words)?, + #arg: Provide::provide_or_fail(self, words)?, }); } out @@ -149,8 +149,8 @@ impl ToTokens for CommandDef { } } /// Generated by [tengri_proc::command]. - impl<'n> ::tengri::dsl::Take<'n, #state> for #command_enum { - fn take <'source> ( + impl<'n> ::tengri::dsl::Provide<'n, #state> for #command_enum { + fn provide <'source> ( state: &#state, words: &mut ::tengri::dsl::TokenIter<'source> ) -> Perhaps { diff --git a/proc/src/proc_expose.rs b/proc/src/proc_expose.rs index b0fae39..6911a40 100644 --- a/proc/src/proc_expose.rs +++ b/proc/src/proc_expose.rs @@ -85,8 +85,8 @@ impl ToTokens for ExposeImpl { let values = variants.iter().map(ExposeArm::from); write_quote_to(out, quote! { /// Generated by [tengri_proc::expose]. - impl<'n> ::tengri::dsl::Take<'n, #state> for #t { - fn take <'source> ( + impl<'n> ::tengri::dsl::Provide<'n, #state> for #t { + fn provide <'source> ( state: &#state, words: &mut ::tengri::dsl::TokenIter<'source> ) -> Perhaps { diff --git a/proc/src/proc_view.rs b/proc/src/proc_view.rs index f68adc0..a8d8f7a 100644 --- a/proc/src/proc_view.rs +++ b/proc/src/proc_view.rs @@ -46,7 +46,7 @@ impl ToTokens for ViewDef { let builtins: Vec<_> = builtins_with_types() .iter() .map(|ty|write_quote(quote! { - let value: Option<#ty> = Give::<#ty>::give(self, &mut exp.clone())?; + let value: Option<#ty> = Receive::<#ty>::receive(self, &mut exp.clone())?; if let Some(value) = value { return Ok(Some(value.boxed())) } @@ -65,8 +65,8 @@ impl ToTokens for ViewDef { /// Gives [#self_ty] the ability to construct the [Render]able /// which might corresponds to a given [TokenStream], /// while taking [#self_ty]'s state into consideration. - impl ::tengri::dsl::Give>> for #self_ty { - fn give <'source> (&self, words: &mut ::tengri::dsl::TokenIter<'source>) + impl ::tengri::dsl::Receive>> for #self_ty { + fn receive <'source> (&self, words: &mut ::tengri::dsl::TokenIter<'source>) -> Perhaps>> { Ok(if let Some(::tengri::dsl::Token { value, .. }) = words.peek() { @@ -77,7 +77,7 @@ impl ToTokens for ViewDef { #(#builtins)* None }, - // Symbols are handled by user-taked functions + // Symbols are handled by user-provided functions // that take no parameters but `&self`. ::tengri::dsl::Value::Sym(sym) => match sym { #(#exposed)* @@ -100,7 +100,7 @@ impl ToTokens for ViewDef { #self_ty::view(self) } } - // Original user-taked implementation: + // Original user-provided implementation: #block }) }