Skip to content

Commit c9133a8

Browse files
committed
Replace BytesText::from_escaped_str by BytesText::new
Replace `BytesText::from_plain_str` by `BytesText::from_unescaped`
1 parent 801f04d commit c9133a8

File tree

7 files changed

+32
-46
lines changed

7 files changed

+32
-46
lines changed

Changelog.md

+5-1
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@
135135

136136
- [#423]: All escaping functions now accepts and returns strings instead of byte slices
137137
- [#423]: Removed `BytesText::from_plain` because it internally did escaping of a byte array,
138-
but since now escaping works on strings. Use `BytesText::from_plain_str` instead
138+
but since now escaping works on strings. Use `BytesText::from_unescaped` instead
139139

140140
- [#428]: Removed `BytesText::escaped()`. Use `.as_ref()` provided by `Deref` impl instead.
141141
- [#428]: Removed `BytesText::from_escaped()`. Use constructors from strings instead,
@@ -156,6 +156,10 @@
156156
|`BytesStart::borrowed(&[u8], usize)` |_(as above)_
157157
|`BytesEnd::owned(Vec<u8>)` |`BytesEnd::new(impl Into<Cow<str>>)`
158158
|`BytesEnd::borrowed(&[u8])` |_(as above)_
159+
|`BytesText::from_escaped(impl Into<Cow<[u8]>>)` |`BytesText::new(impl Into<Cow<str>>)`
160+
|`BytesText::from_escaped_str(impl Into<Cow<str>>)`|_(as above)_
161+
|`BytesText::from_plain(&[u8])` |`BytesText::from_unescaped(&str)`
162+
|`BytesText::from_plain_str(&str)` |_(as above)_
159163
|`BytesCData::new(impl Into<Cow<[u8]>>)` |`BytesCData::new(impl Into<Cow<str>>)`
160164
|`BytesCData::from_str(&str)` |_(as above)_
161165

src/de/mod.rs

+9-15
Original file line numberDiff line numberDiff line change
@@ -1051,7 +1051,7 @@ mod tests {
10511051
de.write,
10521052
vec![
10531053
Start(BytesStart::new("inner")),
1054-
Text(BytesText::from_escaped_str("text")),
1054+
Text(BytesText::new("text")),
10551055
Start(BytesStart::new("inner")),
10561056
End(BytesEnd::new("inner")),
10571057
End(BytesEnd::new("inner")),
@@ -1085,7 +1085,7 @@ mod tests {
10851085
de.read,
10861086
vec![
10871087
Start(BytesStart::new("inner")),
1088-
Text(BytesText::from_escaped_str("text")),
1088+
Text(BytesText::new("text")),
10891089
Start(BytesStart::new("inner")),
10901090
End(BytesEnd::new("inner")),
10911091
End(BytesEnd::new("inner")),
@@ -1109,7 +1109,7 @@ mod tests {
11091109
vec![
11101110
// This comment here to keep the same formatting of both arrays
11111111
// otherwise rustfmt suggest one-line it
1112-
Text(BytesText::from_escaped_str("text")),
1112+
Text(BytesText::new("text")),
11131113
]
11141114
);
11151115

@@ -1128,16 +1128,10 @@ mod tests {
11281128
de.start_replay();
11291129
assert_eq!(
11301130
de.read,
1131-
vec![
1132-
Text(BytesText::from_escaped_str("text")),
1133-
End(BytesEnd::new("inner")),
1134-
]
1131+
vec![Text(BytesText::new("text")), End(BytesEnd::new("inner")),]
11351132
);
11361133
assert_eq!(de.write, vec![]);
1137-
assert_eq!(
1138-
de.next().unwrap(),
1139-
Text(BytesText::from_escaped_str("text"))
1140-
);
1134+
assert_eq!(de.next().unwrap(), Text(BytesText::new("text")));
11411135
assert_eq!(de.next().unwrap(), End(BytesEnd::new("inner")));
11421136
assert_eq!(de.next().unwrap(), Start(BytesStart::new("target")));
11431137
assert_eq!(de.next().unwrap(), End(BytesEnd::new("target")));
@@ -1174,7 +1168,7 @@ mod tests {
11741168
de.write,
11751169
vec![
11761170
Start(BytesStart::new("skip")),
1177-
Text(BytesText::from_escaped_str("text")),
1171+
Text(BytesText::new("text")),
11781172
Start(BytesStart::new("skip")),
11791173
End(BytesEnd::new("skip")),
11801174
End(BytesEnd::new("skip")),
@@ -1195,7 +1189,7 @@ mod tests {
11951189
de.write,
11961190
vec![
11971191
Start(BytesStart::new("skip")),
1198-
Text(BytesText::from_escaped_str("text")),
1192+
Text(BytesText::new("text")),
11991193
Start(BytesStart::new("skip")),
12001194
End(BytesEnd::new("skip")),
12011195
End(BytesEnd::new("skip")),
@@ -1217,7 +1211,7 @@ mod tests {
12171211
de.read,
12181212
vec![
12191213
Start(BytesStart::new("skip")),
1220-
Text(BytesText::from_escaped_str("text")),
1214+
Text(BytesText::new("text")),
12211215
Start(BytesStart::new("skip")),
12221216
End(BytesEnd::new("skip")),
12231217
End(BytesEnd::new("skip")),
@@ -1368,7 +1362,7 @@ mod tests {
13681362
r#"item name="hello" source="world.rs""#,
13691363
4
13701364
)),
1371-
Text(BytesText::from_escaped_str("Some text")),
1365+
Text(BytesText::new("Some text")),
13721366
End(BytesEnd::new("item")),
13731367
Start(BytesStart::from_content("item2", 5)),
13741368
End(BytesEnd::new("item2")),

src/events/mod.rs

+4-16
Original file line numberDiff line numberDiff line change
@@ -683,27 +683,15 @@ impl<'a> BytesText<'a> {
683683

684684
/// Creates a new `BytesText` from an escaped string.
685685
#[inline]
686-
pub fn from_escaped_str<C: Into<Cow<'a, str>>>(content: C) -> Self {
687-
Self::wrap(
688-
match content.into() {
689-
Cow::Owned(o) => Cow::Owned(o.into_bytes()),
690-
Cow::Borrowed(b) => Cow::Borrowed(b.as_bytes()),
691-
},
692-
Decoder::utf8(),
693-
)
686+
pub fn new<C: Into<Cow<'a, str>>>(content: C) -> Self {
687+
Self::wrap(str_cow_to_bytes(content), Decoder::utf8())
694688
}
695689

696690
/// Creates a new `BytesText` from a string. The string is expected not to
697691
/// be escaped.
698692
#[inline]
699-
pub fn from_plain_str(content: &'a str) -> Self {
700-
Self {
701-
content: match escape(content) {
702-
Cow::Borrowed(s) => Cow::Borrowed(s.as_bytes()),
703-
Cow::Owned(s) => Cow::Owned(s.into_bytes()),
704-
},
705-
decoder: Decoder::utf8(),
706-
}
693+
pub fn from_unescaped(content: &'a str) -> Self {
694+
Self::new(escape(content))
707695
}
708696

709697
/// Ensures that all data is owned to extend the object's lifetime if

src/reader.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -2577,7 +2577,7 @@ mod test {
25772577

25782578
assert_eq!(
25792579
reader.read_event_impl($buf).unwrap(),
2580-
Event::StartText(BytesText::from_escaped_str("bom").into())
2580+
Event::StartText(BytesText::new("bom").into())
25812581
);
25822582
}
25832583

@@ -2597,7 +2597,7 @@ mod test {
25972597

25982598
assert_eq!(
25992599
reader.read_event_impl($buf).unwrap(),
2600-
Event::DocType(BytesText::from_escaped_str("x"))
2600+
Event::DocType(BytesText::new("x"))
26012601
);
26022602
}
26032603

@@ -2607,7 +2607,7 @@ mod test {
26072607

26082608
assert_eq!(
26092609
reader.read_event_impl($buf).unwrap(),
2610-
Event::PI(BytesText::from_escaped_str("xml-stylesheet"))
2610+
Event::PI(BytesText::new("xml-stylesheet"))
26112611
);
26122612
}
26132613

@@ -2656,7 +2656,7 @@ mod test {
26562656

26572657
assert_eq!(
26582658
reader.read_event_impl($buf).unwrap(),
2659-
Event::Text(BytesText::from_escaped_str("text"))
2659+
Event::Text(BytesText::new("text"))
26602660
);
26612661
}
26622662

@@ -2676,7 +2676,7 @@ mod test {
26762676

26772677
assert_eq!(
26782678
reader.read_event_impl($buf).unwrap(),
2679-
Event::Comment(BytesText::from_escaped_str(""))
2679+
Event::Comment(BytesText::new(""))
26802680
);
26812681
}
26822682

src/se/mod.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -102,9 +102,9 @@ impl<'r, W: Write> Serializer<'r, W> {
102102
) -> Result<(), DeError> {
103103
let value = value.to_string();
104104
let event = if escaped {
105-
BytesText::from_escaped_str(&value)
105+
BytesText::new(&value)
106106
} else {
107-
BytesText::from_plain_str(&value)
107+
BytesText::from_unescaped(&value)
108108
};
109109
self.writer.write_event(Event::Text(event))?;
110110
Ok(())

src/writer.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ impl<W: Write> Writer<W> {
193193
/// // writes <tag attr1="value1" attr2="value2">with some text inside</tag>
194194
/// writer.create_element("tag")
195195
/// .with_attributes(vec![("attr1", "value1"), ("attr2", "value2")].into_iter()) // or add attributes from an iterator
196-
/// .write_text_content(BytesText::from_plain_str("with some text inside"))?;
196+
/// .write_text_content(BytesText::from_unescaped("with some text inside"))?;
197197
///
198198
/// // writes <tag><fruit quantity="0">apple</fruit><fruit quantity="1">orange</fruit></tag>
199199
/// writer.create_element("tag")
@@ -203,7 +203,7 @@ impl<W: Write> Writer<W> {
203203
/// writer
204204
/// .create_element("fruit")
205205
/// .with_attribute(("quantity", quant.to_string().as_str()))
206-
/// .write_text_content(BytesText::from_plain_str(item))?;
206+
/// .write_text_content(BytesText::from_unescaped(item))?;
207207
/// }
208208
/// Ok(())
209209
/// })?;
@@ -417,7 +417,7 @@ mod indentation {
417417
let start = BytesStart::new("paired")
418418
.with_attributes(vec![("attr1", "value1"), ("attr2", "value2")].into_iter());
419419
let end = start.to_end();
420-
let text = BytesText::from_plain_str("text");
420+
let text = BytesText::from_unescaped("text");
421421

422422
writer
423423
.write_event(Event::Start(start.clone()))
@@ -443,7 +443,7 @@ mod indentation {
443443
let start = BytesStart::new("paired")
444444
.with_attributes(vec![("attr1", "value1"), ("attr2", "value2")].into_iter());
445445
let end = start.to_end();
446-
let text = BytesText::from_plain_str("text");
446+
let text = BytesText::from_unescaped("text");
447447
let inner = BytesStart::new("inner");
448448

449449
writer
@@ -528,7 +528,7 @@ mod indentation {
528528
.create_element("paired")
529529
.with_attribute(("attr1", "value1"))
530530
.with_attribute(("attr2", "value2"))
531-
.write_text_content(BytesText::from_plain_str("text"))
531+
.write_text_content(BytesText::from_unescaped("text"))
532532
.expect("failure");
533533

534534
assert_eq!(
@@ -552,7 +552,7 @@ mod indentation {
552552
writer
553553
.create_element("fruit")
554554
.with_attribute(("quantity", quant.to_string().as_str()))
555-
.write_text_content(BytesText::from_plain_str(item))?;
555+
.write_text_content(BytesText::from_unescaped(item))?;
556556
}
557557
writer
558558
.create_element("inner")

tests/unit_tests.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -597,7 +597,7 @@ fn test_read_write_roundtrip_escape_text() -> Result<()> {
597597
Text(e) => {
598598
let t = e.unescape().unwrap();
599599
assert!(writer
600-
.write_event(Text(BytesText::from_plain_str(&t)))
600+
.write_event(Text(BytesText::from_unescaped(&t)))
601601
.is_ok());
602602
}
603603
e => assert!(writer.write_event(e).is_ok()),

0 commit comments

Comments
 (0)