wip: examples for the edn rendering

This commit is contained in:
🪞👃🪞 2025-01-05 04:25:31 +01:00
parent 433e4df0f2
commit 174a7ee614
9 changed files with 51 additions and 30 deletions

4
edn/README.md Normal file
View file

@ -0,0 +1,4 @@
# `tek_edn`
parser and bindings for tek's configuration language,
which is based on edn.

8
edn/examples/edn01.rs Normal file
View file

@ -0,0 +1,8 @@
use tek_edn::*;
const SOURCE: &'static str = include_str!("edn01.edn");
fn main () {
Tui::run(Arc::new(RwLock::new(Demo::new())))?;
Ok(())
}

8
edn/examples/edn02.rs Normal file
View file

@ -0,0 +1,8 @@
use tek_edn::*;
const SOURCE: &'static str = include_str!("edn02.edn");
fn main () {
Tui::run(Arc::new(RwLock::new(Demo::new())))?;
Ok(())
}

View file

@ -1,20 +0,0 @@
use crate::*;
use std::marker::PhantomData;
use ::tek_layout::{*, tek_engine::{Usually, Content, Render, Engine, Thunk}};
use EdnItem::*;
pub type EdnRender<'a, Engine> =
dyn Render<Engine> + Send + Sync + 'a;
pub type EdnCallback<'a, Engine, State> =
dyn Fn(&'a State)->Box<EdnRender<'a, Engine>> + Send + Sync + 'a;
pub type EdnRenderCallback<'a, Engine, State> =
Box<EdnCallback<'a, Engine, State>>;
pub trait EdnLayout<'a, E: Engine + 'a> where Self: 'a {
fn get_bool (&self, _sym: &str) -> bool { false }
fn get_unit (&self, _sym: &str) -> E::Unit { 0.into() }
fn get_usize (&self, _sym: &str) -> usize { 0 }
fn get_content (&'a self, _sym: &str) -> Box<EdnRender<'a, E>> { Box::new(()) }
}

View file

@ -1,22 +1,40 @@
use crate::*; use crate::*;
use std::marker::PhantomData; use std::marker::PhantomData;
use ::tek_layout::{*, tek_engine::{Usually, Content, Render, Engine, Thunk}};
use EdnItem::*;
pub type EdnRender<'a, Engine> =
dyn Render<Engine> + Send + Sync + 'a;
pub type EdnCallback<'a, Engine, State> =
dyn Fn(&'a State)->Box<EdnRender<'a, Engine>> + Send + Sync + 'a;
pub type EdnRenderCallback<'a, Engine, State> =
Box<EdnCallback<'a, Engine, State>>;
pub trait EdnLayout<E: Engine> {
fn get_bool (&self, _sym: &str) -> bool { false }
fn get_unit (&self, _sym: &str) -> E::Unit { 0.into() }
fn get_usize (&self, _sym: &str) -> usize { 0 }
fn get_content <'a> (&'a self, _sym: &str) -> Box<EdnRender<'a, E>> { Box::new(()) }
}
/// Renders from EDN source and context. /// Renders from EDN source and context.
pub struct EdnView<'a, E: Engine + 'a, T: EdnLayout<'a, E> + 'a> { pub struct EdnView<E: Engine, T: EdnLayout<E>> {
_engine: PhantomData<&'a E>, _engine: PhantomData<E>,
context: T, context: T,
layout: EdnItem<String> layout: EdnItem<String>
//render: Box<dyn Fn(&'a T)->Box<dyn Render<E> + Send + Sync + 'a> + Send + Sync + 'a> //render: Box<dyn Fn(&'a T)->Box<dyn Render<E> + Send + Sync + 'a> + Send + Sync + 'a>
} }
impl<'a, E: Engine + 'a, T: EdnLayout<'a, E> + 'a> EdnView<'a, E, T> { impl<E: Engine, T: EdnLayout<E>> EdnView<E, T> {
pub fn new (context: T, source: &'a str) -> Usually<Self> { pub fn new (context: T, source: &str) -> Usually<Self> {
let layout = EdnItem::read_one(&source)?.0; let layout = EdnItem::read_one(&source)?.0;
Ok(Self { _engine: Default::default(), context, layout, }) Ok(Self { _engine: Default::default(), context, layout, })
} }
} }
impl<'a, E: Engine + 'a, T: EdnLayout<'a, E> + Send + Sync + 'a> Content<E> for EdnView<'a, E, T> { impl<E: Engine, T: EdnLayout<E> + Send + Sync> Content<E> for EdnView<E, T> {
fn content (&self) -> impl Render<E> { fn content (&self) -> impl Render<E> {
use EdnItem::*; use EdnItem::*;
match &self.layout { match &self.layout {

View file

@ -11,7 +11,6 @@ pub(crate) use ::tek_layout::{
mod edn_error; pub use self::edn_error::*; mod edn_error; pub use self::edn_error::*;
mod edn_item; pub use self::edn_item::*; mod edn_item; pub use self::edn_item::*;
mod edn_layout; pub use self::edn_layout::*;
mod edn_token; pub use self::edn_token::*; mod edn_token; pub use self::edn_token::*;
mod edn_view; pub use self::edn_view::*; mod edn_view; pub use self::edn_view::*;

View file

@ -27,6 +27,9 @@ impl<E: Engine, C: Content<E>> Render<E> for C {
fn layout (&self, area: E::Area) -> E::Area { Content::layout(self, area) } fn layout (&self, area: E::Area) -> E::Area { Content::layout(self, area) }
fn render (&self, output: &mut E::Output) { Content::render(self, output) } fn render (&self, output: &mut E::Output) { Content::render(self, output) }
} }
impl<'a, E: Engine> Content<E> for Box<dyn Render<E> + 'a> {
fn content (&self) -> impl Render<E> { self }
}
impl<'a, E: Engine> Content<E> for Box<dyn Render<E> + Send + Sync + 'a> { impl<'a, E: Engine> Content<E> for Box<dyn Render<E> + Send + Sync + 'a> {
fn content (&self) -> impl Render<E> { self } fn content (&self) -> impl Render<E> { self }
} }
@ -74,6 +77,7 @@ impl<E: Engine, T: Content<E>> Content<E> for Option<T> {
} }
} }
#[macro_export] macro_rules! render { #[macro_export] macro_rules! render {
(($self:ident:$Struct:ty) => $content:expr) => { (($self:ident:$Struct:ty) => $content:expr) => {
impl <E: Engine> Content<E> for $Struct { impl <E: Engine> Content<E> for $Struct {

View file

@ -92,7 +92,7 @@ impl Tui {
} }
} }
pub trait TuiRun<R: Content<Tui> + Handle<Tui> + Sized + 'static> { pub trait TuiRun<R: Render<Tui> + Handle<Tui> + Sized + 'static> {
/// Run an app in the main loop. /// Run an app in the main loop.
fn run (&self, state: &Arc<RwLock<R>>) -> Usually<()>; fn run (&self, state: &Arc<RwLock<R>>) -> Usually<()>;
/// Spawn the input thread. /// Spawn the input thread.
@ -101,7 +101,7 @@ pub trait TuiRun<R: Content<Tui> + Handle<Tui> + Sized + 'static> {
fn run_output (&self, state: &Arc<RwLock<R>>, sleep: Duration) -> JoinHandle<()>; fn run_output (&self, state: &Arc<RwLock<R>>, sleep: Duration) -> JoinHandle<()>;
} }
impl<T: Content<Tui> + Handle<Tui> + Sized + 'static> TuiRun<T> for Arc<RwLock<Tui>> { impl<T: Render<Tui> + Handle<Tui> + Sized + 'static> TuiRun<T> for Arc<RwLock<Tui>> {
fn run (&self, state: &Arc<RwLock<T>>) -> Usually<()> { fn run (&self, state: &Arc<RwLock<T>>) -> Usually<()> {
let _input_thread = self.run_input(state, Duration::from_millis(100)); let _input_thread = self.run_input(state, Duration::from_millis(100));
self.write().unwrap().setup()?; self.write().unwrap().setup()?;

View file

@ -65,7 +65,7 @@ impl TuiOut {
} }
} }
impl Content<Tui> for &str { impl Render<Tui> for &str {
fn layout (&self, to: [u16;4]) -> [u16;4] { fn layout (&self, to: [u16;4]) -> [u16;4] {
to.center_xy([self.chars().count() as u16, 1]) to.center_xy([self.chars().count() as u16, 1])
} }
@ -74,7 +74,7 @@ impl Content<Tui> for &str {
} }
} }
impl Content<Tui> for String { impl Render<Tui> for String {
fn layout (&self, to: [u16;4]) -> [u16;4] { fn layout (&self, to: [u16;4]) -> [u16;4] {
to.center_xy([self.chars().count() as u16, 1]) to.center_xy([self.chars().count() as u16, 1])
} }