use crate::prelude::*;
use std::fmt;
use std::sync::Arc;
#[derive(Clone)]
pub struct Html(Arc<dyn Fn(&mut Context) -> Markup + Send + Sync>);
impl fmt::Debug for Html {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_tuple("Html")
.field(&"Fn(&mut Context) -> Markup")
.finish()
}
}
impl Default for Html {
fn default() -> Self {
Self::with(|_| html! {})
}
}
impl Component for Html {
fn new() -> Self {
Self::default()
}
fn prepare(&self, cx: &mut Context) -> Result<Markup, ComponentError> {
Ok(self.html(cx))
}
}
impl Html {
pub fn with<F>(f: F) -> Self
where
F: Fn(&mut Context) -> Markup + Send + Sync + 'static,
{
Html(Arc::new(f))
}
#[builder_fn]
pub fn with_fn<F>(mut self, f: F) -> Self
where
F: Fn(&mut Context) -> Markup + Send + Sync + 'static,
{
self.0 = Arc::new(f);
self
}
pub fn html(&self, cx: &mut Context) -> Markup {
(self.0)(cx)
}
}