Skip to content

Commit a79c609

Browse files
authored
Document AttributeValue::from (#204)
This commit adds documentation that explains how `AttributeValue`'s `From` implementation is used.
1 parent 94793ad commit a79c609

File tree

2 files changed

+55
-39
lines changed

2 files changed

+55
-39
lines changed

crates/html-macro/src/parser/open_tag.rs

+5
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,11 @@ fn create_valid_node(
103103
tokens.push(add_closure);
104104
}
105105
_ => {
106+
// NOTE: The `AttributeValue`'s documentation mentions that contributors can search
107+
// for `#value.into()` to find where the `AttributeValue`'s `From` implementation
108+
// is used.
109+
// So, if we change this code such that it no longer says `#value.into()`, we
110+
// should update the `AttributeValue's` `From` implementation's documentation.
106111
let insert_attribute = quote! {
107112
#var_name_node.as_velement_mut().expect("Not an element")
108113
.attrs.insert(#key.to_string(), #value.into());

crates/virtual-node/src/velement/attribute_value.rs

+50-39
Original file line numberDiff line numberDiff line change
@@ -64,65 +64,76 @@ impl Into<JsValue> for AttributeValue {
6464
}
6565
}
6666

67-
impl From<String> for AttributeValue {
68-
fn from(s: String) -> Self {
69-
AttributeValue::String(s)
67+
mod from_impls {
68+
//! These `From` implementations are used by the `html-macro` to convert an arbitrary expression
69+
//! into an [`AttributeValue`].
70+
//! Relying on the `From` impl allows us to create an `AttributeValue` without needing the
71+
//! `AttributeValue` type to be in scope. This means that the generated macro code does not
72+
//! require `AttributeValue` to be in scope.
73+
//! To find this use case, search for `#value.into()` within the `crates/html-macro` crate.
74+
75+
use super::*;
76+
77+
impl From<String> for AttributeValue {
78+
fn from(s: String) -> Self {
79+
AttributeValue::String(s)
80+
}
7081
}
71-
}
7282

73-
impl From<&String> for AttributeValue {
74-
fn from(s: &String) -> Self {
75-
AttributeValue::String(s.to_string())
83+
impl From<&String> for AttributeValue {
84+
fn from(s: &String) -> Self {
85+
AttributeValue::String(s.to_string())
86+
}
7687
}
77-
}
7888

79-
impl From<&str> for AttributeValue {
80-
fn from(s: &str) -> Self {
81-
AttributeValue::String(s.to_string())
89+
impl From<&str> for AttributeValue {
90+
fn from(s: &str) -> Self {
91+
AttributeValue::String(s.to_string())
92+
}
8293
}
83-
}
8494

85-
impl<S: AsRef<str>, const N: usize> From<[S; N]> for AttributeValue {
86-
fn from(vals: [S; N]) -> Self {
87-
let mut combined = "".to_string();
95+
impl<S: AsRef<str>, const N: usize> From<[S; N]> for AttributeValue {
96+
fn from(vals: [S; N]) -> Self {
97+
let mut combined = "".to_string();
8898

89-
for (idx, val) in vals.iter().enumerate() {
90-
if idx != 0 {
91-
combined += " ";
99+
for (idx, val) in vals.iter().enumerate() {
100+
if idx != 0 {
101+
combined += " ";
102+
}
103+
104+
combined += val.as_ref();
92105
}
93106

94-
combined += val.as_ref();
107+
AttributeValue::String(combined)
95108
}
96-
97-
AttributeValue::String(combined)
98109
}
99-
}
100110

101-
impl<S: AsRef<str>> From<Vec<S>> for AttributeValue {
102-
fn from(vals: Vec<S>) -> Self {
103-
let mut combined = "".to_string();
111+
impl<S: AsRef<str>> From<Vec<S>> for AttributeValue {
112+
fn from(vals: Vec<S>) -> Self {
113+
let mut combined = "".to_string();
104114

105-
for (idx, val) in vals.iter().enumerate() {
106-
if idx != 0 {
107-
combined += " ";
115+
for (idx, val) in vals.iter().enumerate() {
116+
if idx != 0 {
117+
combined += " ";
118+
}
119+
120+
combined += val.as_ref();
108121
}
109122

110-
combined += val.as_ref();
123+
AttributeValue::String(combined)
111124
}
112-
113-
AttributeValue::String(combined)
114125
}
115-
}
116126

117-
impl From<bool> for AttributeValue {
118-
fn from(b: bool) -> Self {
119-
AttributeValue::Bool(b)
127+
impl From<bool> for AttributeValue {
128+
fn from(b: bool) -> Self {
129+
AttributeValue::Bool(b)
130+
}
120131
}
121-
}
122132

123-
impl From<&bool> for AttributeValue {
124-
fn from(b: &bool) -> Self {
125-
AttributeValue::Bool(*b)
133+
impl From<&bool> for AttributeValue {
134+
fn from(b: &bool) -> Self {
135+
AttributeValue::Bool(*b)
136+
}
126137
}
127138
}
128139

0 commit comments

Comments
 (0)