use crate::{Context, Event, Html, LiveResult};
use std::fmt;
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct ComponentId(String);
impl ComponentId {
pub fn new(id: impl Into<String>) -> Self {
Self(id.into())
}
pub fn as_str(&self) -> &str {
&self.0
}
}
impl fmt::Display for ComponentId {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(&self.0)
}
}
impl From<&str> for ComponentId {
fn from(value: &str) -> Self {
Self::new(value)
}
}
impl From<String> for ComponentId {
fn from(value: String) -> Self {
Self::new(value)
}
}
#[derive(Debug, Clone, PartialEq)]
pub struct ComponentRender {
id: ComponentId,
html: Html,
}
impl ComponentRender {
pub fn new(id: impl Into<ComponentId>, html: Html) -> Self {
Self {
id: id.into(),
html,
}
}
pub fn id(&self) -> &ComponentId {
&self.id
}
pub fn html(&self) -> &Html {
&self.html
}
pub fn into_parts(self) -> (ComponentId, Html) {
(self.id, self.html)
}
}
pub trait LiveComponent: Send + 'static {
fn id(&self) -> ComponentId;
fn mount(&mut self, _ctx: &mut Context) -> LiveResult {
Ok(())
}
fn handle_event(&mut self, _event: Event, _ctx: &mut Context) -> LiveResult {
Ok(())
}
fn render(&self) -> Html;
}