once again, why did i begin to refactor this

This commit is contained in:
🪞👃🪞 2025-01-18 00:13:36 +01:00
parent 297f9b30df
commit 798de37172
15 changed files with 582 additions and 602 deletions

View file

@ -1,4 +1,5 @@
use crate::*;
use RefAtom::*;
#[derive(Debug, Copy, Clone, Default)] pub enum Alignment { #[default] Center, X, Y, NW, N, NE, E, SE, S, SW, W }
pub struct Align<E, A>(PhantomData<E>, Alignment, A);
impl<E, A> Align<E, A> {
@ -44,13 +45,12 @@ 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: EdnViewData<'a, E>> TryFromEdn<'a, T> for Align<E, RenderBox<'a, E>> {
impl<'a, E: Output + 'a, T: ViewContext<'a, E>> TryFromEdn<'a, T> for Align<E, RenderBox<'a, E>> {
fn try_from_edn (
state: &'a T,
head: &Atom<impl AsRef<str>>,
tail: &'a [Atom<impl AsRef<str>>]
head: &impl Atom,
tail: &'a [impl Atom]
) -> Option<Self> {
use Atom::*;
Some(match (head.to_ref(), tail) {
(Key("align/c"), [a]) => Self::c(state.get_content(a).expect("no content")),
(Key("align/x"), [a]) => Self::x(state.get_content(a).expect("no content")),

View file

@ -1,4 +1,5 @@
use crate::*;
use RefAtom::*;
pub use self::Direction::*;
/// A cardinal direction.
#[derive(Copy, Clone, PartialEq)]
@ -92,9 +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: EdnViewData<'a, E>> TryFromEdn<'a, T> for Bsp<E, RenderBox<'a, E>, RenderBox<'a, E>> {
fn try_from_edn (s: &'a T, head: &Atom<impl AsRef<str>>, tail: &'a [Atom<impl AsRef<str>>]) -> Option<Self> {
use Atom::*;
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> {
Some(match (head.to_ref(), tail) {
(Key("bsp/n"), [a, b]) => Self::n(
s.get_content(a).expect("no south"),

View file

@ -1,4 +1,5 @@
use crate::*;
use RefAtom::*;
/// Show an item only when a condition is true.
pub struct When<E, A>(pub PhantomData<E>, pub bool, pub A);
impl<E, A> When<E, A> {
@ -6,11 +7,10 @@ impl<E, A> When<E, A> {
Self(Default::default(), c, a)
}
}
impl<'a, E: Output + 'a, T: EdnViewData<'a, E>> TryFromEdn<'a, T> for When<E, RenderBox<'a, E>> {
impl<'a, E: Output + 'a, T: ViewContext<'a, E>> TryFromEdn<'a, T> for When<E, RenderBox<'a, E>> {
fn try_from_edn (
state: &'a T, head: &Atom<impl AsRef<str>>, tail: &'a [Atom<impl AsRef<str>>]
state: &'a T, head: &impl Atom, tail: &'a [impl Atom]
) -> Option<Self> {
use Atom::*;
if let (Key("when"), [condition, content]) = (head.to_ref(), tail) {
Some(Self(
Default::default(),
@ -47,9 +47,8 @@ impl<E, A, B> Either<E, A, B> {
Self(Default::default(), c, a, b)
}
}
impl<'a, E: Output + 'a, T: EdnViewData<'a, E>> TryFromEdn<'a, T> for Either<E, RenderBox<'a, E>, RenderBox<'a, E>> {
fn try_from_edn (state: &'a T, head: &Atom<impl AsRef<str>>, tail: &'a [Atom<impl AsRef<str>>]) -> Option<Self> {
use Atom::*;
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> {
if let (Key("either"), [condition, content, alternative]) = (head.to_ref(), tail) {
Some(Self::new(
state.get_bool(condition).expect("either: no condition"),

View file

@ -1,5 +1,5 @@
use crate::*;
use RefAtom::*;
pub fn map_south<O: Output>(
item_offset: O::Unit,
item_height: O::Unit,

View file

@ -1,4 +1,5 @@
use crate::*;
use RefAtom::*;
/// Defines an enum that transforms its content
/// along either the X axis, the Y axis, or both.
///
@ -26,11 +27,10 @@ macro_rules! transform_xy {
$area
}
}
impl<'a, E: Output + 'a, T: EdnViewData<'a, E>> TryFromEdn<'a, T> for $Enum<E, RenderBox<'a, E>> {
impl<'a, E: Output + 'a, T: ViewContext<'a, E>> TryFromEdn<'a, T> for $Enum<E, RenderBox<'a, E>> {
fn try_from_edn (
state: &'a T, head: &Atom<impl AsRef<str>>, tail: &'a [Atom<impl AsRef<str>>]
state: &'a T, head: &impl Atom, tail: &'a [impl Atom]
) -> Option<Self> {
use Atom::*;
Some(match (head.to_ref(), tail) {
(Key($x), [a]) => Self::x(state.get_content(a).expect("no content")),
(Key($y), [a]) => Self::y(state.get_content(a).expect("no content")),
@ -82,11 +82,10 @@ macro_rules! transform_xy_unit {
$layout.into()
}
}
impl<'a, E: Output + 'a, T: EdnViewData<'a, E>> TryFromEdn<'a, T> for $Enum<E, E::Unit, RenderBox<'a, E>> {
impl<'a, E: Output + 'a, T: ViewContext<'a, E>> TryFromEdn<'a, T> for $Enum<E, E::Unit, RenderBox<'a, E>> {
fn try_from_edn (
state: &'a T, head: &Atom<impl AsRef<str>>, tail: &'a [Atom<impl AsRef<str>>]
state: &'a T, head: &impl Atom, tail: &'a [impl Atom]
) -> Option<Self> {
use Atom::*;
Some(match (head.to_ref(), tail) {
(Key($x), [x, a]) => Self::x(
state.get_unit(x).expect("no x"),

View file

@ -1,11 +1,10 @@
use crate::*;
use std::{sync::Arc, marker::PhantomData};
use Atom::*;
/// Define an EDN-backed view.
///
/// This consists of:
/// * render callback (implementation of [Content])
/// * value providers (implementations of [EdnProvide])
/// * value providers (implementations of [Context])
#[macro_export] macro_rules! edn_view {
($Output:ty: |$self:ident: $App:ty| $content:expr; {
$( $type:ty { $($sym:literal => $value:expr),* } );*
@ -14,19 +13,19 @@ use Atom::*;
fn content(&$self) -> impl Render<$Output> { $content }
}
$(
impl<'a> EdnProvide<'a, $type> for $App {
fn get (&'a $self, edn: &'a Atom<impl AsRef<str>>) -> Option<$type> {
impl<'a> Context<'a, $type> for $App {
fn get (&'a $self, edn: &'a impl Atom<'a>) -> Option<$type> {
Some(match edn.to_ref() { $(Atom::Sym($sym) => $value,)* _ => return None })
}
}
)*
}
}
/// Implements `EdnProvide` for content components and expressions
/// Implements `Context` for content components and expressions
#[macro_export] macro_rules! edn_provide_content {
(|$self:ident:$State:ty|{ $($pat:pat => $expr:expr),* $(,)? }) => {
impl<'a, E: Output> EdnProvide<'a, Box<dyn Render<E> + 'a>> for $State {
fn get (&'a $self, edn: &'a Atom<impl AsRef<str>>) -> Option<Box<dyn Render<E> + 'a>> {
impl<'a, E: Output> Context<'a, Box<dyn Render<E> + 'a>> for $State {
fn get (&'a $self, edn: &'a impl Atom<'a>) -> Option<Box<dyn Render<E> + 'a>> {
Some(match edn.to_ref() {
$(Atom::Sym($pat) => $expr),*,
_ => return None
@ -35,8 +34,8 @@ use Atom::*;
}
};
($Output:ty: |$self:ident:$State:ty|{ $($pat:pat => $expr:expr),* $(,)? }) => {
impl<'a> EdnProvide<'a, Box<dyn Render<$Output> + 'a>> for $State {
fn get (&'a $self, edn: &'a Atom<impl AsRef<str>>) -> Option<Box<dyn Render<$Output> + 'a>> {
impl<'a> Context<'a, Box<dyn Render<$Output> + 'a>> for $State {
fn get (&'a $self, edn: &'a impl Atom<'a>) -> Option<Box<dyn Render<$Output> + 'a>> {
Some(match edn.to_ref() {
$(Atom::Sym($pat) => $expr),*,
_ => return None
@ -46,14 +45,14 @@ use Atom::*;
}
}
/// Renders from EDN source and context.
#[derive(Default)] pub enum EdnView<'a, E: Output, T: EdnViewData<'a, E> + std::fmt::Debug> {
#[derive(Default)] pub enum EdnView<'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: EdnViewData<'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 EdnView<'a, E, T> {
fn fmt (&self, f: &mut std::fmt::Formatter<'_>) -> Result<(), std::fmt::Error> {
match self {
Self::Inert | Self::_Unused(_) =>
@ -66,24 +65,24 @@ impl<'a, E: Output, T: EdnViewData<'a, E> + std::fmt::Debug> std::fmt::Debug for
}
}
}
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> ViewContext<'a, E> for T where T:
Context<'a, bool> +
Context<'a, usize> +
Context<'a, E::Unit> +
Context<'a, Box<dyn Render<E> + 'a>>
{}
impl<'a, E: Output, T: EdnViewData<'a, E> + std::fmt::Debug> EdnView<'a, E, T> {
impl<'a, E: Output, T: ViewContext<'a, E> + std::fmt::Debug> EdnView<'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<Atom<impl AsRef<str>>>) -> Self {
pub fn from_items (state: T, items: Vec<impl Atom<'a>>) -> Self {
Self::Ok(state, Atom::Exp(items).to_arc())
}
}
impl<E: Output, T: for<'a>EdnViewData<'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 EdnView<'_, E, T> {
fn content (&self) -> impl Render<E> {
match self {
Self::Ok(state, layout) => state.get_content(layout),
@ -107,31 +106,31 @@ macro_rules! edn_try_delegate {
}
/// Provides values to the template
pub trait EdnViewData<'a, E: Output>:
EdnProvide<'a, bool> +
EdnProvide<'a, usize> +
EdnProvide<'a, E::Unit> +
EdnProvide<'a, Box<dyn Render<E> + 'a>>
pub trait ViewContext<'a, E: Output>:
Context<'a, bool> +
Context<'a, usize> +
Context<'a, E::Unit> +
Context<'a, Box<dyn Render<E> + 'a>>
{
fn get_bool (&'a self, item: &'a Atom<impl AsRef<str>>) -> Option<bool> {
fn get_bool (&'a self, item: &'a impl Atom<'a>) -> Option<bool> {
Some(match &item {
Sym(s) => match s.as_ref() {
":false" | ":f" => false,
":true" | ":t" => true,
_ => return EdnProvide::get(self, item)
_ => return Context::get(self, item)
},
Num(0) => false,
Num(_) => true,
_ => return EdnProvide::get(self, item)
_ => return Context::get(self, item)
})
}
fn get_usize (&'a self, item: &'a Atom<impl AsRef<str>>) -> Option<usize> {
Some(match &item { Num(n) => *n, _ => return EdnProvide::get(self, item) })
fn get_usize (&'a self, item: &'a impl Atom<'a>) -> Option<usize> {
Some(match &item { Num(n) => *n, _ => return Context::get(self, item) })
}
fn get_unit (&'a self, item: &'a Atom<impl AsRef<str>>) -> Option<E::Unit> {
Some(match &item { Num(n) => (*n as u16).into(), _ => return EdnProvide::get(self, item) })
fn get_unit (&'a self, item: &'a impl Atom<'a>) -> Option<E::Unit> {
Some(match &item { Num(n) => (*n as u16).into(), _ => return Context::get(self, item) })
}
fn get_content (&'a self, item: &'a Atom<impl AsRef<str>>) -> Option<Box<dyn Render<E> + 'a>> where E: 'a {
fn get_content (&'a self, item: &'a impl Atom<'a>) -> Option<Box<dyn Render<E> + 'a>> where E: 'a {
Some(match item {
Nil => Box::new(()),
Exp(ref e) => {
@ -149,9 +148,9 @@ pub trait EdnViewData<'a, E: Output>:
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>>);
EdnProvide::get_or_fail(self, &item)
Context::get_or_fail(self, &item)
},
_ => EdnProvide::get_or_fail(self, &item)
_ => Context::get_or_fail(self, &item)
})
//panic!("no content")
}