Skip to content

Commit

Permalink
Remove confusing Reader::read_text_into which looks similar to `rea…
Browse files Browse the repository at this point in the history
…d_text` but works totally different

It is better to explicitly match `Event::Text`
  • Loading branch information
Mingun committed Aug 14, 2022
1 parent d67f426 commit 792d23d
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 60 deletions.
2 changes: 2 additions & 0 deletions Changelog.md
Expand Up @@ -184,6 +184,8 @@
- [#440]: Removed `Deserializer::from_slice` and `quick_xml::de::from_slice` methods because deserializing from a byte
array cannot guarantee borrowing due to possible copying while decoding.

- [#455]: Removed `Reader::read_text_into` which is only not a better wrapper over match on `Event::Text`

### New Tests

- [#9]: Added tests for incorrect nested tags in input
Expand Down
18 changes: 6 additions & 12 deletions examples/read_texts.rs
@@ -1,6 +1,5 @@
fn main() {
use quick_xml::events::Event;
use quick_xml::name::QName;
use quick_xml::Reader;

let xml = "<tag1>text1</tag1><tag1>text2</tag1>\
Expand All @@ -9,23 +8,18 @@ fn main() {
let mut reader = Reader::from_str(xml);
reader.trim_text(true);

let mut txt = Vec::new();
let mut buf = Vec::new();

loop {
match reader.read_event_into(&mut buf) {
Ok(Event::Start(ref e)) if e.name().as_ref() == b"tag2" => {
txt.push(
reader
.read_text_into(QName(b"tag2"), &mut Vec::new())
.expect("Cannot decode text value"),
);
match reader.read_event() {
Ok(Event::Start(e)) if e.name().as_ref() == b"tag2" => {
// read_text_into for buffered readers not implemented
let txt = reader
.read_text(e.name())
.expect("Cannot decode text value");
println!("{:?}", txt);
}
Ok(Event::Eof) => break, // exits the loop when reaching end of file
Err(e) => panic!("Error at position {}: {:?}", reader.buffer_position(), e),
_ => (), // There are several other `Event`s we do not consider here
}
buf.clear();
}
}
48 changes: 0 additions & 48 deletions src/reader/buffered_reader.rs
Expand Up @@ -365,54 +365,6 @@ impl<R: BufRead> Reader<R> {
buf.clear();
}))
}

/// Reads optional text between start and end tags.
///
/// If the next event is a [`Text`] event, returns the decoded and unescaped content as a
/// `String`. If the next event is an [`End`] event, returns the empty string. In all other
/// cases, returns an error.
///
/// Any text will be decoded using the XML encoding specified in the XML declaration (or UTF-8
/// if none is specified).
///
/// # Examples
///
/// ```
/// # use pretty_assertions::assert_eq;
/// use quick_xml::Reader;
/// use quick_xml::events::Event;
///
/// let mut xml = Reader::from_reader(b"
/// <a>&lt;b&gt;</a>
/// <a></a>
/// " as &[u8]);
/// xml.trim_text(true);
///
/// let expected = ["<b>", ""];
/// for &content in expected.iter() {
/// match xml.read_event_into(&mut Vec::new()) {
/// Ok(Event::Start(ref e)) => {
/// assert_eq!(&xml.read_text_into(e.name(), &mut Vec::new()).unwrap(), content);
/// },
/// e => panic!("Expecting Start event, found {:?}", e),
/// }
/// }
/// ```
///
/// [`Text`]: Event::Text
/// [`End`]: Event::End
pub fn read_text_into(&mut self, end: QName, buf: &mut Vec<u8>) -> Result<String> {
let s = match self.read_event_into(buf) {
Err(e) => return Err(e),

Ok(Event::Text(e)) => e.unescape()?.into_owned(),
Ok(Event::End(e)) if e.name() == end => return Ok("".to_string()),
Ok(Event::Eof) => return Err(Error::UnexpectedEof("Text".to_string())),
_ => return Err(Error::TextNotFound),
};
self.read_to_end_into(end, buf)?;
Ok(s)
}
}

impl Reader<BufReader<File>> {
Expand Down

0 comments on commit 792d23d

Please sign in to comment.