diff --git a/Changelog.md b/Changelog.md index 85d13bc0..45de2629 100644 --- a/Changelog.md +++ b/Changelog.md @@ -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, @@ -156,6 +156,10 @@ |`BytesStart::borrowed(&[u8], usize)` |_(as above)_ |`BytesEnd::owned(Vec)` |`BytesEnd::new(impl Into>)` |`BytesEnd::borrowed(&[u8])` |_(as above)_ + |`BytesText::from_escaped(impl Into>)` |`BytesText::new(impl Into>)` + |`BytesText::from_escaped_str(impl Into>)`|_(as above)_ + |`BytesText::from_plain(&[u8])` |`BytesText::from_unescaped(&str)` + |`BytesText::from_plain_str(&str)` |_(as above)_ |`BytesCData::new(impl Into>)` |`BytesCData::new(impl Into>)` |`BytesCData::from_str(&str)` |_(as above)_ diff --git a/src/de/mod.rs b/src/de/mod.rs index c897a50a..c7aea57c 100644 --- a/src/de/mod.rs +++ b/src/de/mod.rs @@ -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")), @@ -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")), @@ -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")), ] ); @@ -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"))); @@ -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")), @@ -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")), @@ -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")), @@ -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")), diff --git a/src/events/mod.rs b/src/events/mod.rs index 18b39efb..e0c15d53 100644 --- a/src/events/mod.rs +++ b/src/events/mod.rs @@ -683,27 +683,15 @@ impl<'a> BytesText<'a> { /// Creates a new `BytesText` from an escaped string. #[inline] - pub fn from_escaped_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>>(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 diff --git a/src/reader.rs b/src/reader.rs index b290508b..3f515a5b 100644 --- a/src/reader.rs +++ b/src/reader.rs @@ -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()) ); } @@ -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")) ); } @@ -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")) ); } @@ -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")) ); } @@ -2676,7 +2676,7 @@ mod test { assert_eq!( reader.read_event_impl($buf).unwrap(), - Event::Comment(BytesText::from_escaped_str("")) + Event::Comment(BytesText::new("")) ); } diff --git a/src/se/mod.rs b/src/se/mod.rs index da6f9128..c78dcfe4 100644 --- a/src/se/mod.rs +++ b/src/se/mod.rs @@ -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(()) diff --git a/src/writer.rs b/src/writer.rs index 23114144..b3c55557 100644 --- a/src/writer.rs +++ b/src/writer.rs @@ -193,7 +193,7 @@ impl Writer { /// // writes with some text inside /// 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 appleorange /// writer.create_element("tag") @@ -203,7 +203,7 @@ impl Writer { /// 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(()) /// })?; @@ -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())) @@ -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 @@ -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!( @@ -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") diff --git a/tests/unit_tests.rs b/tests/unit_tests.rs index 5a4be7a6..20d55115 100644 --- a/tests/unit_tests.rs +++ b/tests/unit_tests.rs @@ -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()),