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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
use std::fmt::Write;

use crate::{TagOpening, Empty, Sum};


/// Represents a list of attributes.
pub trait Attributes {
    /// Writes the attributes to `w`.
    ///
    /// # Arguments
    /// * `w` - The tag opening to write to.
    fn write_attributes<'a, 't, W: Write>(self, w: &mut TagOpening<'a, 't, W>) -> std::fmt::Result;
} // TODO TagOpening into trait and rename to TagOpening struct into something else

impl<A: Attributes, B: Attributes> Attributes for Sum<A, B> {
    fn write_attributes<'a, 't, W: Write>(self, w: &mut TagOpening<'a, 't, W>) -> std::fmt::Result {
        self.0.write_attributes(w)?;
        self.1.write_attributes(w)?;
        Ok(())
    }
}

impl<I: IntoIterator<Item = (Name, Value)>, Name, Value> Attributes for I
where
    Name: AttributeName,
    Value: AttributeValue,
{
    fn write_attributes<'a, 't, W: Write>(self, w: &mut TagOpening<'a, 't, W>) -> std::fmt::Result {
        for (n, v) in self {
            w.attr(n, v)?;
        }
        Ok(())
    }
}

impl Attributes for Empty {
    fn write_attributes<'a, 't, W: Write>(self, _w: &mut TagOpening<'a, 't, W>) -> std::fmt::Result {
        Ok(())
    }
}


/// Tells whether the given string is a valid attribute name.
pub fn is_valid_attribute_name(name: &str) -> bool {
    if name.is_empty() {
        return false;
    }

    if name.trim() != name {
        return false;
    }

    let mut chars = name.chars();
    let first_char = chars.next().unwrap();

    if !first_char.is_ascii_alphabetic() {
        return false;
    }

    for c in chars {
        if !c.is_ascii_alphanumeric() && c != '-' && c != '_' {
            return false;
        }
    }

    true
}

/// Represents a name of an attribute.
pub trait AttributeName {
    /// Tells whether the attribute name is valid.
    fn is_valid_attribute_name(&self) -> bool;
    /// Writes the attribute name to `w`.
    ///
    /// # Arguments
    /// * `w` - The writer to write to.
    fn write_attribute_name(self, w: &mut impl Write) -> std::fmt::Result;
}

impl AttributeName for &str {
    fn is_valid_attribute_name(&self) -> bool {
        is_valid_attribute_name(self)
    }
    fn write_attribute_name(self, w: &mut impl Write) -> std::fmt::Result {
        w.write_str(self)
    }
}

impl AttributeName for &&str {
    fn is_valid_attribute_name(&self) -> bool {
        is_valid_attribute_name(self)
    }
    fn write_attribute_name(self, w: &mut impl Write) -> std::fmt::Result {
        w.write_str(*self)
    }
}

impl AttributeName for String {
    fn is_valid_attribute_name(&self) -> bool {
        is_valid_attribute_name(&self)
    }
    fn write_attribute_name(self, w: &mut impl Write) -> std::fmt::Result {
        w.write_str(&self)
    }
}

impl AttributeName for &String {
    fn is_valid_attribute_name(&self) -> bool {
        is_valid_attribute_name(self)
    }
    fn write_attribute_name(self, w: &mut impl Write) -> std::fmt::Result {
        w.write_str(self)
    }
}

/// Represents a value of an attribute.
pub trait AttributeValue {
    /// Tells whether the attribute value is a unit value, meaning that it is not written.
    fn is_unit(&self) -> bool {
        false
    }
    /// Writes the attribute value to `w`.
    ///
    /// # Arguments
    /// * `w` - The writer to write to.
    fn write_attribute_value(self, w: &mut impl Write) -> std::fmt::Result;
}

impl AttributeValue for &str {
    fn is_unit(&self) -> bool {
        false
    }
    fn write_attribute_value(self, w: &mut impl Write) -> std::fmt::Result {
        w.write_str(self)
    }
}

impl AttributeValue for &&str {
    fn is_unit(&self) -> bool {
        false
    }
    fn write_attribute_value(self, w: &mut impl Write) -> std::fmt::Result {
        w.write_str(*self)
    }
}

impl AttributeValue for String {
    fn is_unit(&self) -> bool {
        false
    }
    fn write_attribute_value(self, w: &mut impl Write) -> std::fmt::Result {
        w.write_str(&self)
    }
}

impl AttributeValue for &String {
    fn is_unit(&self) -> bool {
        false
    }
    fn write_attribute_value(self, w: &mut impl Write) -> std::fmt::Result {
        w.write_str(self)
    }
}

impl AttributeValue for () {
    fn is_unit(&self) -> bool {
        true
    }
    fn write_attribute_value(self, _w: &mut impl Write) -> std::fmt::Result {
        Ok(())
    }
}

/*impl<F: FnOnce(&mut dyn Write) -> std::fmt::Result> AttributeValue for F {
    fn is_unit(&self) -> bool {
        false
    }
    fn write_attribute_value(self, w: &mut impl Write) -> std::fmt::Result {
        self(w)
    }
}*/