mirror of
https://codeberg.org/unspeaker/tek.git
synced 2025-12-06 19:56:42 +01:00
wip: make EdnItem work on Arc<str>
This commit is contained in:
parent
d4f962fbfa
commit
1b9da07280
17 changed files with 152 additions and 260 deletions
|
|
@ -46,7 +46,7 @@ impl<E: Output, A: Content<E>> Content<E> for Align<E, A> {
|
|||
}
|
||||
}
|
||||
impl<'a, E: Output + 'a, T: EdnViewData<'a, E>> TryFromEdn<'a, T> for Align<E, RenderBox<'a, E>> {
|
||||
fn try_from_edn (state: &'a T, head: &EdnItem<&str>, tail: &'a [EdnItem<&str>]) -> Option<Self> {
|
||||
fn try_from_edn (state: &'a T, head: &EdnItem, tail: &'a [EdnItem]) -> Option<Self> {
|
||||
use EdnItem::*;
|
||||
Some(match (head, tail) {
|
||||
(Key("align/c"), [a]) => Self::c(state.get_content(a).expect("no content")),
|
||||
|
|
|
|||
|
|
@ -92,7 +92,7 @@ impl<E: Output, A: Content<E>, B: Content<E>> BspAreas<E, A, B> for Bsp<E, A, B>
|
|||
fn contents (&self) -> (&A, &B) { (&self.2, &self.3) }
|
||||
}
|
||||
impl<'a, E: Output + 'a, T: EdnViewData<'a, E>> TryFromEdn<'a, T> for Bsp<E, RenderBox<'a, E>, RenderBox<'a, E>> {
|
||||
fn try_from_edn (s: &'a T, head: &EdnItem<&str>, tail: &'a [EdnItem<&str>]) -> Option<Self> {
|
||||
fn try_from_edn (s: &'a T, head: &EdnItem, tail: &'a [EdnItem]) -> Option<Self> {
|
||||
use EdnItem::*;
|
||||
Some(match (head, tail) {
|
||||
(Key("bsp/n"), [a, b]) => Self::n(
|
||||
|
|
|
|||
|
|
@ -11,7 +11,7 @@ use EdnItem::*;
|
|||
}
|
||||
$(
|
||||
impl<'a> EdnProvide<'a, $type> for $App {
|
||||
fn get <S: AsRef<str>> (&'a $self, edn: &'a EdnItem<S>) -> Option<$type> {
|
||||
fn get <S: AsRef<str>> (&'a $self, edn: &'a EdnItem) -> Option<$type> {
|
||||
Some(match edn.to_ref() { $(EdnItem::Sym($sym) => $value,)* _ => return None })
|
||||
}
|
||||
}
|
||||
|
|
@ -23,7 +23,7 @@ use EdnItem::*;
|
|||
#[macro_export] macro_rules! edn_provide_content {
|
||||
(|$self:ident:$State:ty|{ $($pat:pat => $expr:expr),* $(,)? }) => {
|
||||
impl<'a, E: Output> EdnProvide<'a, Box<dyn Render<E> + 'a>> for $State {
|
||||
fn get <S: AsRef<str>> (&'a $self, edn: &'a EdnItem<S>) -> Option<Box<dyn Render<E> + 'a>> {
|
||||
fn get <S: AsRef<str>> (&'a $self, edn: &'a EdnItem) -> Option<Box<dyn Render<E> + 'a>> {
|
||||
Some(match edn.to_ref() {
|
||||
$(EdnItem::Sym($pat) => $expr),*,
|
||||
_ => return None
|
||||
|
|
@ -33,7 +33,7 @@ use EdnItem::*;
|
|||
};
|
||||
($Output:ty: |$self:ident:$State:ty|{ $($pat:pat => $expr:expr),* $(,)? }) => {
|
||||
impl<'a> EdnProvide<'a, Box<dyn Render<$Output> + 'a>> for $State {
|
||||
fn get <S: AsRef<str>> (&'a $self, edn: &'a EdnItem<S>) -> Option<Box<dyn Render<$Output> + 'a>> {
|
||||
fn get <S: AsRef<str>> (&'a $self, edn: &'a EdnItem) -> Option<Box<dyn Render<$Output> + 'a>> {
|
||||
Some(match edn.to_ref() {
|
||||
$(EdnItem::Sym($pat) => $expr),*,
|
||||
_ => return None
|
||||
|
|
@ -45,7 +45,7 @@ use EdnItem::*;
|
|||
/// Renders from EDN source and context.
|
||||
#[derive(Default)] pub enum EdnView<'a, E: Output, T: EdnViewData<'a, E> + std::fmt::Debug> {
|
||||
#[default] Inert,
|
||||
Ok(T, EdnItem<&'a str>),
|
||||
Ok(T, EdnItem),
|
||||
//render: Box<dyn Fn(&'a T)->Box<dyn Render<E> + Send + Sync + 'a> + Send + Sync + 'a>
|
||||
Err(String),
|
||||
_Unused(PhantomData<&'a E>),
|
||||
|
|
@ -76,7 +76,7 @@ impl<'a, E: Output, T: EdnViewData<'a, E> + std::fmt::Debug> EdnView<'a, E, T> {
|
|||
Err(error) => Self::Err(format!("{error} in {source}"))
|
||||
}
|
||||
}
|
||||
pub fn from_items (state: T, items: Vec<EdnItem<&'a str>>) -> Self {
|
||||
pub fn from_items (state: T, items: Vec<EdnItem>) -> Self {
|
||||
Self::Ok(state, EdnItem::Exp(items))
|
||||
}
|
||||
}
|
||||
|
|
@ -92,6 +92,17 @@ impl<E: Output, T: for<'a>EdnViewData<'a, E> + Send + Sync + std::fmt::Debug> Co
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
macro_rules! edn_try_delegate {
|
||||
($s:ident, $e:expr, $T:ty) => {
|
||||
if let [head, tail @ ..] = $e.as_slice() {
|
||||
if let Some(content) = <$T>::try_from_edn($s, head, tail) {
|
||||
return Some(content.boxed())
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Provides values to the template
|
||||
pub trait EdnViewData<'a, E: Output>:
|
||||
EdnProvide<'a, bool> +
|
||||
|
|
@ -99,55 +110,42 @@ pub trait EdnViewData<'a, E: Output>:
|
|||
EdnProvide<'a, E::Unit> +
|
||||
EdnProvide<'a, Box<dyn Render<E> + 'a>>
|
||||
{
|
||||
fn get_bool (&'a self, item: &'a EdnItem<&str>) -> Option<bool> {
|
||||
fn get_bool (&'a self, item: &'a EdnItem) -> Option<bool> {
|
||||
Some(match &item {
|
||||
Sym(":false") | Sym(":f") | Num(0) => false,
|
||||
Sym(":true") | Sym(":t") | Num(_) => true,
|
||||
Sym(s) => match s.as_ref() {
|
||||
":false" | ":f" => false,
|
||||
":true" | ":t" => true,
|
||||
_ => return EdnProvide::get(self, item)
|
||||
},
|
||||
Num(0) => false,
|
||||
Num(_) => true,
|
||||
_ => return EdnProvide::get(self, item)
|
||||
})
|
||||
}
|
||||
fn get_usize (&'a self, item: &'a EdnItem<&str>) -> Option<usize> {
|
||||
fn get_usize (&'a self, item: &'a EdnItem) -> Option<usize> {
|
||||
Some(match &item { Num(n) => *n, _ => return EdnProvide::get(self, item) })
|
||||
}
|
||||
fn get_unit (&'a self, item: &'a EdnItem<&str>) -> Option<E::Unit> {
|
||||
fn get_unit (&'a self, item: &'a EdnItem) -> Option<E::Unit> {
|
||||
Some(match &item { Num(n) => (*n as u16).into(), _ => return EdnProvide::get(self, item) })
|
||||
}
|
||||
fn get_content (&'a self, item: &'a EdnItem<&'a str>) -> Option<Box<dyn Render<E> + 'a>> where E: 'a {
|
||||
fn get_content (&'a self, item: &'a EdnItem) -> Option<Box<dyn Render<E> + 'a>> where E: 'a {
|
||||
Some(match item {
|
||||
Nil => Box::new(()),
|
||||
Exp(e) => if let [head, tail @ ..] = e.as_slice() {
|
||||
if let Some(builtin) = When::<_, RenderBox<'a, E>>::try_from_edn(self, head, tail) {
|
||||
builtin.boxed()
|
||||
} else if let Some(builtin) = Either::<_, RenderBox<'a, E>, RenderBox<'a, E>>::try_from_edn(self, head, tail) {
|
||||
builtin.boxed()
|
||||
} else if let Some(builtin) = Align::<_, RenderBox<'a, E>>::try_from_edn(self, head, tail) {
|
||||
builtin.boxed()
|
||||
} else if let Some(builtin) = Bsp::<_, RenderBox<'a, E>, RenderBox<'a, E>>::try_from_edn(self, head, tail) {
|
||||
builtin.boxed()
|
||||
} else if let Some(builtin) = Fill::<_, RenderBox<'a, E>>::try_from_edn(self, head, tail) {
|
||||
builtin.boxed()
|
||||
} else if let Some(builtin) = Fixed::<_, _, RenderBox<'a, E>>::try_from_edn(self, head, tail) {
|
||||
builtin.boxed()
|
||||
} else if let Some(builtin) = Min::<_, _, RenderBox<'a, E>>::try_from_edn(self, head, tail) {
|
||||
builtin.boxed()
|
||||
} else if let Some(builtin) = Max::<_, _, RenderBox<'a, E>>::try_from_edn(self, head, tail) {
|
||||
builtin.boxed()
|
||||
} else if let Some(builtin) = Shrink::<_, _, RenderBox<'a, E>>::try_from_edn(self, head, tail) {
|
||||
builtin.boxed()
|
||||
} else if let Some(builtin) = Expand::<_, _, RenderBox<'a, E>>::try_from_edn(self, head, tail) {
|
||||
builtin.boxed()
|
||||
} else if let Some(builtin) = Push::<_, _, RenderBox<'a, E>>::try_from_edn(self, head, tail) {
|
||||
builtin.boxed()
|
||||
} else if let Some(builtin) = Pull::<_, _, RenderBox<'a, E>>::try_from_edn(self, head, tail) {
|
||||
builtin.boxed()
|
||||
} else if let Some(builtin) = Margin::<_, _, RenderBox<'a, E>>::try_from_edn(self, head, tail) {
|
||||
builtin.boxed()
|
||||
} else if let Some(builtin) = Padding::<_, _, RenderBox<'a, E>>::try_from_edn(self, head, tail) {
|
||||
builtin.boxed()
|
||||
} else {
|
||||
EdnProvide::get_or_fail(self, &item)
|
||||
}
|
||||
} else {
|
||||
Nil => Box::new(()),
|
||||
Exp(ref e) => {
|
||||
edn_try_delegate!(self, e, When::<_, RenderBox<'a, E>>);
|
||||
edn_try_delegate!(self, e, Either::<_, RenderBox<'a, E>, RenderBox<'a, E>>);
|
||||
edn_try_delegate!(self, e, Align::<_, RenderBox<'a, E>>);
|
||||
edn_try_delegate!(self, e, Bsp::<_, RenderBox<'a, E>, RenderBox<'a, E>>);
|
||||
edn_try_delegate!(self, e, Fill::<_, RenderBox<'a, E>>);
|
||||
edn_try_delegate!(self, e, Fixed::<_, _, RenderBox<'a, E>>);
|
||||
edn_try_delegate!(self, e, Min::<_, _, RenderBox<'a, E>>);
|
||||
edn_try_delegate!(self, e, Max::<_, _, RenderBox<'a, E>>);
|
||||
edn_try_delegate!(self, e, Shrink::<_, _, RenderBox<'a, E>>);
|
||||
edn_try_delegate!(self, e, Expand::<_, _, RenderBox<'a, E>>);
|
||||
edn_try_delegate!(self, e, Push::<_, _, RenderBox<'a, E>>);
|
||||
edn_try_delegate!(self, e, Pull::<_, _, RenderBox<'a, E>>);
|
||||
edn_try_delegate!(self, e, Margin::<_, _, RenderBox<'a, E>>);
|
||||
edn_try_delegate!(self, e, Padding::<_, _, RenderBox<'a, E>>);
|
||||
EdnProvide::get_or_fail(self, &item)
|
||||
},
|
||||
_ => EdnProvide::get_or_fail(self, &item)
|
||||
|
|
|
|||
|
|
@ -7,7 +7,7 @@ impl<E, A, B> Either<E, A, B> {
|
|||
}
|
||||
}
|
||||
impl<'a, E: Output + 'a, T: EdnViewData<'a, E>> TryFromEdn<'a, T> for Either<E, RenderBox<'a, E>, RenderBox<'a, E>> {
|
||||
fn try_from_edn (state: &'a T, head: &EdnItem<&str>, tail: &'a [EdnItem<&str>]) -> Option<Self> {
|
||||
fn try_from_edn (state: &'a T, head: &EdnItem, tail: &'a [EdnItem]) -> Option<Self> {
|
||||
use EdnItem::*;
|
||||
if let (Key("either"), [condition, content, alternative]) = (head, tail) {
|
||||
Some(Self::new(
|
||||
|
|
|
|||
|
|
@ -29,7 +29,7 @@ macro_rules! transform_xy {
|
|||
}
|
||||
impl<'a, E: Output + 'a, T: EdnViewData<'a, E>> TryFromEdn<'a, T> for $Enum<E, RenderBox<'a, E>> {
|
||||
fn try_from_edn (
|
||||
state: &'a T, head: &EdnItem<&str>, tail: &'a [EdnItem<&str>]
|
||||
state: &'a T, head: &EdnItem, tail: &'a [EdnItem]
|
||||
) -> Option<Self> {
|
||||
use EdnItem::*;
|
||||
Some(match (head, tail) {
|
||||
|
|
|
|||
|
|
@ -43,7 +43,7 @@ macro_rules! transform_xy_unit {
|
|||
}
|
||||
impl<'a, E: Output + 'a, T: EdnViewData<'a, E>> TryFromEdn<'a, T> for $Enum<E, E::Unit, RenderBox<'a, E>> {
|
||||
fn try_from_edn (
|
||||
state: &'a T, head: &EdnItem<&str>, tail: &'a [EdnItem<&str>]
|
||||
state: &'a T, head: &EdnItem, tail: &'a [EdnItem]
|
||||
) -> Option<Self> {
|
||||
use EdnItem::*;
|
||||
Some(match (head, tail) {
|
||||
|
|
|
|||
|
|
@ -8,7 +8,7 @@ impl<E, A> When<E, A> {
|
|||
}
|
||||
impl<'a, E: Output + 'a, T: EdnViewData<'a, E>> TryFromEdn<'a, T> for When<E, RenderBox<'a, E>> {
|
||||
fn try_from_edn (
|
||||
state: &'a T, head: &EdnItem<&str>, tail: &'a [EdnItem<&str>]
|
||||
state: &'a T, head: &EdnItem, tail: &'a [EdnItem]
|
||||
) -> Option<Self> {
|
||||
use EdnItem::*;
|
||||
if let (Key("when"), [condition, content]) = (head, tail) {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue