1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
use std::rc::Rc;

use leptos_reactive::Scope;

#[derive(Clone)]
pub enum Attribute {
    String(String),
    Fn(Rc<dyn Fn() -> Attribute>),
    Option(Option<String>),
    Bool(bool),
}

impl Attribute {
    pub fn as_value_string(&self, attr_name: &'static str) -> String {
        match self {
            Attribute::String(value) => format!("{attr_name}=\"{value}\""),
            Attribute::Fn(f) => {
                let mut value = f();
                while let Attribute::Fn(f) = value {
                    value = f();
                }
                value.as_value_string(attr_name)
            }
            Attribute::Option(value) => value
                .as_ref()
                .map(|value| format!("{attr_name}=\"{value}\""))
                .unwrap_or_default(),
            Attribute::Bool(include) => {
                if *include {
                    attr_name.to_string()
                } else {
                    String::new()
                }
            }
        }
    }
}

impl PartialEq for Attribute {
    fn eq(&self, other: &Self) -> bool {
        match (self, other) {
            (Self::String(l0), Self::String(r0)) => l0 == r0,
            (Self::Fn(_), Self::Fn(_)) => false,
            (Self::Option(l0), Self::Option(r0)) => l0 == r0,
            (Self::Bool(l0), Self::Bool(r0)) => l0 == r0,
            _ => false,
        }
    }
}

impl std::fmt::Debug for Attribute {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::String(arg0) => f.debug_tuple("String").field(arg0).finish(),
            Self::Fn(_) => f.debug_tuple("Fn").finish(),
            Self::Option(arg0) => f.debug_tuple("Option").field(arg0).finish(),
            Self::Bool(arg0) => f.debug_tuple("Bool").field(arg0).finish(),
        }
    }
}

pub trait IntoAttribute {
    fn into_attribute(self, cx: Scope) -> Attribute;
}

impl IntoAttribute for String {
    fn into_attribute(self, _cx: Scope) -> Attribute {
        Attribute::String(self)
    }
}

impl IntoAttribute for bool {
    fn into_attribute(self, _cx: Scope) -> Attribute {
        Attribute::Bool(self)
    }
}

impl IntoAttribute for Option<String> {
    fn into_attribute(self, _cx: Scope) -> Attribute {
        Attribute::Option(self)
    }
}

impl<T, U> IntoAttribute for T
where
    T: Fn() -> U + 'static,
    U: IntoAttribute,
{
    fn into_attribute(self, cx: Scope) -> Attribute {
        let modified_fn = Rc::new(move || (self)().into_attribute(cx));
        Attribute::Fn(modified_fn)
    }
}

macro_rules! attr_type {
    ($attr_type:ty) => {
        impl IntoAttribute for $attr_type {
            fn into_attribute(self, _cx: Scope) -> Attribute {
                Attribute::String(self.to_string())
            }
        }

        impl IntoAttribute for Option<$attr_type> {
            fn into_attribute(self, _cx: Scope) -> Attribute {
                Attribute::Option(self.map(|n| n.to_string()))
            }
        }
    };
}

attr_type!(&String);
attr_type!(&str);
attr_type!(usize);
attr_type!(u8);
attr_type!(u16);
attr_type!(u32);
attr_type!(u64);
attr_type!(u128);
attr_type!(isize);
attr_type!(i8);
attr_type!(i16);
attr_type!(i32);
attr_type!(i64);
attr_type!(i128);
attr_type!(f32);
attr_type!(f64);
attr_type!(char);