ahecha_html/render/render_string/attributes.rs
1use std::fmt::{Result, Write};
2
3use crate::{
4 escape_html,
5 html::{AttributeValue, Attributes},
6 RenderString,
7};
8
9impl RenderString for Attributes {
10 fn render_into<W: Write>(self, writer: &mut W) -> Result {
11 self.attrs.iter().try_for_each(|(key, value)| {
12 write!(writer, " {}", key)?;
13 match value {
14 AttributeValue::Bool(value) => {
15 if !value {
16 return write!(writer, "={}", value);
17 }
18 }
19 AttributeValue::None => {}
20 AttributeValue::String(value) => {
21 if !value.is_empty() {
22 write!(writer, "=\"")?;
23 escape_html(value, writer)?;
24 write!(writer, "\"")?;
25 }
26 }
27 }
28
29 Ok(())
30 })?;
31
32 Ok(())
33 }
34}
35
36// pub trait RenderAttributeValue {
37// fn to_attribute_value(&self) -> String;
38// }
39
40// impl RenderAttributeValue for Option<String> {
41// fn to_attribute_value(&self) -> String {
42// match self {
43// Some(s) => s.to_owned(),
44// None => "".to_string(),
45// }
46// }
47// }
48
49// impl RenderAttributeValue for Option<&str> {
50// fn to_attribute_value(&self) -> String {
51// match self {
52// Some(s) => s.to_string(),
53// None => "".to_string(),
54// }
55// }
56// }
57
58// macro_rules! impl_attribute_value {
59// ($($t:ty),*) => {
60// $(impl RenderAttributeValue for $t {
61// fn to_attribute_value(&self) -> String {
62// self.to_string()
63// }
64// })*
65
66// $(impl From<$t> for AttributeValue {
67// fn from(item: $t) -> AttributeValue {
68// AttributeValue::String(item.to_string())
69// }
70// })*
71
72// $(impl From<Option<$t>> for AttributeValue {
73// fn from(item: Option<$t>) -> AttributeValue {
74// match item {
75// Some(item) => item.into(),
76// None => AttributeValue::None,
77// }
78// }
79// })*
80// };
81// }
82
83// macro_rules! impl_attribute_value_with_into {
84// ($($t:ty),*) => {
85// $(impl_attribute_value!($t);)*
86
87// $(impl From<AttributeValue> for $t {
88// fn from(item: AttributeValue) -> $t {
89// match item {
90// AttributeValue::String(s) => s.parse().unwrap(),
91// _ => todo!("AttributeValue"),
92// }
93// }
94// })*
95// };
96// }
97
98// impl RenderAttributeValue for AttributeValue {
99// fn to_attribute_value(&self) -> String {
100// match self {
101// Self::Bool(value) => value.to_string(),
102// Self::None => String::new(),
103// Self::String(value) => value.to_owned(),
104// }
105// }
106// }
107
108// impl_attribute_value!(&&str, &str, &String, String, bool);
109// impl_attribute_value_with_into!(u8, u16, u32, u64, i8, i16, i32, i64, f32, f64);
110
111// impl RenderAttributes for Vec<(String, AttributeValue)> {
112// fn render_attributes_into<W: Write>(self, writer: &mut W) -> Result {
113// // self.sort_by(|a, b| a.0.partial_cmp(&b.0));
114// for (k, v) in self.iter() {
115// match v {
116// AttributeValue::None => (),
117// _ => {
118// write!(writer, " {}=\"", k)?;
119// escape_html(&v.to_attribute_value(), writer)?;
120// write!(writer, "\"")?;
121// }
122// }
123// }
124
125// Ok(())
126// }
127// }
128
129// pub trait RenderAttributes: Sized {
130// fn render_attributes_into<W: Write>(self, writer: &mut W) -> Result;
131// }
132
133// impl RenderAttributes for () {
134// fn render_attributes_into<W: Write>(self, _writer: &mut W) -> Result {
135// Ok(())
136// }
137// }
138
139// impl<A> RenderAttributes for (&str, A)
140// where
141// A: RenderAttributeValue,
142// {
143// fn render_attributes_into<W: Write>(self, writer: &mut W) -> Result {
144// write!(writer, " {}=\"", self.0)?;
145// escape_html(&self.1.to_attribute_value(), writer)?;
146// write!(writer, "\"")
147// }
148// }