xso: correctly handle I/O errors during start event in from_reader
Without this, it'd panic, which is "not ideal", as they say.
This commit is contained in:
parent
cd43aa2fcb
commit
7074344834
1 changed files with 24 additions and 2 deletions
|
@ -493,11 +493,33 @@ pub fn from_bytes<T: FromXml>(mut buf: &[u8]) -> Result<T, self::error::Error> {
|
||||||
Err(self::error::Error::XmlError(rxml::Error::InvalidEof(None)))
|
Err(self::error::Error::XmlError(rxml::Error::InvalidEof(None)))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn read_start_event_io<I: std::io::BufRead>(
|
||||||
|
r: &mut rxml::Reader<I>,
|
||||||
|
) -> io::Result<(rxml::QName, rxml::AttrMap)> {
|
||||||
|
for ev in r {
|
||||||
|
match ev? {
|
||||||
|
rxml::Event::XmlDeclaration(_, rxml::XmlVersion::V1_0) => (),
|
||||||
|
rxml::Event::StartElement(_, name, attrs) => return Ok((name, attrs)),
|
||||||
|
_ => {
|
||||||
|
return Err(io::Error::new(
|
||||||
|
io::ErrorKind::InvalidData,
|
||||||
|
self::error::Error::Other("Unexpected event at start of document"),
|
||||||
|
))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Err(io::Error::new(
|
||||||
|
io::ErrorKind::InvalidData,
|
||||||
|
self::error::Error::XmlError(rxml::Error::InvalidEof(Some(
|
||||||
|
rxml::error::ErrorContext::DocumentBegin,
|
||||||
|
))),
|
||||||
|
))
|
||||||
|
}
|
||||||
|
|
||||||
/// Attempt to parse a type implementing [`FromXml`] from a reader.
|
/// Attempt to parse a type implementing [`FromXml`] from a reader.
|
||||||
pub fn from_reader<T: FromXml, R: io::BufRead>(r: R) -> io::Result<T> {
|
pub fn from_reader<T: FromXml, R: io::BufRead>(r: R) -> io::Result<T> {
|
||||||
let mut reader = rxml::Reader::new(r);
|
let mut reader = rxml::Reader::new(r);
|
||||||
let (name, attrs) =
|
let (name, attrs) = read_start_event_io(&mut reader)?;
|
||||||
read_start_event(&mut reader).map_err(|e| io::Error::new(io::ErrorKind::InvalidData, e))?;
|
|
||||||
let mut builder = match T::from_events(name, attrs) {
|
let mut builder = match T::from_events(name, attrs) {
|
||||||
Ok(v) => v,
|
Ok(v) => v,
|
||||||
Err(self::error::FromEventsError::Mismatch { .. }) => {
|
Err(self::error::FromEventsError::Mismatch { .. }) => {
|
||||||
|
|
Loading…
Reference in a new issue