Skip to content

Commit e707c52

Browse files
committed
refactor: use impl Into<Cow<'a, str>> trait to convert strings to Cows
1 parent dfbbc1e commit e707c52

File tree

4 files changed

+30
-63
lines changed

4 files changed

+30
-63
lines changed

src/address.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,13 +64,13 @@ impl ToXml for Address<'_> {
6464

6565
if let Some(phone_numbers) = &self.phone {
6666
for phone_number in phone_numbers {
67-
e = e.with_text_element("Phone", phone_number);
67+
e = e.with_text_element("Phone", *phone_number);
6868
}
6969
}
7070

7171
if let Some(email_addresses) = &self.email {
7272
for email_address in email_addresses {
73-
e = e.with_text_element("Email", email_address);
73+
e = e.with_text_element("Email", *email_address);
7474
}
7575
}
7676

src/contact.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,13 +44,13 @@ impl ToXml for Contact<'_> {
4444

4545
if let Some(phone_numbers) = &self.phone {
4646
for phone_number in phone_numbers {
47-
e = e.with_text_element("Phone", phone_number);
47+
e = e.with_text_element("Phone", *phone_number);
4848
}
4949
}
5050

5151
if let Some(email_addresses) = &self.email {
5252
for email_address in email_addresses {
53-
e = e.with_text_element("Email", email_address);
53+
e = e.with_text_element("Email", *email_address);
5454
}
5555
}
5656

src/details.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ impl ToXml for DetailsItem<'_> {
9797

9898
// Description(s).
9999
for description in &self.description {
100-
e = e.with_text_element("Description", description);
100+
e = e.with_text_element("Description", *description);
101101
}
102102

103103
// Quantity.

src/xml.rs

Lines changed: 25 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -53,22 +53,21 @@ pub(crate) struct XmlElement {
5353
}
5454

5555
impl XmlElement {
56-
pub(crate) fn new(name: &str) -> Self {
56+
pub(crate) fn new<'a>(name: impl Into<Cow<'a, str>>) -> Self {
57+
let mut name = name.into();
58+
xml_escape(&mut name);
59+
5760
XmlElement {
5861
name: name.to_string(),
5962
..Default::default()
6063
}
6164
}
6265

63-
pub(crate) fn with_attr<'a>(
64-
mut self,
65-
name: impl AsRef<str> + 'a,
66-
value: impl AsRef<str> + 'a,
67-
) -> Self {
66+
pub(crate) fn with_attr<'a>(mut self, name: impl Into<Cow<'a, str>>, value: impl Into<Cow<'a, str>>) -> Self {
6867
self.attrs.get_or_insert_with(Vec::new).push(
6968
XmlAttribute {
70-
name: Cow::from(name.as_ref()),
71-
value: Cow::from(value.as_ref()),
69+
name: name.into(),
70+
value: value.into(),
7271
}
7372
.to_xml(),
7473
);
@@ -82,49 +81,26 @@ impl XmlElement {
8281

8382
pub(crate) fn with_text_element<'a>(
8483
mut self,
85-
name: &'a str,
86-
text: impl AsRef<str> + 'a,
84+
name: impl Into<Cow<'a, str>>,
85+
text: impl Into<Cow<'a, str>>,
8786
) -> Self {
88-
self.body.push(
89-
XmlElement {
90-
name: name.to_owned(),
91-
body: vec![
92-
XmlText {
93-
text: Cow::from(text.as_ref()),
94-
}
95-
.to_xml(),
96-
],
97-
..Default::default()
98-
}
99-
.to_xml(),
100-
);
87+
self.body.push(XmlElement::new(name).with_text(text).to_xml());
10188
self
10289
}
10390

104-
pub(crate) fn with_text<'a>(mut self, text: impl AsRef<str> + 'a) -> Self {
105-
self.body.push(
106-
XmlText {
107-
text: Cow::from(text.as_ref()),
108-
}
109-
.to_xml(),
110-
);
91+
pub(crate) fn with_text<'a>(mut self, text: impl Into<Cow<'a, str>>) -> Self {
92+
self.body.push(XmlText { text: text.into() }.to_xml());
11193
self
11294
}
11395
}
11496

11597
impl ToXml for XmlElement {
11698
fn to_xml(&self) -> String {
117-
let mut name = Cow::from(&self.name);
118-
xml_escape(&mut name);
119-
12099
let attrs = self.attrs.as_ref().map_or("".to_string(), |a| a.join(" "));
121100

122101
let body = self.body.join("");
123102

124-
format!(
125-
"<{name}{}{attrs}>{body}</{name}>",
126-
if !attrs.is_empty() { " " } else { "" }
127-
)
103+
format!("<{}{}{attrs}>{body}</{}>", self.name, if !attrs.is_empty() { " " } else { "" }, self.name)
128104
}
129105
}
130106

@@ -157,10 +133,7 @@ mod tests {
157133
assert_eq!(t5.as_ref(), "&gt;");
158134
assert_eq!(t6.as_ref(), "&amp;&quot;");
159135
assert_eq!(t7.as_ref(), "&amp;ü&quot;");
160-
assert_eq!(
161-
t8.as_ref(),
162-
"&lt;test foo=&quot;bar&quot;&gt;baz&lt;/test&gt;"
163-
);
136+
assert_eq!(t8.as_ref(), "&lt;test foo=&quot;bar&quot;&gt;baz&lt;/test&gt;");
164137
}
165138

166139
#[test]
@@ -170,10 +143,7 @@ mod tests {
170143

171144
#[test]
172145
fn generates_xml_text() {
173-
assert_eq!(
174-
XmlElement::new("foo").with_text("<bar&>").to_xml(),
175-
"<foo>&lt;bar&amp;&gt;</foo>"
176-
);
146+
assert_eq!(XmlElement::new("foo").with_text("<bar&>").to_xml(), "<foo>&lt;bar&amp;&gt;</foo>");
177147
}
178148

179149
#[test]
@@ -190,13 +160,7 @@ mod tests {
190160

191161
#[test]
192162
fn generates_xml_attribute() {
193-
assert_eq!(
194-
XmlElement::new("foo")
195-
.with_attr("a", "b")
196-
.with_text("bar")
197-
.to_xml(),
198-
"<foo a=\"b\">bar</foo>"
199-
);
163+
assert_eq!(XmlElement::new("foo").with_attr("a", "b").with_text("bar").to_xml(), "<foo a=\"b\">bar</foo>");
200164
}
201165

202166
#[test]
@@ -213,12 +177,7 @@ mod tests {
213177

214178
#[test]
215179
fn generates_nested_xml_element() {
216-
assert_eq!(
217-
XmlElement::new("foo")
218-
.with_element(&XmlElement::new("a"))
219-
.to_xml(),
220-
"<foo><a></a></foo>"
221-
);
180+
assert_eq!(XmlElement::new("foo").with_element(&XmlElement::new("a")).to_xml(), "<foo><a></a></foo>");
222181
}
223182

224183
#[test]
@@ -275,4 +234,12 @@ mod tests {
275234
"<a foo=\"b&lt;&gt;ar\"><b c=\"&quot;d&amp;e\"></b></a>"
276235
);
277236
}
237+
238+
#[test]
239+
fn escapes_with_text_element() {
240+
assert_eq!(
241+
XmlElement::new("a&b").with_text_element("c&d", "f&g").to_xml(),
242+
"<a&amp;b><c&amp;d>f&amp;g</c&amp;d></a&amp;b>"
243+
);
244+
}
278245
}

0 commit comments

Comments
 (0)