Skip to content

Commit

Permalink
Replace BytesText::from_escaped_str by BytesText::new
Browse files Browse the repository at this point in the history
Replace `BytesText::from_plain_str` by `BytesText::from_unescaped`
  • Loading branch information
Mingun committed Jul 19, 2022
1 parent 801f04d commit c9133a8
Show file tree
Hide file tree
Showing 7 changed files with 32 additions and 46 deletions.
6 changes: 5 additions & 1 deletion Changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@

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

- [#428]: Removed `BytesText::escaped()`. Use `.as_ref()` provided by `Deref` impl instead.
- [#428]: Removed `BytesText::from_escaped()`. Use constructors from strings instead,
Expand All @@ -156,6 +156,10 @@
|`BytesStart::borrowed(&[u8], usize)` |_(as above)_
|`BytesEnd::owned(Vec<u8>)` |`BytesEnd::new(impl Into<Cow<str>>)`
|`BytesEnd::borrowed(&[u8])` |_(as above)_
|`BytesText::from_escaped(impl Into<Cow<[u8]>>)` |`BytesText::new(impl Into<Cow<str>>)`
|`BytesText::from_escaped_str(impl Into<Cow<str>>)`|_(as above)_
|`BytesText::from_plain(&[u8])` |`BytesText::from_unescaped(&str)`
|`BytesText::from_plain_str(&str)` |_(as above)_
|`BytesCData::new(impl Into<Cow<[u8]>>)` |`BytesCData::new(impl Into<Cow<str>>)`
|`BytesCData::from_str(&str)` |_(as above)_

Expand Down
24 changes: 9 additions & 15 deletions src/de/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1051,7 +1051,7 @@ mod tests {
de.write,
vec![
Start(BytesStart::new("inner")),
Text(BytesText::from_escaped_str("text")),
Text(BytesText::new("text")),
Start(BytesStart::new("inner")),
End(BytesEnd::new("inner")),
End(BytesEnd::new("inner")),
Expand Down Expand Up @@ -1085,7 +1085,7 @@ mod tests {
de.read,
vec![
Start(BytesStart::new("inner")),
Text(BytesText::from_escaped_str("text")),
Text(BytesText::new("text")),
Start(BytesStart::new("inner")),
End(BytesEnd::new("inner")),
End(BytesEnd::new("inner")),
Expand All @@ -1109,7 +1109,7 @@ mod tests {
vec![
// This comment here to keep the same formatting of both arrays
// otherwise rustfmt suggest one-line it
Text(BytesText::from_escaped_str("text")),
Text(BytesText::new("text")),
]
);

Expand All @@ -1128,16 +1128,10 @@ mod tests {
de.start_replay();
assert_eq!(
de.read,
vec![
Text(BytesText::from_escaped_str("text")),
End(BytesEnd::new("inner")),
]
vec![Text(BytesText::new("text")), End(BytesEnd::new("inner")),]
);
assert_eq!(de.write, vec![]);
assert_eq!(
de.next().unwrap(),
Text(BytesText::from_escaped_str("text"))
);
assert_eq!(de.next().unwrap(), Text(BytesText::new("text")));
assert_eq!(de.next().unwrap(), End(BytesEnd::new("inner")));
assert_eq!(de.next().unwrap(), Start(BytesStart::new("target")));
assert_eq!(de.next().unwrap(), End(BytesEnd::new("target")));
Expand Down Expand Up @@ -1174,7 +1168,7 @@ mod tests {
de.write,
vec![
Start(BytesStart::new("skip")),
Text(BytesText::from_escaped_str("text")),
Text(BytesText::new("text")),
Start(BytesStart::new("skip")),
End(BytesEnd::new("skip")),
End(BytesEnd::new("skip")),
Expand All @@ -1195,7 +1189,7 @@ mod tests {
de.write,
vec![
Start(BytesStart::new("skip")),
Text(BytesText::from_escaped_str("text")),
Text(BytesText::new("text")),
Start(BytesStart::new("skip")),
End(BytesEnd::new("skip")),
End(BytesEnd::new("skip")),
Expand All @@ -1217,7 +1211,7 @@ mod tests {
de.read,
vec![
Start(BytesStart::new("skip")),
Text(BytesText::from_escaped_str("text")),
Text(BytesText::new("text")),
Start(BytesStart::new("skip")),
End(BytesEnd::new("skip")),
End(BytesEnd::new("skip")),
Expand Down Expand Up @@ -1368,7 +1362,7 @@ mod tests {
r#"item name="hello" source="world.rs""#,
4
)),
Text(BytesText::from_escaped_str("Some text")),
Text(BytesText::new("Some text")),
End(BytesEnd::new("item")),
Start(BytesStart::from_content("item2", 5)),
End(BytesEnd::new("item2")),
Expand Down
20 changes: 4 additions & 16 deletions src/events/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -683,27 +683,15 @@ impl<'a> BytesText<'a> {

/// Creates a new `BytesText` from an escaped string.
#[inline]
pub fn from_escaped_str<C: Into<Cow<'a, str>>>(content: C) -> Self {
Self::wrap(
match content.into() {
Cow::Owned(o) => Cow::Owned(o.into_bytes()),
Cow::Borrowed(b) => Cow::Borrowed(b.as_bytes()),
},
Decoder::utf8(),
)
pub fn new<C: Into<Cow<'a, str>>>(content: C) -> Self {
Self::wrap(str_cow_to_bytes(content), Decoder::utf8())
}

/// Creates a new `BytesText` from a string. The string is expected not to
/// be escaped.
#[inline]
pub fn from_plain_str(content: &'a str) -> Self {
Self {
content: match escape(content) {
Cow::Borrowed(s) => Cow::Borrowed(s.as_bytes()),
Cow::Owned(s) => Cow::Owned(s.into_bytes()),
},
decoder: Decoder::utf8(),
}
pub fn from_unescaped(content: &'a str) -> Self {
Self::new(escape(content))
}

/// Ensures that all data is owned to extend the object's lifetime if
Expand Down
10 changes: 5 additions & 5 deletions src/reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2577,7 +2577,7 @@ mod test {

assert_eq!(
reader.read_event_impl($buf).unwrap(),
Event::StartText(BytesText::from_escaped_str("bom").into())
Event::StartText(BytesText::new("bom").into())
);
}

Expand All @@ -2597,7 +2597,7 @@ mod test {

assert_eq!(
reader.read_event_impl($buf).unwrap(),
Event::DocType(BytesText::from_escaped_str("x"))
Event::DocType(BytesText::new("x"))
);
}

Expand All @@ -2607,7 +2607,7 @@ mod test {

assert_eq!(
reader.read_event_impl($buf).unwrap(),
Event::PI(BytesText::from_escaped_str("xml-stylesheet"))
Event::PI(BytesText::new("xml-stylesheet"))
);
}

Expand Down Expand Up @@ -2656,7 +2656,7 @@ mod test {

assert_eq!(
reader.read_event_impl($buf).unwrap(),
Event::Text(BytesText::from_escaped_str("text"))
Event::Text(BytesText::new("text"))
);
}

Expand All @@ -2676,7 +2676,7 @@ mod test {

assert_eq!(
reader.read_event_impl($buf).unwrap(),
Event::Comment(BytesText::from_escaped_str(""))
Event::Comment(BytesText::new(""))
);
}

Expand Down
4 changes: 2 additions & 2 deletions src/se/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,9 +102,9 @@ impl<'r, W: Write> Serializer<'r, W> {
) -> Result<(), DeError> {
let value = value.to_string();
let event = if escaped {
BytesText::from_escaped_str(&value)
BytesText::new(&value)
} else {
BytesText::from_plain_str(&value)
BytesText::from_unescaped(&value)
};
self.writer.write_event(Event::Text(event))?;
Ok(())
Expand Down
12 changes: 6 additions & 6 deletions src/writer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ impl<W: Write> Writer<W> {
/// // writes <tag attr1="value1" attr2="value2">with some text inside</tag>
/// writer.create_element("tag")
/// .with_attributes(vec![("attr1", "value1"), ("attr2", "value2")].into_iter()) // or add attributes from an iterator
/// .write_text_content(BytesText::from_plain_str("with some text inside"))?;
/// .write_text_content(BytesText::from_unescaped("with some text inside"))?;
///
/// // writes <tag><fruit quantity="0">apple</fruit><fruit quantity="1">orange</fruit></tag>
/// writer.create_element("tag")
Expand All @@ -203,7 +203,7 @@ impl<W: Write> Writer<W> {
/// writer
/// .create_element("fruit")
/// .with_attribute(("quantity", quant.to_string().as_str()))
/// .write_text_content(BytesText::from_plain_str(item))?;
/// .write_text_content(BytesText::from_unescaped(item))?;
/// }
/// Ok(())
/// })?;
Expand Down Expand Up @@ -417,7 +417,7 @@ mod indentation {
let start = BytesStart::new("paired")
.with_attributes(vec![("attr1", "value1"), ("attr2", "value2")].into_iter());
let end = start.to_end();
let text = BytesText::from_plain_str("text");
let text = BytesText::from_unescaped("text");

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

writer
Expand Down Expand Up @@ -528,7 +528,7 @@ mod indentation {
.create_element("paired")
.with_attribute(("attr1", "value1"))
.with_attribute(("attr2", "value2"))
.write_text_content(BytesText::from_plain_str("text"))
.write_text_content(BytesText::from_unescaped("text"))
.expect("failure");

assert_eq!(
Expand All @@ -552,7 +552,7 @@ mod indentation {
writer
.create_element("fruit")
.with_attribute(("quantity", quant.to_string().as_str()))
.write_text_content(BytesText::from_plain_str(item))?;
.write_text_content(BytesText::from_unescaped(item))?;
}
writer
.create_element("inner")
Expand Down
2 changes: 1 addition & 1 deletion tests/unit_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -597,7 +597,7 @@ fn test_read_write_roundtrip_escape_text() -> Result<()> {
Text(e) => {
let t = e.unescape().unwrap();
assert!(writer
.write_event(Text(BytesText::from_plain_str(&t)))
.write_event(Text(BytesText::from_unescaped(&t)))
.is_ok());
}
e => assert!(writer.write_event(e).is_ok()),
Expand Down

0 comments on commit c9133a8

Please sign in to comment.