Skip to content

Commit

Permalink
Use .error_position() instead of .buffer_position() in examples w…
Browse files Browse the repository at this point in the history
…here error position is reported

`.error_position()` is intended to report position where error start occurring,
`.buffer_position()` is intended to show to what position reader read data

tests/issues.rs leave untouched because this is the code from real problems in GH issues,
it should remain as close to issue as possible
  • Loading branch information
Mingun committed Jul 6, 2024
1 parent df65be0 commit 45e8be4
Show file tree
Hide file tree
Showing 9 changed files with 11 additions and 11 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ loop {
// when the input is a &str or a &[u8], we don't actually need to use another
// buffer, we could directly call `reader.read_event()`
match reader.read_event_into(&mut buf) {
Err(e) => panic!("Error at position {}: {:?}", reader.buffer_position(), e),
Err(e) => panic!("Error at position {}: {:?}", reader.error_position(), e),
// exits the loop when reaching end of file
Ok(Event::Eof) => break,

Expand Down Expand Up @@ -98,7 +98,7 @@ loop {
Ok(Event::Eof) => break,
// we can either move or borrow the event to write, depending on your use-case
Ok(e) => assert!(writer.write_event(e).is_ok()),
Err(e) => panic!("Error at position {}: {:?}", reader.buffer_position(), e),
Err(e) => panic!("Error at position {}: {:?}", reader.error_position(), e),
}
}

Expand Down
2 changes: 1 addition & 1 deletion examples/custom_entities.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
);
}
Ok(Event::Eof) => break,
Err(e) => panic!("Error at position {}: {:?}", reader.buffer_position(), e),
Err(e) => panic!("Error at position {}: {:?}", reader.error_position(), e),
_ => (),
}
}
Expand Down
2 changes: 1 addition & 1 deletion examples/read_buffered.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ fn main() -> Result<(), quick_xml::Error> {
count += 1;
}
Ok(Event::Eof) => break, // exits the loop when reaching end of file
Err(e) => panic!("Error at position {}: {:?}", reader.buffer_position(), e),
Err(e) => panic!("Error at position {}: {:?}", reader.error_position(), e),
_ => (), // There are several other `Event`s we do not consider here
}
}
Expand Down
2 changes: 1 addition & 1 deletion examples/read_texts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ fn main() {
println!("{:?}", txt);
}
Ok(Event::Eof) => break, // exits the loop when reaching end of file
Err(e) => panic!("Error at position {}: {:?}", reader.buffer_position(), e),
Err(e) => panic!("Error at position {}: {:?}", reader.error_position(), e),
_ => (), // There are several other `Event`s we do not consider here
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/reader/async_tokio.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ impl<R: AsyncBufRead + Unpin> Reader<R> {
/// match reader.read_event_into_async(&mut buf).await {
/// Ok(Event::Start(_)) => count += 1,
/// Ok(Event::Text(e)) => txt.push(e.unescape().unwrap().into_owned()),
/// Err(e) => panic!("Error at position {}: {:?}", reader.buffer_position(), e),
/// Err(e) => panic!("Error at position {}: {:?}", reader.error_position(), e),
/// Ok(Event::Eof) => break,
/// _ => (),
/// }
Expand Down
2 changes: 1 addition & 1 deletion src/reader/buffered_reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -328,7 +328,7 @@ impl<R: BufRead> Reader<R> {
/// match reader.read_event_into(&mut buf) {
/// Ok(Event::Start(_)) => count += 1,
/// Ok(Event::Text(e)) => txt.push(e.unescape().unwrap().into_owned()),
/// Err(e) => panic!("Error at position {}: {:?}", reader.buffer_position(), e),
/// Err(e) => panic!("Error at position {}: {:?}", reader.error_position(), e),
/// Ok(Event::Eof) => break,
/// _ => (),
/// }
Expand Down
2 changes: 1 addition & 1 deletion src/reader/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -589,7 +589,7 @@ impl EncodingRef {
/// // when the input is a &str or a &[u8], we don't actually need to use another
/// // buffer, we could directly call `reader.read_event()`
/// match reader.read_event_into(&mut buf) {
/// Err(e) => panic!("Error at position {}: {:?}", reader.buffer_position(), e),
/// Err(e) => panic!("Error at position {}: {:?}", reader.error_position(), e),
/// // exits the loop when reaching end of file
/// Ok(Event::Eof) => break,
///
Expand Down
2 changes: 1 addition & 1 deletion src/writer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ use {crate::de::DeError, serde::Serialize};
/// Ok(Event::Eof) => break,
/// // we can either move or borrow the event to write, depending on your use-case
/// Ok(e) => assert!(writer.write_event(e.borrow()).is_ok()),
/// Err(e) => panic!("Error at position {}: {:?}", reader.buffer_position(), e),
/// Err(e) => panic!("Error at position {}: {:?}", reader.error_position(), e),
/// }
/// }
///
Expand Down
4 changes: 2 additions & 2 deletions tests/reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -175,15 +175,15 @@ fn test_escaped_content() {
Ok(c) => assert_eq!(c, "<test>"),
Err(e) => panic!(
"cannot escape content at position {}: {:?}",
r.buffer_position(),
r.error_position(),
e
),
}
}
Ok(e) => panic!("Expecting text event, got {:?}", e),
Err(e) => panic!(
"Cannot get next event at position {}: {:?}",
r.buffer_position(),
r.error_position(),
e
),
}
Expand Down

0 comments on commit 45e8be4

Please sign in to comment.