mirror of
https://codeberg.org/unspeaker/tengri.git
synced 2025-12-06 19:56:44 +01:00
proc, view: fix usage of builtins
This commit is contained in:
parent
776cea6f1b
commit
7516517078
4 changed files with 83 additions and 78 deletions
|
|
@ -1,9 +1,10 @@
|
||||||
|
//mod reduce; pub use self::reduce::*;
|
||||||
mod align; pub use self::align::*;
|
mod align; pub use self::align::*;
|
||||||
mod bsp; pub use self::bsp::*;
|
mod bsp; pub use self::bsp::*;
|
||||||
mod cond; pub use self::cond::*;
|
mod either; pub use self::either::*;
|
||||||
mod map; pub use self::map::*;
|
mod map; pub use self::map::*;
|
||||||
mod memo; pub use self::memo::*;
|
mod memo; pub use self::memo::*;
|
||||||
mod stack; pub use self::stack::*;
|
mod stack; pub use self::stack::*;
|
||||||
//mod reduce; pub use self::reduce::*;
|
|
||||||
mod thunk; pub use self::thunk::*;
|
mod thunk; pub use self::thunk::*;
|
||||||
mod transform; pub use self::transform::*;
|
mod transform; pub use self::transform::*;
|
||||||
|
mod when; pub use self::when::*;
|
||||||
|
|
|
||||||
38
output/src/ops/either.rs
Normal file
38
output/src/ops/either.rs
Normal file
|
|
@ -0,0 +1,38 @@
|
||||||
|
use crate::*;
|
||||||
|
|
||||||
|
/// Show one item if a condition is true and another if the condition is false
|
||||||
|
pub struct Either<A, B>(pub bool, pub A, pub B);
|
||||||
|
impl<A, B> Either<A, B> {
|
||||||
|
/// Create a ternary condition.
|
||||||
|
pub const fn new (c: bool, a: A, b: B) -> Self {
|
||||||
|
Self(c, a, b)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
#[cfg(feature = "dsl")]
|
||||||
|
impl<A, B, T: Dsl<bool> + Dsl<A> + Dsl<B>> Namespace<T> for Either<A, B> {
|
||||||
|
fn take_from <'source> (
|
||||||
|
state: &T,
|
||||||
|
token: &mut TokenIter<'source>
|
||||||
|
) -> Perhaps<Self> {
|
||||||
|
if let Some(Token { value: Value::Key("either"), .. }) = token.peek() {
|
||||||
|
let base = token.clone();
|
||||||
|
let _ = token.next().unwrap();
|
||||||
|
return Ok(Some(Self(
|
||||||
|
state.take_or_fail(token, "either: no condition")?,
|
||||||
|
state.take_or_fail(token, "either: no content 1")?,
|
||||||
|
state.take_or_fail(token, "either: no content 2")?,
|
||||||
|
)))
|
||||||
|
}
|
||||||
|
Ok(None)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
impl<E: Output, A: Render<E>, B: Render<E>> Content<E> for Either<A, B> {
|
||||||
|
fn layout (&self, to: E::Area) -> E::Area {
|
||||||
|
let Self(cond, a, b) = self;
|
||||||
|
if *cond { a.layout(to) } else { b.layout(to) }
|
||||||
|
}
|
||||||
|
fn render (&self, to: &mut E) {
|
||||||
|
let Self(cond, a, b) = self;
|
||||||
|
if *cond { a.render(to) } else { b.render(to) }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -8,15 +8,6 @@ impl<A> When<A> {
|
||||||
Self(c, a)
|
Self(c, a)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Show one item if a condition is true and another if the condition is false
|
|
||||||
pub struct Either<A, B>(pub bool, pub A, pub B);
|
|
||||||
impl<A, B> Either<A, B> {
|
|
||||||
/// Create a ternary condition.
|
|
||||||
pub const fn new (c: bool, a: A, b: B) -> Self {
|
|
||||||
Self(c, a, b)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
#[cfg(feature = "dsl")]
|
#[cfg(feature = "dsl")]
|
||||||
impl<A, T: Dsl<bool> + Dsl<A>> Namespace<T> for When<A> {
|
impl<A, T: Dsl<bool> + Dsl<A>> Namespace<T> for When<A> {
|
||||||
fn take_from <'source> (
|
fn take_from <'source> (
|
||||||
|
|
@ -55,31 +46,3 @@ impl<E: Output, A: Render<E>> Content<E> for When<A> {
|
||||||
if *cond { item.render(to) }
|
if *cond { item.render(to) }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
#[cfg(feature = "dsl")]
|
|
||||||
impl<A, B, T: Dsl<bool> + Dsl<A> + Dsl<B>> Namespace<T> for Either<A, B> {
|
|
||||||
fn take_from <'source> (
|
|
||||||
state: &T,
|
|
||||||
token: &mut TokenIter<'source>
|
|
||||||
) -> Perhaps<Self> {
|
|
||||||
if let Some(Token { value: Value::Key("either"), .. }) = token.peek() {
|
|
||||||
let base = token.clone();
|
|
||||||
let _ = token.next().unwrap();
|
|
||||||
return Ok(Some(Self(
|
|
||||||
state.take_or_fail(token, "either: no condition")?,
|
|
||||||
state.take_or_fail(token, "either: no content 1")?,
|
|
||||||
state.take_or_fail(token, "either: no content 2")?,
|
|
||||||
)))
|
|
||||||
}
|
|
||||||
Ok(None)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
impl<E: Output, A: Render<E>, B: Render<E>> Content<E> for Either<A, B> {
|
|
||||||
fn layout (&self, to: E::Area) -> E::Area {
|
|
||||||
let Self(cond, a, b) = self;
|
|
||||||
if *cond { a.layout(to) } else { b.layout(to) }
|
|
||||||
}
|
|
||||||
fn render (&self, to: &mut E) {
|
|
||||||
let Self(cond, a, b) = self;
|
|
||||||
if *cond { a.render(to) } else { b.render(to) }
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
@ -42,50 +42,53 @@ impl Parse for ViewImpl {
|
||||||
impl ToTokens for ViewDef {
|
impl ToTokens for ViewDef {
|
||||||
fn to_tokens (&self, out: &mut TokenStream2) {
|
fn to_tokens (&self, out: &mut TokenStream2) {
|
||||||
let Self(ViewMeta { output }, ViewImpl { block, exposed }) = self;
|
let Self(ViewMeta { output }, ViewImpl { block, exposed }) = self;
|
||||||
let view = &block.self_ty;
|
let self_ty = &block.self_ty;
|
||||||
let builtins = builtins().iter().map(|ty|write_quote(quote! {
|
let builtins: Vec<_> = builtins_with_types()
|
||||||
if let Some(value) = Namespace::<#ty>::take_from(state, &mut exp.clone())? {
|
.iter()
|
||||||
return Ok(Some(value.boxed()))
|
.map(|ty|write_quote(quote! {
|
||||||
}
|
let value: Option<#ty> = Dsl::take(state, &mut exp.clone())?;
|
||||||
})).collect::<Vec<_>>();
|
if let Some(value) = value {
|
||||||
let mut available = vec![];
|
return Ok(Some(value.boxed()))
|
||||||
let exposed: Vec<_> = exposed.iter().map(|(key, value)|{
|
}
|
||||||
available.push(key.clone());
|
}))
|
||||||
write_quote(quote! { #key => Some(#view::#value(state).boxed()), })
|
.collect();
|
||||||
}).collect();
|
let exposed: Vec<_> = exposed
|
||||||
let available: String = available.join(", ");
|
.iter()
|
||||||
let error_msg = LitStr::new(
|
.map(|(key, value)|write_quote(quote! {
|
||||||
&format!("expected Sym(content), got: {{token:?}}, available: {available}"),
|
#key => Some(#self_ty::#value(state).boxed()),
|
||||||
Span::call_site()
|
})).collect();
|
||||||
);
|
|
||||||
write_quote_to(out, quote! {
|
write_quote_to(out, quote! {
|
||||||
#block
|
#block
|
||||||
/// Generated by [tengri_proc].
|
/// Generated by [tengri_proc].
|
||||||
///
|
///
|
||||||
/// Delegates the rendering of [#view] to the [#view::view} method,
|
/// Delegates the rendering of [#self_ty] to the [#self_ty::view} method,
|
||||||
/// which you will need to implement, e.g. passing a [TokenIter]
|
/// which you will need to implement, e.g. passing a [TokenIter]
|
||||||
/// containing a layout and keybindings config from user dirs.
|
/// containing a layout and keybindings config from user dirs.
|
||||||
impl ::tengri::output::Content<#output> for #view {
|
impl ::tengri::output::Content<#output> for #self_ty {
|
||||||
fn content (&self) -> impl Render<#output> {
|
fn content (&self) -> impl Render<#output> {
|
||||||
self.view()
|
#self_ty::view(self)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
/// Generated by [tengri_proc].
|
/// Generated by [tengri_proc].
|
||||||
///
|
///
|
||||||
/// Gives [#view] the ability to construct the [Render]able
|
/// Gives [#self_ty] the ability to construct the [Render]able
|
||||||
/// which might corresponds to a given [TokenStream],
|
/// which might corresponds to a given [TokenStream],
|
||||||
/// while taking [#view]'s state into consideration.
|
/// while taking [#self_ty]'s state into consideration.
|
||||||
impl ::tengri::dsl::Namespace<#view> for Box<dyn Render<#output>> {
|
impl ::tengri::dsl::Namespace<#self_ty> for Box<dyn Render<#output> + '_> {
|
||||||
fn take_from <'source> (
|
fn take_from <'source> (
|
||||||
state: &#view,
|
state: &#self_ty,
|
||||||
words: &mut ::tengri::dsl::TokenIter<'source>
|
words: &mut ::tengri::dsl::TokenIter<'source>
|
||||||
) -> Perhaps<Self> {
|
) -> Perhaps<Self> {
|
||||||
Ok(if let Some(::tengri::dsl::Token { value, .. }) = words.peek() {
|
Ok(if let Some(::tengri::dsl::Token { value, .. }) = words.peek() {
|
||||||
match value {
|
match value {
|
||||||
|
// Expressions are handled by built-in functions
|
||||||
|
// that operate over constants and symbols.
|
||||||
::tengri::dsl::Value::Exp(_, exp) => {
|
::tengri::dsl::Value::Exp(_, exp) => {
|
||||||
//#(#builtins)*
|
#(#builtins)*
|
||||||
None
|
None
|
||||||
},
|
},
|
||||||
|
// Symbols are handled by user-provided functions
|
||||||
|
// that take no parameters but `&self`.
|
||||||
::tengri::dsl::Value::Sym(sym) => match sym {
|
::tengri::dsl::Value::Sym(sym) => match sym {
|
||||||
#(#exposed)*
|
#(#exposed)*
|
||||||
_ => None
|
_ => None
|
||||||
|
|
@ -101,21 +104,21 @@ impl ToTokens for ViewDef {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn builtins () -> [TokenStream2;14] {
|
fn builtins_with_types () -> [TokenStream2;14] {
|
||||||
[
|
[
|
||||||
quote! { When::<_> },
|
quote! { When< Box<dyn Render<_> + '_> > },
|
||||||
quote! { Either::<_, _> },
|
quote! { Either< Box<dyn Render<_> + '_>, Box<dyn Render<_> + '_>> },
|
||||||
quote! { Align::<_> },
|
quote! { Align< Box<dyn Render<_> + '_> > },
|
||||||
quote! { Bsp::<_, _> },
|
quote! { Bsp< Box<dyn Render<_> + '_>, Box<dyn Render<_> + '_>> },
|
||||||
quote! { Fill::<_> },
|
quote! { Fill< Box<dyn Render<_> + '_> > },
|
||||||
quote! { Fixed::<_, _> },
|
quote! { Fixed<_, Box<dyn Render<_> + '_> > },
|
||||||
quote! { Min::<_, _> },
|
quote! { Min<_, Box<dyn Render<_> + '_> > },
|
||||||
quote! { Max::<_, _> },
|
quote! { Max<_, Box<dyn Render<_> + '_> > },
|
||||||
quote! { Shrink::<_, _> },
|
quote! { Shrink<_, Box<dyn Render<_> + '_> > },
|
||||||
quote! { Expand::<_, _> },
|
quote! { Expand<_, Box<dyn Render<_> + '_> > },
|
||||||
quote! { Push::<_, _> },
|
quote! { Push<_, Box<dyn Render<_> + '_> > },
|
||||||
quote! { Pull::<_, _> },
|
quote! { Pull<_, Box<dyn Render<_> + '_> > },
|
||||||
quote! { Margin::<_, _> },
|
quote! { Margin<_, Box<dyn Render<_> + '_> > },
|
||||||
quote! { Padding::<_, _> },
|
quote! { Padding<_, Box<dyn Render<_> + '_> > },
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue