use alloc::sync::Arc;
use core::any::Any;
use mf2_runtime::MarkupPart;
use crate::line::tachys;
use crate::markup::IntoMarkupHandler;
use crate::tr::MarkupHandler;
use tachys::view::any_view::{AnyView, IntoAny};
pub struct NestingHandler(Arc<dyn Fn(AnyView) -> AnyView + Send + Sync>);
impl NestingHandler {
#[must_use]
pub fn call(&self, children: AnyView) -> AnyView {
(self.0)(children)
}
}
impl MarkupHandler for NestingHandler {
fn as_any(&self) -> &dyn Any {
self
}
}
impl core::fmt::Debug for NestingHandler {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
f.write_str("NestingHandler(..)")
}
}
pub struct Flat<F>(pub F);
impl<F> core::fmt::Debug for Flat<F> {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
f.write_str("Flat(..)")
}
}
pub struct FlatHandler(Arc<dyn Fn(&MarkupPart<'_>) -> AnyView + Send + Sync>);
impl FlatHandler {
#[must_use]
pub fn call(&self, part: &MarkupPart<'_>) -> AnyView {
(self.0)(part)
}
}
impl MarkupHandler for FlatHandler {
fn as_any(&self) -> &dyn Any {
self
}
}
impl core::fmt::Debug for FlatHandler {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
f.write_str("FlatHandler(..)")
}
}
impl<F, V> IntoMarkupHandler for F
where
F: Fn(AnyView) -> V + Send + Sync + 'static,
V: IntoAny,
{
fn into_markup_handler(self) -> Arc<dyn MarkupHandler> {
Arc::new(NestingHandler(Arc::new(move |children| {
self(children).into_any()
})))
}
}
impl<F, V> IntoMarkupHandler for Flat<F>
where
F: for<'p> Fn(&MarkupPart<'p>) -> V + Send + Sync + 'static,
V: IntoAny,
{
fn into_markup_handler(self) -> Arc<dyn MarkupHandler> {
let f = self.0;
Arc::new(FlatHandler(Arc::new(move |part| f(part).into_any())))
}
}