Skip to content

Commit 792d23d

Browse files
committed
Remove confusing Reader::read_text_into which looks similar to read_text but works totally different
It is better to explicitly match `Event::Text`
1 parent d67f426 commit 792d23d

File tree

3 files changed

+8
-60
lines changed

3 files changed

+8
-60
lines changed

Changelog.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,8 @@
184184
- [#440]: Removed `Deserializer::from_slice` and `quick_xml::de::from_slice` methods because deserializing from a byte
185185
array cannot guarantee borrowing due to possible copying while decoding.
186186

187+
- [#455]: Removed `Reader::read_text_into` which is only not a better wrapper over match on `Event::Text`
188+
187189
### New Tests
188190

189191
- [#9]: Added tests for incorrect nested tags in input

examples/read_texts.rs

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
fn main() {
22
use quick_xml::events::Event;
3-
use quick_xml::name::QName;
43
use quick_xml::Reader;
54

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

12-
let mut txt = Vec::new();
13-
let mut buf = Vec::new();
14-
1511
loop {
16-
match reader.read_event_into(&mut buf) {
17-
Ok(Event::Start(ref e)) if e.name().as_ref() == b"tag2" => {
18-
txt.push(
19-
reader
20-
.read_text_into(QName(b"tag2"), &mut Vec::new())
21-
.expect("Cannot decode text value"),
22-
);
12+
match reader.read_event() {
13+
Ok(Event::Start(e)) if e.name().as_ref() == b"tag2" => {
14+
// read_text_into for buffered readers not implemented
15+
let txt = reader
16+
.read_text(e.name())
17+
.expect("Cannot decode text value");
2318
println!("{:?}", txt);
2419
}
2520
Ok(Event::Eof) => break, // exits the loop when reaching end of file
2621
Err(e) => panic!("Error at position {}: {:?}", reader.buffer_position(), e),
2722
_ => (), // There are several other `Event`s we do not consider here
2823
}
29-
buf.clear();
3024
}
3125
}

src/reader/buffered_reader.rs

Lines changed: 0 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -365,54 +365,6 @@ impl<R: BufRead> Reader<R> {
365365
buf.clear();
366366
}))
367367
}
368-
369-
/// Reads optional text between start and end tags.
370-
///
371-
/// If the next event is a [`Text`] event, returns the decoded and unescaped content as a
372-
/// `String`. If the next event is an [`End`] event, returns the empty string. In all other
373-
/// cases, returns an error.
374-
///
375-
/// Any text will be decoded using the XML encoding specified in the XML declaration (or UTF-8
376-
/// if none is specified).
377-
///
378-
/// # Examples
379-
///
380-
/// ```
381-
/// # use pretty_assertions::assert_eq;
382-
/// use quick_xml::Reader;
383-
/// use quick_xml::events::Event;
384-
///
385-
/// let mut xml = Reader::from_reader(b"
386-
/// <a>&lt;b&gt;</a>
387-
/// <a></a>
388-
/// " as &[u8]);
389-
/// xml.trim_text(true);
390-
///
391-
/// let expected = ["<b>", ""];
392-
/// for &content in expected.iter() {
393-
/// match xml.read_event_into(&mut Vec::new()) {
394-
/// Ok(Event::Start(ref e)) => {
395-
/// assert_eq!(&xml.read_text_into(e.name(), &mut Vec::new()).unwrap(), content);
396-
/// },
397-
/// e => panic!("Expecting Start event, found {:?}", e),
398-
/// }
399-
/// }
400-
/// ```
401-
///
402-
/// [`Text`]: Event::Text
403-
/// [`End`]: Event::End
404-
pub fn read_text_into(&mut self, end: QName, buf: &mut Vec<u8>) -> Result<String> {
405-
let s = match self.read_event_into(buf) {
406-
Err(e) => return Err(e),
407-
408-
Ok(Event::Text(e)) => e.unescape()?.into_owned(),
409-
Ok(Event::End(e)) if e.name() == end => return Ok("".to_string()),
410-
Ok(Event::Eof) => return Err(Error::UnexpectedEof("Text".to_string())),
411-
_ => return Err(Error::TextNotFound),
412-
};
413-
self.read_to_end_into(end, buf)?;
414-
Ok(s)
415-
}
416368
}
417369

418370
impl Reader<BufReader<File>> {

0 commit comments

Comments
 (0)