mirror of
https://codeberg.org/unspeaker/tek.git
synced 2025-12-06 19:56:42 +01:00
enable rest of layout operators
This commit is contained in:
parent
08a6716bb7
commit
ddcb967a2c
1 changed files with 82 additions and 151 deletions
|
|
@ -1,10 +1,54 @@
|
|||
use crate::*;
|
||||
use std::marker::PhantomData;
|
||||
use EdnItem::*;
|
||||
pub type EdnCallback<'a, O, State> =
|
||||
dyn Fn(&'a State)-> RenderBox<'a, O> + Send + Sync + 'a;
|
||||
pub type EdnRenderCallback<'a, O, State> =
|
||||
Box<EdnCallback<'a, O, State>>;
|
||||
/// Renders from EDN source and context.
|
||||
#[derive(Default)]
|
||||
pub enum EdnView<'a, E: Output, T: EdnViewData<'a, E> + std::fmt::Debug> {
|
||||
#[default]
|
||||
Inert,
|
||||
_Unused(PhantomData<&'a E>),
|
||||
Ok(T, EdnItem<&'a str>),
|
||||
//render: Box<dyn Fn(&'a T)->Box<dyn Render<E> + Send + Sync + 'a> + Send + Sync + 'a>
|
||||
Err(String)
|
||||
}
|
||||
impl<'a, E: Output, T: EdnViewData<'a, E> + std::fmt::Debug> std::fmt::Debug for EdnView<'a, E, T> {
|
||||
fn fmt (&self, f: &mut std::fmt::Formatter<'_>) -> Result<(), std::fmt::Error> {
|
||||
match self {
|
||||
Self::Inert | Self::_Unused(_) => write!(f, "EdnView::Inert"),
|
||||
Self::Ok(state, view) => write!(f, "EdnView::Ok(state={state:?} view={view:?}"),
|
||||
Self::Err(error) => write!(f, "EdnView::Err({error})"),
|
||||
}
|
||||
}
|
||||
}
|
||||
impl<'a, E: Output, T> EdnViewData<'a, E> for T where T:
|
||||
EdnProvide<'a, bool> +
|
||||
EdnProvide<'a, usize> +
|
||||
EdnProvide<'a, E::Unit> +
|
||||
EdnProvide<'a, Box<dyn Render<E> + 'a>>
|
||||
{}
|
||||
impl<'a, E: Output, T: EdnViewData<'a, E> + std::fmt::Debug> EdnView<'a, E, T> {
|
||||
pub fn from_source (state: T, source: &'a str) -> Self {
|
||||
match EdnItem::read_one(&source) {
|
||||
Ok((layout, _)) => Self::Ok(state, layout),
|
||||
Err(error) => Self::Err(format!("{error} in {source}"))
|
||||
}
|
||||
}
|
||||
pub fn from_items (state: T, items: Vec<EdnItem<&'a str>>) -> Self {
|
||||
Self::Ok(state, EdnItem::Exp(items))
|
||||
}
|
||||
}
|
||||
impl<E: Output, T: for<'a>EdnViewData<'a, E> + Send + Sync + std::fmt::Debug> Content<E> for EdnView<'_, E, T> {
|
||||
fn content (&self) -> impl Render<E> {
|
||||
match self {
|
||||
Self::Ok(state, layout) => state.get_content(layout),
|
||||
Self::Err(_error) => {
|
||||
panic!("{self:?}");
|
||||
//TODO:&format!("EdnView error: {error:?}")) // FIXME: String is not Render
|
||||
},
|
||||
_ => todo!()
|
||||
}
|
||||
}
|
||||
}
|
||||
/// Provides values to the template
|
||||
pub trait EdnViewData<'a, E: Output>:
|
||||
EdnProvide<'a, bool> +
|
||||
|
|
@ -27,181 +71,68 @@ pub trait EdnViewData<'a, E: Output>:
|
|||
fn get_unit (&'a self, item: &'a EdnItem<&str>) -> E::Unit {
|
||||
match &item { Num(n) => (*n as u16).into(), _ => EdnProvide::get_or_fail(self, item) }
|
||||
}
|
||||
fn arg <T> (&'a self, item: &'a EdnItem<&'a str>) -> Option<T> where Self: EdnProvide<'a, T> {
|
||||
EdnProvide::<T>::get(self, item)
|
||||
}
|
||||
fn item (&'a self, item: &'a EdnItem<&'a str>) -> Option<Box<dyn Render<E> + 'a>> {
|
||||
self.get(item)
|
||||
}
|
||||
fn get_content (&'a self, item: &'a EdnItem<&'a str>) -> Box<dyn Render<E> + 'a> where E: 'a {
|
||||
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) {
|
||||
return builtin.boxed()
|
||||
}
|
||||
|
||||
if let Some(builtin) = Either::<_, RenderBox<'a, E>, RenderBox<'a, E>>::try_from_edn(
|
||||
self, head, tail
|
||||
) {
|
||||
return builtin.boxed()
|
||||
}
|
||||
|
||||
if let Some(builtin) = Align::<_, RenderBox<'a, E>>::try_from_edn(self, head, tail) {
|
||||
return builtin.boxed()
|
||||
}
|
||||
|
||||
if let Some(builtin) = Bsp::<_, RenderBox<'a, E>, RenderBox<'a, E>>::try_from_edn(
|
||||
self, head, tail
|
||||
) {
|
||||
return builtin.boxed()
|
||||
}
|
||||
|
||||
if let Some(builtin) = Fill::<_, RenderBox<'a, E>>::try_from_edn(self, head, tail) {
|
||||
return builtin.boxed()
|
||||
}
|
||||
|
||||
panic!("{item:?}");
|
||||
match (head, tail) {
|
||||
|
||||
(Key("fixed/x"), [x, a]) => Fixed::x(self.arg(x).unwrap(), self.get_content(a)).boxed(),
|
||||
(Key("fixed/y"), [y, a]) => Fixed::y(self.arg(y).unwrap(), self.get_content(a)).boxed(),
|
||||
(Key("fixed/xy"), [x, y, a]) => Fixed::xy(self.arg(x).unwrap(), self.arg(y).unwrap(), self.get_content(a)).boxed(),
|
||||
|
||||
(Key("max/x"), [x, a]) => Max::x(self.arg(x).unwrap(), self.get_content(a)).boxed(),
|
||||
(Key("max/y"), [y, a]) => Max::y(self.arg(y).unwrap(), self.get_content(a)).boxed(),
|
||||
|
||||
(Key("push/x"), [x, a]) => Push::x(self.arg(x).unwrap(), self.get_content(a)).boxed(),
|
||||
(Key("push/y"), [y, a]) => Push::y(self.arg(y).unwrap(), self.get_content(a)).boxed(),
|
||||
|
||||
(Key("pad/x"), [x, a]) => Padding::x(self.arg(x).unwrap(), self.get_content(a)).boxed(),
|
||||
(Key("pad/y"), [y, a]) => Padding::y(self.arg(y).unwrap(), self.get_content(a)).boxed(),
|
||||
(Key("pad/xy"), [x, y, a]) => Padding::xy(self.arg(x).unwrap(), self.arg(y).unwrap(), self.get_content(a)).boxed(),
|
||||
|
||||
(Key("grow/x"), [x, a]) => Margin::x(self.arg(x).unwrap(), self.get_content(a)).boxed(),
|
||||
(Key("grow/y"), [y, a]) => Margin::y(self.arg(y).unwrap(), self.get_content(a)).boxed(),
|
||||
(Key("grow/xy"), [x, y, a]) => Margin::xy(self.arg(x).unwrap(), self.arg(y).unwrap(), self.get_content(a)).boxed(),
|
||||
|
||||
_ => todo!("{:?} {:?}", &head, &tail)
|
||||
if let Some(builtin) = Fixed::<_, _, RenderBox<'a, E>>::try_from_edn(self, head, tail) {
|
||||
return builtin.boxed()
|
||||
}
|
||||
if let Some(builtin) = Min::<_, _, RenderBox<'a, E>>::try_from_edn(self, head, tail) {
|
||||
return builtin.boxed()
|
||||
}
|
||||
if let Some(builtin) = Max::<_, _, RenderBox<'a, E>>::try_from_edn(self, head, tail) {
|
||||
return builtin.boxed()
|
||||
}
|
||||
if let Some(builtin) = Shrink::<_, _, RenderBox<'a, E>>::try_from_edn(self, head, tail) {
|
||||
return builtin.boxed()
|
||||
}
|
||||
if let Some(builtin) = Expand::<_, _, RenderBox<'a, E>>::try_from_edn(self, head, tail) {
|
||||
return builtin.boxed()
|
||||
}
|
||||
if let Some(builtin) = Push::<_, _, RenderBox<'a, E>>::try_from_edn(self, head, tail) {
|
||||
return builtin.boxed()
|
||||
}
|
||||
if let Some(builtin) = Pull::<_, _, RenderBox<'a, E>>::try_from_edn(self, head, tail) {
|
||||
return builtin.boxed()
|
||||
}
|
||||
if let Some(builtin) = Margin::<_, _, RenderBox<'a, E>>::try_from_edn(self, head, tail) {
|
||||
return builtin.boxed()
|
||||
}
|
||||
if let Some(builtin) = Padding::<_, _, RenderBox<'a, E>>::try_from_edn(self, head, tail) {
|
||||
return builtin.boxed()
|
||||
}
|
||||
EdnProvide::get_or_fail(self, &item)
|
||||
} else {
|
||||
panic!("invalid expression: {e:?}")
|
||||
EdnProvide::get_or_fail(self, &item)
|
||||
},
|
||||
_ => EdnProvide::get_or_fail(self, &item)
|
||||
}
|
||||
//panic!("no content")
|
||||
}
|
||||
}
|
||||
impl<'a, E: Output, T> EdnViewData<'a, E> for T where T:
|
||||
EdnProvide<'a, bool> +
|
||||
EdnProvide<'a, usize> +
|
||||
EdnProvide<'a, E::Unit> +
|
||||
EdnProvide<'a, Box<dyn Render<E> + 'a>>
|
||||
{}
|
||||
|
||||
/// Renders from EDN source and context.
|
||||
#[derive(Default)]
|
||||
pub enum EdnView<'a, E: Output, T: EdnViewData<'a, E> + std::fmt::Debug> {
|
||||
#[default]
|
||||
Inert,
|
||||
_Unused(PhantomData<&'a E>),
|
||||
Ok(T, EdnItem<&'a str>),
|
||||
//render: Box<dyn Fn(&'a T)->Box<dyn Render<E> + Send + Sync + 'a> + Send + Sync + 'a>
|
||||
Err(String)
|
||||
}
|
||||
impl<'a, E: Output, T: EdnViewData<'a, E> + std::fmt::Debug> std::fmt::Debug for EdnView<'a, E, T> {
|
||||
fn fmt (&self, f: &mut std::fmt::Formatter<'_>) -> Result<(), std::fmt::Error> {
|
||||
match self {
|
||||
Self::Inert | Self::_Unused(_) => write!(f, "EdnView::Inert"),
|
||||
Self::Ok(state, view) => write!(f, "EdnView::Ok(state={state:?} view={view:?}"),
|
||||
Self::Err(error) => write!(f, "EdnView::Err({error})"),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a, E: Output, T: EdnViewData<'a, E> + std::fmt::Debug> EdnView<'a, E, T> {
|
||||
pub fn from_source (state: T, source: &'a str) -> Self {
|
||||
match EdnItem::read_one(&source) {
|
||||
Ok((layout, _)) => Self::Ok(state, layout),
|
||||
Err(error) => Self::Err(format!("{error} in {source}"))
|
||||
}
|
||||
}
|
||||
pub fn from_items (state: T, items: Vec<EdnItem<&'a str>>) -> Self {
|
||||
Self::Ok(state, EdnItem::Exp(items))
|
||||
}
|
||||
}
|
||||
|
||||
impl<E: Output, T: for<'a>EdnViewData<'a, E> + Send + Sync + std::fmt::Debug> Content<E> for EdnView<'_, E, T> {
|
||||
fn content (&self) -> impl Render<E> {
|
||||
match self {
|
||||
Self::Ok(state, layout) => state.get_content(layout),
|
||||
Self::Err(_error) => {
|
||||
panic!("{self:?}");
|
||||
//TODO:&format!("EdnView error: {error:?}")) // FIXME: String is not Render
|
||||
},
|
||||
_ => todo!()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//fn match_exp <'a, E: Output + 'a, State: EdnViewData<'a, E>> (
|
||||
//s: &'a State,
|
||||
//head: &EdnItem<&str>,
|
||||
//tail: &'a [EdnItem<String>]
|
||||
//) -> Box<dyn Render<E> + 'a> {
|
||||
//match (head, tail) {
|
||||
|
||||
//(Key("when"), [c, a]) => When(
|
||||
//s.get_bool(c.to_ref()),
|
||||
//s.get_content(a.to_ref())).boxed(),
|
||||
|
||||
//(Key("either"),[c, a, b]) => Either(
|
||||
//s.get_bool(c.to_ref()),
|
||||
//s.get_content(a.to_ref()),
|
||||
//s.get_content(b.to_ref())).boxed(),
|
||||
|
||||
//(Key("align/c"), [a]) => Align::c(s.get_content(a.to_ref())).boxed(),
|
||||
//(Key("align/x"), [a]) => Align::x(s.get_content(a.to_ref())).boxed(),
|
||||
//(Key("align/y"), [a]) => Align::y(s.get_content(a.to_ref())).boxed(),
|
||||
//(Key("align/n"), [a]) => Align::n(s.get_content(a.to_ref())).boxed(),
|
||||
//(Key("align/s"), [a]) => Align::s(s.get_content(a.to_ref())).boxed(),
|
||||
//(Key("align/e"), [a]) => Align::e(s.get_content(a.to_ref())).boxed(),
|
||||
//(Key("align/w"), [a]) => Align::w(s.get_content(a.to_ref())).boxed(),
|
||||
//(Key("align/nw"), [a]) => Align::nw(s.get_content(a.to_ref())).boxed(),
|
||||
//(Key("align/ne"), [a]) => Align::ne(s.get_content(a.to_ref())).boxed(),
|
||||
//(Key("align/sw"), [a]) => Align::sw(s.get_content(a.to_ref())).boxed(),
|
||||
//(Key("align/se"), [a]) => Align::se(s.get_content(a.to_ref())).boxed(),
|
||||
|
||||
//(Key("bsp/a"), [a, b]) => Bsp::a(s.get_content(a.to_ref()), s.get_content(b.to_ref()),).boxed(),
|
||||
//(Key("bsp/b"), [a, b]) => Bsp::b(s.get_content(a.to_ref()), s.get_content(b.to_ref()),).boxed(),
|
||||
//(Key("bsp/e"), [a, b]) => Bsp::e(s.get_content(a.to_ref()), s.get_content(b.to_ref()),).boxed(),
|
||||
//(Key("bsp/n"), [a, b]) => Bsp::n(s.get_content(a.to_ref()), s.get_content(b.to_ref()),).boxed(),
|
||||
//(Key("bsp/s"), [a, b]) => Bsp::s(s.get_content(a.to_ref()), s.get_content(b.to_ref()),).boxed(),
|
||||
//(Key("bsp/w"), [a, b]) => Bsp::w(s.get_content(a.to_ref()), s.get_content(b.to_ref()),).boxed(),
|
||||
|
||||
//(Key("fill/x"), [a]) => Fill::x(s.get_content(a.to_ref())).boxed(),
|
||||
//(Key("fill/y"), [a]) => Fill::y(s.get_content(a.to_ref())).boxed(),
|
||||
//(Key("fill/xy"), [a]) => Fill::xy(s.get_content(a.to_ref())).boxed(),
|
||||
|
||||
//(Key("fixed/x"), [x, a]) => Fixed::x(s.get_unit(x.to_ref()), s.get_content(a.to_ref())).boxed(),
|
||||
//(Key("fixed/y"), [y, a]) => Fixed::y(s.get_unit(y.to_ref()), s.get_content(a.to_ref())).boxed(),
|
||||
//(Key("fixed/xy"), [x, y, a]) => Fixed::xy(s.get_unit(x.to_ref()), s.get_unit(y.to_ref()), s.get_content(a.to_ref())).boxed(),
|
||||
|
||||
//(Key("max/x"), [x, a]) => Max::x(s.get_unit(x.to_ref()), s.get_content(a.to_ref())).boxed(),
|
||||
//(Key("max/y"), [y, a]) => Max::y(s.get_unit(y.to_ref()), s.get_content(a.to_ref())).boxed(),
|
||||
|
||||
//(Key("push/x"), [x, a]) => Push::x(s.get_unit(x.to_ref()), s.get_content(a.to_ref())).boxed(),
|
||||
//(Key("push/y"), [y, a]) => Push::y(s.get_unit(y.to_ref()), s.get_content(a.to_ref())).boxed(),
|
||||
|
||||
//(Key("pad/x"), [x, a]) => Padding::x(s.get_unit(x.to_ref()), s.get_content(a.to_ref())).boxed(),
|
||||
//(Key("pad/y"), [y, a]) => Padding::y(s.get_unit(y.to_ref()), s.get_content(a.to_ref())).boxed(),
|
||||
//(Key("pad/xy"), [x, y, a]) => Padding::xy(s.get_unit(x.to_ref()), s.get_unit(y.to_ref()), s.get_content(a.to_ref())).boxed(),
|
||||
|
||||
//(Key("grow/x"), [x, a]) => Margin::x(s.get_unit(x.to_ref()), s.get_content(a.to_ref())).boxed(),
|
||||
//(Key("grow/y"), [y, a]) => Margin::y(s.get_unit(y.to_ref()), s.get_content(a.to_ref())).boxed(),
|
||||
//(Key("grow/xy"), [x, y, a]) => Margin::xy(s.get_unit(x.to_ref()), s.get_unit(y.to_ref()), s.get_content(a.to_ref())).boxed(),
|
||||
|
||||
//_ => todo!("{:?} {:?}", &head, &tail)
|
||||
//}
|
||||
//}
|
||||
// A function that returns a `RenderBox.
|
||||
pub type EdnCallback<'a, O, State> =
|
||||
dyn Fn(&'a State)-> RenderBox<'a, O> + Send + Sync + 'a;
|
||||
// A box containing a function that returns a `RenderBox.
|
||||
pub type EdnRenderCallback<'a, O, State> =
|
||||
Box<EdnCallback<'a, O, State>>;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue