use crate::prelude::*;
use beet_core::prelude::*;
use std::hash::Hash;
pub type LangDirectives = (
StyleScope,
StyleCascade,
ScriptElement,
StyleElement,
CodeNode,
LangSnippetHash,
InnerText,
FileInnerText,
);
#[derive(
Debug, Copy, Clone, PartialEq, Eq, Hash, Deref, Component, Reflect,
)]
#[reflect(Component)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(feature = "tokens", derive(ToTokens))]
#[component(immutable)]
pub struct LangSnippetHash(pub u64);
impl LangSnippetHash {
pub fn new(hash: u64) -> Self { Self(hash) }
}
impl std::fmt::Display for LangSnippetHash {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.0)
}
}
#[derive(Debug, Clone, PartialEq, Hash, Component, Reflect)]
#[reflect(Component)]
#[component(immutable)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(feature = "tokens", derive(ToTokens))]
pub struct ScriptElement;
#[derive(Debug, Clone, PartialEq, Hash, Component, Reflect)]
#[reflect(Component)]
#[component(immutable)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(feature = "tokens", derive(ToTokens))]
pub struct StyleElement;
#[derive(
Debug, Default, Clone, PartialEq, Hash, Deref, DerefMut, Component, Reflect,
)]
#[reflect(Component)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(feature = "tokens", derive(ToTokens))]
#[component(on_add= on_add)]
pub struct InnerText(pub String);
fn on_add(mut world: DeferredWorld, cx: HookContext) {
world
.commands()
.entity(cx.entity)
.insert(ElementNode::open());
}
#[derive(Debug, Clone, PartialEq, Hash, Component, Reflect)]
#[reflect(Component)]
#[component(immutable)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct FileInnerText {
pub path: String,
}
impl FileInnerText {
pub fn new(path: impl Into<String>) -> Self { Self { path: path.into() } }
}
#[cfg(feature = "tokens")]
impl TokenizeSelf for FileInnerText {
fn self_tokens(&self, tokens: &mut proc_macro2::TokenStream) {
let path = &self.path;
tokens.extend(quote::quote! {
InnerText::new(include_str!(#path))
});
}
}
impl InnerText {
pub fn new(text: impl Into<String>) -> Self { Self(text.into()) }
}