use crate::{
html::attributes::{Style, Value},
vdom::Listener,
};
use std::fmt::{self, Debug};
pub enum AttributeValue<MSG> {
FunctionCall(Value),
Simple(Value),
Style(Vec<Style>),
EventListener(Listener<MSG>),
Empty,
}
impl<MSG> Clone for AttributeValue<MSG> {
fn clone(&self) -> Self {
match self {
AttributeValue::FunctionCall(this) => AttributeValue::FunctionCall(this.clone()),
AttributeValue::Simple(this) => AttributeValue::Simple(this.clone()),
AttributeValue::Style(this) => AttributeValue::Style(this.clone()),
AttributeValue::EventListener(this) => AttributeValue::EventListener(this.clone()),
AttributeValue::Empty => AttributeValue::Empty,
}
}
}
impl<MSG> Debug for AttributeValue<MSG> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
AttributeValue::FunctionCall(this) => this.fmt(f),
AttributeValue::Simple(this) => this.fmt(f),
AttributeValue::Style(this) => this.fmt(f),
AttributeValue::EventListener(this) => this.fmt(f),
AttributeValue::Empty => write!(f, "Empty"),
}
}
}
impl<MSG> PartialEq for AttributeValue<MSG> {
fn eq(&self, other: &Self) -> bool {
match (self, other) {
(AttributeValue::FunctionCall(this), AttributeValue::FunctionCall(other)) => {
this == other
}
(AttributeValue::Simple(this), AttributeValue::Simple(other)) => this == other,
(AttributeValue::Style(this), AttributeValue::Style(other)) => this == other,
(AttributeValue::EventListener(this), AttributeValue::EventListener(other)) => {
this == other
}
(AttributeValue::Empty, AttributeValue::Empty) => true,
(_, _) => false,
}
}
}
impl<MSG> From<Listener<MSG>> for AttributeValue<MSG> {
fn from(listener: Listener<MSG>) -> Self {
Self::EventListener(listener)
}
}
impl<MSG, V> From<V> for AttributeValue<MSG>
where
V: Into<Value>,
{
fn from(v: V) -> Self {
Self::Simple(Into::<Value>::into(v))
}
}
impl<MSG> AttributeValue<MSG> {
pub fn from_styles(styles: impl IntoIterator<Item = Style>) -> Self {
Self::Style(styles.into_iter().collect())
}
pub fn function_call(value: Value) -> Self {
Self::FunctionCall(value)
}
pub fn get_simple(&self) -> Option<&Value> {
match self {
Self::Simple(v) => Some(v),
_ => None,
}
}
pub fn get_function_call_value(&self) -> Option<&Value> {
match self {
Self::FunctionCall(v) => Some(v),
_ => None,
}
}
pub fn is_style(&self) -> bool {
matches!(self, Self::Style(_))
}
pub fn as_event_listener(&self) -> Option<&Listener<MSG>> {
match self {
Self::EventListener(cb) => Some(cb),
_ => None,
}
}
pub fn as_style(&self) -> Option<&Vec<Style>> {
match self {
Self::Style(styles) => Some(styles),
_ => None,
}
}
pub fn is_function_call(&self) -> bool {
matches!(self, Self::FunctionCall(_))
}
pub fn is_empty(&self) -> bool {
matches!(self, Self::Empty)
}
}