move edn_view into layout

This commit is contained in:
🪞👃🪞 2025-01-07 21:41:51 +01:00
parent 4d0f98acd2
commit 9a70fbc416
13 changed files with 91 additions and 89 deletions

View file

@ -8,11 +8,8 @@ clojure-reader = "0.3.0"
konst = "0.3.16"
itertools = "0.14.0"
tek_input = { optional = true, path = "../input" }
tek_output = { optional = true, path = "../output" }
[features]
default = ["tek_input", "tek_output"]
default = []
[dev-dependencies]
tek_tui = { path = "../tui" }

View file

@ -1,101 +0,0 @@
use crate::*;
use std::marker::PhantomData;
use ::tek_output::*;
use EdnItem::*;
pub type EdnCallback<'a, Output, State> =
dyn Fn(&'a State)-> RenderBox<'a, Output> + Send + Sync + 'a;
pub type EdnRenderCallback<'a, Output, State> =
Box<EdnCallback<'a, Output, State>>;
/// Provides values to the template
pub trait EdnViewData<E: Output> {
fn get_bool (&self, _sym: EdnItem<&str>) -> bool { false }
fn get_unit (&self, _sym: EdnItem<&str>) -> E::Unit { 0.into() }
fn get_usize (&self, _sym: EdnItem<&str>) -> usize { 0 }
fn get_content <'a> (&'a self, _sym: EdnItem<&'a str>) -> RenderBox<'a, E> { Box::new(()) }
}
/// Renders from EDN source and context.
pub enum EdnView<E: Output, T: EdnViewData<E>> {
_Unused(PhantomData<E>),
Ok(T, EdnItem<String>),
//render: Box<dyn Fn(&'a T)->Box<dyn Render<E> + Send + Sync + 'a> + Send + Sync + 'a>
Err(String)
}
impl<E: Output, T: EdnViewData<E>> EdnView<E, T> {
pub fn from_source (state: T, source: &str) -> Self {
match EdnItem::read_one(&source) {
Ok((layout, _)) => Self::Ok(state, layout),
Err(error) => Self::Err(format!("{error}"))
}
}
pub fn from_items (state: T, items: &[EdnItem<&str>]) -> Self {
Self::Ok(state, EdnItem::Exp(items.iter().map(|i|(*i).clone()).collect()))
}
}
impl<E: Output, T: EdnViewData<E> + Send + Sync> Content<E> for EdnView<E, T> {
fn content (&self) -> impl Render<E> {
use EdnItem::*;
match self {
Self::Ok(s, layout) => match layout {
Nil => ().boxed(),
Sym(t) => s.get_content(Sym(t.as_str())).boxed(),
Key(t) => panic!("todo: add error handling to content() chain. unexpected key {t}"),
Num(n) => panic!("todo: add error handling to content() chain. unexpected num {n}"),
Exp(e) => if let [head, tail @ ..] = e.as_slice() {
let head = &head.to_ref();
match (head, tail) {
(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("when"), [c, a]) => When(s.get_bool(c.to_ref()), s.get_content(a.to_ref())).boxed(),
_ => todo!("{:?} {:?}", &head, &tail)
}
} else {
panic!("todo: add error handling to content() chain. invalid expression {e:?}")
},
},
Self::Err(error) => {
Box::new(())//&format!("EdnView error: {error:?}"))
},
_ => todo!()
}
}
}

View file

@ -1,16 +1,12 @@
#![feature(type_alias_impl_trait)]
#![feature(impl_trait_in_fn_trait_return)]
pub(crate) use std::{
fmt::{Debug, Formatter, Error as FormatError}
};
pub use ::tek_output;
pub(crate) use std::{fmt::{Debug, Formatter, Error as FormatError}};
mod edn_error; pub use self::edn_error::*;
mod edn_item; pub use self::edn_item::*;
mod edn_iter; pub use self::edn_iter::*;
mod edn_token; pub use self::edn_token::*;
mod edn_view; pub use self::edn_view::*;
#[cfg(feature = "tek_output")]
pub(crate) use ::tek_output::*;