From f1b3fc0040cfce150da98f201c879296047b5fa5 Mon Sep 17 00:00:00 2001 From: unspeaker Date: Sun, 5 Jan 2025 04:48:01 +0100 Subject: [PATCH] wip: more edn rendering setup --- edn/examples/edn01.edn | 2 +- edn/examples/edn01.rs | 20 ++++++++++++++++---- edn/examples/edn02.edn | 13 +------------ edn/examples/edn02.rs | 20 ++++++++++++++++---- edn/src/edn_item.rs | 27 ++++++++++++++++++++------- edn/src/edn_main.rs | 8 ++++++++ edn/src/edn_view.rs | 2 +- edn/src/lib.rs | 30 ++++++++++++++++-------------- engine/src/input.rs | 4 +++- engine/src/output.rs | 6 +++--- 10 files changed, 85 insertions(+), 47 deletions(-) create mode 100644 edn/src/edn_main.rs diff --git a/edn/examples/edn01.edn b/edn/examples/edn01.edn index aa803c29..27e8681e 100644 --- a/edn/examples/edn01.edn +++ b/edn/examples/edn01.edn @@ -1 +1 @@ -:test +:hello diff --git a/edn/examples/edn01.rs b/edn/examples/edn01.rs index 26fe7431..679760f6 100644 --- a/edn/examples/edn01.rs +++ b/edn/examples/edn01.rs @@ -1,8 +1,20 @@ -use tek_edn::*; +use tek_edn::{*, tek_layout::{*, tek_engine::{*, tui::{*, TuiRun}}}}; +use std::sync::{Arc, RwLock}; -const SOURCE: &'static str = include_str!("edn01.edn"); +const EDN: &'static str = include_str!("edn01.edn"); -fn main () { - Tui::run(Arc::new(RwLock::new(Demo::new())))?; +fn main () -> Usually<()> { + let state = Arc::new(RwLock::new(Example)); + Tui::new().unwrap().run(&state)?; Ok(()) } + +pub struct Example; + +impl EdnLayout for Example { + fn get_content <'a> (&'a self, sym: &'a str) -> Box> { + Box::new(Thunk::new(move||if sym == ":hello" { "Hello world!" } else { "" })) + } +} + +impl Handle for Example {} diff --git a/edn/examples/edn02.edn b/edn/examples/edn02.edn index afca85a5..4f352a6d 100644 --- a/edn/examples/edn02.edn +++ b/edn/examples/edn02.edn @@ -1,12 +1 @@ -(sized - (bsp/s (fill/x (fixed/y 2 (lay - (align/w :input-meter-l) - (align/e :input-meter-r) - (align/x :transport)))) - (bsp/n (row :clip-play :clip-next :clip-edit :edit-stat) - (bsp/n (max/y :sample-h (fill/xy :sample-view)) - (bsp/n (align/w (fixed/y 1 :sample-stat)) - (bsp/n (fixed/x :pool-w :pool-view) - (fill/xy (bsp/e - (fixed/x :samples-w (push/y :samples-y :samples-view)) - :midi-view)))))))) +(bsp/s :hello :world) diff --git a/edn/examples/edn02.rs b/edn/examples/edn02.rs index d5e23ee8..0585c4c2 100644 --- a/edn/examples/edn02.rs +++ b/edn/examples/edn02.rs @@ -1,8 +1,20 @@ -use tek_edn::*; +use tek_edn::{*, tek_layout::{*, tek_engine::{*, tui::{*, TuiRun}}}}; +use std::sync::{Arc, RwLock}; -const SOURCE: &'static str = include_str!("edn02.edn"); +const EDN: &'static str = include_str!("edn02.edn"); -fn main () { - Tui::run(Arc::new(RwLock::new(Demo::new())))?; +fn main () -> Usually<()> { + let state = Arc::new(RwLock::new(Example)); + Tui::new().unwrap().run(&state)?; Ok(()) } + +pub struct Example; + +impl EdnLayout for Example { + fn get_content <'a> (&'a self, sym: &'a str) -> Box> { + Box::new(Thunk::new(move||if sym == ":hello" { "Hello world!" } else { "" })) + } +} + +impl Handle for Example {} diff --git a/edn/src/edn_item.rs b/edn/src/edn_item.rs index a2c8e874..d4af7b6b 100644 --- a/edn/src/edn_item.rs +++ b/edn/src/edn_item.rs @@ -14,14 +14,27 @@ impl Default for EdnItem { } impl Debug for EdnItem { fn fmt (&self, f: &mut Formatter<'_>) -> Result<(), FormatError> { + use EdnItem::*; match self { - Self::Nil => write!(f, "Nil"), - Self::Num(u) => write!(f, "Num({u})"), - Self::Sym(u) => write!(f, "Sym({u:?})"), - Self::Key(u) => write!(f, "Key({u:?})"), - Self::Exp(e) => write!(f, "Exp({})", itertools::join( - e.iter().map(|i|format!("{:?}", i)), "," - )) + Nil => write!(f, "Nil"), + Num(u) => write!(f, "Num({u})"), + Sym(u) => write!(f, "Sym({u:?})"), + Key(u) => write!(f, "Key({u:?})"), + Exp(e) => write!(f, "Exp({})", + itertools::join(e.iter().map(|i|format!("{:?}", i)), ",")) + } + } +} +impl PartialEq for EdnItem { + fn eq (&self, other: &Self) -> bool { + use EdnItem::*; + match (self, other) { + (Nil, Nil) => true, + (Num(a), Num(b)) => a == b, + (Sym(a), Sym(b)) => a == b, + (Key(a), Key(b)) => a == b, + (Exp(a), Exp(b)) => a == b, + _ => false } } } diff --git a/edn/src/edn_main.rs b/edn/src/edn_main.rs new file mode 100644 index 00000000..f565d808 --- /dev/null +++ b/edn/src/edn_main.rs @@ -0,0 +1,8 @@ +use crate::*; + +#[macro_export] macro_rules! run_tek_edn { + ($source:expr) => { + struct EdnRunner; + impl EdnLayout for EdnRunner; + } +} diff --git a/edn/src/edn_view.rs b/edn/src/edn_view.rs index aad174e4..ecc50407 100644 --- a/edn/src/edn_view.rs +++ b/edn/src/edn_view.rs @@ -16,7 +16,7 @@ pub trait EdnLayout { 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> { Box::new(()) } + fn get_content <'a> (&'a self, _sym: &'a str) -> Box> { Box::new(()) } } /// Renders from EDN source and context. diff --git a/edn/src/lib.rs b/edn/src/lib.rs index e0e93502..857ab98e 100644 --- a/edn/src/lib.rs +++ b/edn/src/lib.rs @@ -4,40 +4,42 @@ pub(crate) use std::{ fmt::{Debug, Formatter, Error as FormatError} }; +pub use ::tek_layout; pub(crate) use ::tek_layout::{ *, tek_engine::{Usually, Content, Render, Engine, Thunk} }; -mod edn_error; pub use self::edn_error::*; -mod edn_item; pub use self::edn_item::*; -mod edn_token; pub use self::edn_token::*; -mod edn_view; pub use self::edn_view::*; +mod edn_error; pub use self::edn_error::*; +mod edn_item; pub use self::edn_item::*; +mod edn_token; pub use self::edn_token::*; +mod edn_view; pub use self::edn_view::*; +mod edn_main; pub use self::edn_main::*; #[cfg(test)] #[test] fn test_edn () -> Result<(), ParseError> { use EdnItem::*; - assert_eq!(EdnItem::read_all("")?, + assert_eq!(EdnItem::::read_all("")?, vec![]); - assert_eq!(EdnItem::read_all(" ")?, + assert_eq!(EdnItem::::read_all(" ")?, vec![]); - assert_eq!(EdnItem::read_all("1234")?, + assert_eq!(EdnItem::::read_all("1234")?, vec![Num(1234)]); - assert_eq!(EdnItem::read_all("1234 5 67")?, + assert_eq!(EdnItem::::read_all("1234 5 67")?, vec![Num(1234), Num(5), Num(67)]); - assert_eq!(EdnItem::read_all("foo/bar")?, + assert_eq!(EdnItem::::read_all("foo/bar")?, vec![Key("foo/bar".into())]); - assert_eq!(EdnItem::read_all(":symbol")?, + assert_eq!(EdnItem::::read_all(":symbol")?, vec![Sym(":symbol".into())]); - assert_eq!(EdnItem::read_all(" foo/bar :baz 456")?, + assert_eq!(EdnItem::::read_all(" foo/bar :baz 456")?, vec![Key("foo/bar".into()), Sym(":baz".into()), Num(456)]); - assert_eq!(EdnItem::read_all(" (foo/bar :baz 456) ")?, + assert_eq!(EdnItem::::read_all(" (foo/bar :baz 456) ")?, vec![Exp(vec![Key("foo/bar".into()), Sym(":baz".into()), Num(456)])]); Ok(()) } #[cfg(test)] #[test] fn test_edn_layout () -> Result<(), ParseError> { - let source = include_str!("example.edn"); - let layout = EdnItem::read_all(source)?; + EdnItem::::read_all(include_str!("../examples/edn01.edn"))?; + EdnItem::::read_all(include_str!("../examples/edn02.edn"))?; //panic!("{layout:?}"); //let content = >::from(&layout); Ok(()) diff --git a/engine/src/input.rs b/engine/src/input.rs index 6a77db37..0ec9dbf3 100644 --- a/engine/src/input.rs +++ b/engine/src/input.rs @@ -3,7 +3,9 @@ use std::sync::{Arc, Mutex, RwLock}; /// Handle input pub trait Handle: Send + Sync { - fn handle (&mut self, context: &E::Input) -> Perhaps; + fn handle (&mut self, _input: &E::Input) -> Perhaps { + Ok(None) + } } /// Current input state diff --git a/engine/src/output.rs b/engine/src/output.rs index e37d3a47..ed3b9b8c 100644 --- a/engine/src/output.rs +++ b/engine/src/output.rs @@ -36,11 +36,11 @@ impl<'a, E: Engine> Content for Box + Send + Sync + 'a> { impl Content for &(dyn Render + '_) { fn content (&self) -> impl Render { self } } -pub struct Thunk, F: Fn()->T + Send + Sync>(F, PhantomData); -impl, F: Fn()->T + Send + Sync> Thunk { +pub struct Thunk, F: Fn()->T + Send + Sync>(F, PhantomData); +impl, F: Fn()->T + Send + Sync> Thunk { pub fn new (thunk: F) -> Self { Self(thunk, Default::default()) } } -impl, F: Fn()->T + Send + Sync> Content for Thunk { +impl, F: Fn()->T + Send + Sync> Content for Thunk { fn content (&self) -> impl Render { (self.0)() } } impl> Content for &T {