mirror of
https://codeberg.org/unspeaker/tek.git
synced 2025-12-06 11:46:41 +01:00
fixed up some parsing and removed some edn mentions
This commit is contained in:
parent
5e7b867aba
commit
452bdf9598
15 changed files with 290 additions and 292 deletions
|
|
@ -45,8 +45,8 @@ impl<E: Output, A: Content<E>> Content<E> for Align<E, A> {
|
|||
to.place(Content::layout(self, to.area()), &self.content())
|
||||
}
|
||||
}
|
||||
impl<'a, E: Output + 'a, T: ViewContext<'a, E>> TryFromEdn<'a, T> for Align<E, RenderBox<'a, E>> {
|
||||
fn try_from_edn (
|
||||
impl<'a, E: Output + 'a, T: ViewContext<'a, E>> TryFromAtom<'a, T> for Align<E, RenderBox<'a, E>> {
|
||||
fn try_from_atom (
|
||||
state: &'a T,
|
||||
head: &impl Atom,
|
||||
tail: &'a [impl Atom]
|
||||
|
|
|
|||
|
|
@ -93,8 +93,8 @@ impl<E: Output, A: Content<E>, B: Content<E>> BspAreas<E, A, B> for Bsp<E, A, B>
|
|||
fn direction (&self) -> Direction { self.1 }
|
||||
fn contents (&self) -> (&A, &B) { (&self.2, &self.3) }
|
||||
}
|
||||
impl<'a, E: Output + 'a, T: ViewContext<'a, E>> TryFromEdn<'a, T> for Bsp<E, RenderBox<'a, E>, RenderBox<'a, E>> {
|
||||
fn try_from_edn (s: &'a T, head: &impl Atom, tail: &'a [impl Atom]) -> Option<Self> {
|
||||
impl<'a, E: Output + 'a, T: ViewContext<'a, E>> TryFromAtom<'a, T> for Bsp<E, RenderBox<'a, E>, RenderBox<'a, E>> {
|
||||
fn try_from_atom (s: &'a T, head: &impl Atom, tail: &'a [impl Atom]) -> Option<Self> {
|
||||
Some(match (head.to_ref(), tail) {
|
||||
(Key("bsp/n"), [a, b]) => Self::n(
|
||||
s.get_content(a).expect("no south"),
|
||||
|
|
|
|||
|
|
@ -7,8 +7,8 @@ impl<E, A> When<E, A> {
|
|||
Self(Default::default(), c, a)
|
||||
}
|
||||
}
|
||||
impl<'a, E: Output + 'a, T: ViewContext<'a, E>> TryFromEdn<'a, T> for When<E, RenderBox<'a, E>> {
|
||||
fn try_from_edn (
|
||||
impl<'a, E: Output + 'a, T: ViewContext<'a, E>> TryFromAtom<'a, T> for When<E, RenderBox<'a, E>> {
|
||||
fn try_from_atom (
|
||||
state: &'a T, head: &impl Atom, tail: &'a [impl Atom]
|
||||
) -> Option<Self> {
|
||||
if let (Key("when"), [condition, content]) = (head.to_ref(), tail) {
|
||||
|
|
@ -47,8 +47,8 @@ impl<E, A, B> Either<E, A, B> {
|
|||
Self(Default::default(), c, a, b)
|
||||
}
|
||||
}
|
||||
impl<'a, E: Output + 'a, T: ViewContext<'a, E>> TryFromEdn<'a, T> for Either<E, RenderBox<'a, E>, RenderBox<'a, E>> {
|
||||
fn try_from_edn (state: &'a T, head: &impl Atom, tail: &'a [impl Atom]) -> Option<Self> {
|
||||
impl<'a, E: Output + 'a, T: ViewContext<'a, E>> TryFromAtom<'a, T> for Either<E, RenderBox<'a, E>, RenderBox<'a, E>> {
|
||||
fn try_from_atom (state: &'a T, head: &impl Atom, tail: &'a [impl Atom]) -> Option<Self> {
|
||||
if let (Key("either"), [condition, content, alternative]) = (head.to_ref(), tail) {
|
||||
Some(Self::new(
|
||||
state.get_bool(condition).expect("either: no condition"),
|
||||
|
|
|
|||
|
|
@ -27,8 +27,8 @@ macro_rules! transform_xy {
|
|||
$area
|
||||
}
|
||||
}
|
||||
impl<'a, E: Output + 'a, T: ViewContext<'a, E>> TryFromEdn<'a, T> for $Enum<E, RenderBox<'a, E>> {
|
||||
fn try_from_edn (
|
||||
impl<'a, E: Output + 'a, T: ViewContext<'a, E>> TryFromAtom<'a, T> for $Enum<E, RenderBox<'a, E>> {
|
||||
fn try_from_atom (
|
||||
state: &'a T, head: &impl Atom, tail: &'a [impl Atom]
|
||||
) -> Option<Self> {
|
||||
Some(match (head.to_ref(), tail) {
|
||||
|
|
@ -82,8 +82,8 @@ macro_rules! transform_xy_unit {
|
|||
$layout.into()
|
||||
}
|
||||
}
|
||||
impl<'a, E: Output + 'a, T: ViewContext<'a, E>> TryFromEdn<'a, T> for $Enum<E, E::Unit, RenderBox<'a, E>> {
|
||||
fn try_from_edn (
|
||||
impl<'a, E: Output + 'a, T: ViewContext<'a, E>> TryFromAtom<'a, T> for $Enum<E, E::Unit, RenderBox<'a, E>> {
|
||||
fn try_from_atom (
|
||||
state: &'a T, head: &impl Atom, tail: &'a [impl Atom]
|
||||
) -> Option<Self> {
|
||||
Some(match (head.to_ref(), tail) {
|
||||
|
|
|
|||
|
|
@ -1,5 +1,6 @@
|
|||
use crate::*;
|
||||
use std::{sync::Arc, marker::PhantomData};
|
||||
use TokenKind::*;
|
||||
/// Define an EDN-backed view.
|
||||
///
|
||||
/// This consists of:
|
||||
|
|
@ -14,8 +15,8 @@ use std::{sync::Arc, marker::PhantomData};
|
|||
}
|
||||
$(
|
||||
impl<'a> Context<'a, $type> for $App {
|
||||
fn get (&'a $self, edn: &'a impl Atom) -> Option<$type> {
|
||||
Some(match edn.to_ref() { $(Atom::Sym($sym) => $value,)* _ => return None })
|
||||
fn get (&'a $self, atom: &'a impl Atom) -> Option<$type> {
|
||||
Some(match atom.to_ref() { $(Atom::Sym($sym) => $value,)* _ => return None })
|
||||
}
|
||||
}
|
||||
)*
|
||||
|
|
@ -25,8 +26,8 @@ use std::{sync::Arc, marker::PhantomData};
|
|||
#[macro_export] macro_rules! provide_content {
|
||||
(|$self:ident:$State:ty|{ $($pat:pat => $expr:expr),* $(,)? }) => {
|
||||
impl<'a, E: Output> Context<'a, Box<dyn Render<E> + 'a>> for $State {
|
||||
fn get (&'a $self, edn: &'a impl Atom) -> Option<Box<dyn Render<E> + 'a>> {
|
||||
Some(match edn.to_ref() {
|
||||
fn get (&'a $self, atom: &'a impl Atom) -> Option<Box<dyn Render<E> + 'a>> {
|
||||
Some(match atom.to_ref() {
|
||||
$(Atom::Sym($pat) => $expr),*,
|
||||
_ => return None
|
||||
})
|
||||
|
|
@ -35,8 +36,8 @@ use std::{sync::Arc, marker::PhantomData};
|
|||
};
|
||||
($Output:ty: |$self:ident:$State:ty|{ $($pat:pat => $expr:expr),* $(,)? }) => {
|
||||
impl<'a> Context<'a, Box<dyn Render<$Output> + 'a>> for $State {
|
||||
fn get (&'a $self, edn: &'a impl Atom) -> Option<Box<dyn Render<$Output> + 'a>> {
|
||||
Some(match edn.to_ref() {
|
||||
fn get (&'a $self, atom: &'a impl Atom) -> Option<Box<dyn Render<$Output> + 'a>> {
|
||||
Some(match atom.to_ref() {
|
||||
$(Atom::Sym($pat) => $expr),*,
|
||||
_ => return None
|
||||
})
|
||||
|
|
@ -45,22 +46,22 @@ use std::{sync::Arc, marker::PhantomData};
|
|||
}
|
||||
}
|
||||
/// Renders from EDN source and context.
|
||||
#[derive(Default)] pub enum EdnView<'a, E: Output, T: ViewContext<'a, E> + std::fmt::Debug> {
|
||||
#[derive(Default)] pub enum AtomView<'a, E: Output, T: ViewContext<'a, E> + std::fmt::Debug> {
|
||||
#[default] Inert,
|
||||
Ok(T, Atom<Arc<str>>),
|
||||
//render: Box<dyn Fn(&'a T)->Box<dyn Render<E> + Send + Sync + 'a> + Send + Sync + 'a>
|
||||
Err(String),
|
||||
_Unused(PhantomData<&'a E>),
|
||||
}
|
||||
impl<'a, E: Output, T: ViewContext<'a, E> + std::fmt::Debug> std::fmt::Debug for EdnView<'a, E, T> {
|
||||
impl<'a, E: Output, T: ViewContext<'a, E> + std::fmt::Debug> std::fmt::Debug for AtomView<'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"),
|
||||
write!(f, "AtomView::Inert"),
|
||||
Self::Ok(state, view) =>
|
||||
write!(f, "EdnView::Ok(state={state:?} view={view:?}"),
|
||||
write!(f, "AtomView::Ok(state={state:?} view={view:?}"),
|
||||
Self::Err(error) =>
|
||||
write!(f, "EdnView::Err({error})"),
|
||||
write!(f, "AtomView::Err({error})"),
|
||||
_ => unreachable!()
|
||||
}
|
||||
}
|
||||
|
|
@ -71,40 +72,29 @@ impl<'a, E: Output, T> ViewContext<'a, E> for T where T:
|
|||
Context<'a, E::Unit> +
|
||||
Context<'a, Box<dyn Render<E> + 'a>>
|
||||
{}
|
||||
impl<'a, E: Output, T: ViewContext<'a, E> + std::fmt::Debug> EdnView<'a, E, T> {
|
||||
impl<'a, E: Output, T: ViewContext<'a, E> + std::fmt::Debug> AtomView<'a, E, T> {
|
||||
pub fn from_source (state: T, source: &'a str) -> Self {
|
||||
match Atom::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<impl Atom>) -> Self {
|
||||
Self::Ok(state, Atom::Exp(items).to_arc())
|
||||
pub fn from_atoms (state: T, atoms: Vec<impl Atom>) -> Self {
|
||||
Self::Ok(state, Atom::Exp(atoms).to_arc())
|
||||
}
|
||||
}
|
||||
impl<E: Output, T: for<'a>ViewContext<'a, E> + Send + Sync + std::fmt::Debug> Content<E> for EdnView<'_, E, T> {
|
||||
impl<E: Output, T: for<'a>ViewContext<'a, E> + Send + Sync + std::fmt::Debug> Content<E> for AtomView<'_, 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:&format!("AtomView error: {error:?}")) // FIXME: String is not Render
|
||||
},
|
||||
_ => todo!()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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 ViewContext<'a, E: Output>:
|
||||
Context<'a, bool> +
|
||||
|
|
@ -112,52 +102,53 @@ pub trait ViewContext<'a, E: Output>:
|
|||
Context<'a, E::Unit> +
|
||||
Context<'a, Box<dyn Render<E> + 'a>>
|
||||
{
|
||||
fn get_bool (&'a self, item: &'a impl Atom) -> Option<bool> {
|
||||
Some(match &item {
|
||||
Sym(s) => match s.as_ref() {
|
||||
fn get_bool (&'a self, atom: &'a impl Atom) -> Option<bool> {
|
||||
Some(match atom.kind() {
|
||||
Sym => match atom.text().as_ref() {
|
||||
":false" | ":f" => false,
|
||||
":true" | ":t" => true,
|
||||
_ => return Context::get(self, item)
|
||||
_ => return Context::get(self, atom)
|
||||
},
|
||||
Num(0) => false,
|
||||
Num(_) => true,
|
||||
_ => return Context::get(self, item)
|
||||
Num => match atom.num() {
|
||||
0 => false,
|
||||
_ => true
|
||||
},
|
||||
_ => return Context::get(self, atom)
|
||||
})
|
||||
}
|
||||
fn get_usize (&'a self, item: &'a impl Atom) -> Option<usize> {
|
||||
Some(match &item { Num(n) => *n, _ => return Context::get(self, item) })
|
||||
}
|
||||
fn get_unit (&'a self, item: &'a impl Atom) -> Option<E::Unit> {
|
||||
Some(match &item { Num(n) => (*n as u16).into(), _ => return Context::get(self, item) })
|
||||
}
|
||||
fn get_content (&'a self, item: &'a impl Atom) -> Option<Box<dyn Render<E> + 'a>> where E: 'a {
|
||||
Some(match item {
|
||||
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>>);
|
||||
Context::get_or_fail(self, &item)
|
||||
},
|
||||
_ => Context::get_or_fail(self, &item)
|
||||
fn get_usize (&'a self, atom: &'a impl Atom) -> Option<usize> {
|
||||
Some(match atom.kind() {
|
||||
Num => atom.num(),
|
||||
_ => return Context::get(self, atom)
|
||||
})
|
||||
//panic!("no content")
|
||||
}
|
||||
fn get_unit (&'a self, atom: &'a impl Atom) -> Option<E::Unit> {
|
||||
Some(match atom.kind() {
|
||||
Num => E::Unit::from(atom.num() as u16),
|
||||
_ => return Context::get(self, atom)
|
||||
})
|
||||
}
|
||||
fn get_content (&'a self, atom: &'a impl Atom) -> Option<Box<dyn Render<E> + 'a>> where E: 'a {
|
||||
try_delegate!(self, atom, When::<_, RenderBox<'a, E>>);
|
||||
try_delegate!(self, atom, Either::<_, RenderBox<'a, E>, RenderBox<'a, E>>);
|
||||
try_delegate!(self, atom, Align::<_, RenderBox<'a, E>>);
|
||||
try_delegate!(self, atom, Bsp::<_, RenderBox<'a, E>, RenderBox<'a, E>>);
|
||||
try_delegate!(self, atom, Fill::<_, RenderBox<'a, E>>);
|
||||
try_delegate!(self, atom, Fixed::<_, _, RenderBox<'a, E>>);
|
||||
try_delegate!(self, atom, Min::<_, _, RenderBox<'a, E>>);
|
||||
try_delegate!(self, atom, Max::<_, _, RenderBox<'a, E>>);
|
||||
try_delegate!(self, atom, Shrink::<_, _, RenderBox<'a, E>>);
|
||||
try_delegate!(self, atom, Expand::<_, _, RenderBox<'a, E>>);
|
||||
try_delegate!(self, atom, Push::<_, _, RenderBox<'a, E>>);
|
||||
try_delegate!(self, atom, Pull::<_, _, RenderBox<'a, E>>);
|
||||
try_delegate!(self, atom, Margin::<_, _, RenderBox<'a, E>>);
|
||||
try_delegate!(self, atom, Padding::<_, _, RenderBox<'a, E>>);
|
||||
Context::get_or_fail(self, atom)
|
||||
}
|
||||
}
|
||||
// A function that returns a `RenderBox.
|
||||
pub type EdnCallback<'a, O, State> =
|
||||
pub type AtomCallback<'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>>;
|
||||
pub type AtomRenderCallback<'a, O, State> =
|
||||
Box<AtomCallback<'a, O, State>>;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue