use oco_ref::Oco;
use std::sync::Arc;
use tachys::prelude::IntoAttributeValue;
#[derive(Clone)]
pub struct TextProp(Arc<dyn Fn() -> Oco<'static, str> + Send + Sync>);
impl TextProp {
#[inline(always)]
pub fn get(&self) -> Oco<'static, str> {
(self.0)()
}
}
impl core::fmt::Debug for TextProp {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
f.debug_tuple("TextProp").finish()
}
}
impl From<String> for TextProp {
fn from(s: String) -> Self {
let s: Oco<'_, str> = Oco::Counted(Arc::from(s));
TextProp(Arc::new(move || s.clone()))
}
}
impl From<&'static str> for TextProp {
fn from(s: &'static str) -> Self {
let s: Oco<'_, str> = s.into();
TextProp(Arc::new(move || s.clone()))
}
}
impl From<Arc<str>> for TextProp {
fn from(s: Arc<str>) -> Self {
let s: Oco<'_, str> = s.into();
TextProp(Arc::new(move || s.clone()))
}
}
impl From<Oco<'static, str>> for TextProp {
fn from(s: Oco<'static, str>) -> Self {
TextProp(Arc::new(move || s.clone()))
}
}
impl<F, S> From<F> for TextProp
where
F: Fn() -> S + 'static + Send + Sync,
S: Into<Oco<'static, str>>,
{
#[inline(always)]
fn from(s: F) -> Self {
TextProp(Arc::new(move || s().into()))
}
}
impl Default for TextProp {
fn default() -> Self {
Self(Arc::new(|| Oco::Borrowed("")))
}
}
impl IntoAttributeValue for TextProp {
type Output = Oco<'static, str>;
fn into_attribute_value(self) -> Self::Output {
self.get()
}
}