Skip to content

Commit

Permalink
Remove DeError::UnexpectedEnd, because this error could be returned…
Browse files Browse the repository at this point in the history
… only if the deserializer has a bug
  • Loading branch information
Mingun committed Oct 28, 2023
1 parent da2823d commit d9520c4
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 23 deletions.
12 changes: 8 additions & 4 deletions src/de/map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -669,7 +669,9 @@ where
seed.deserialize(BorrowedStrDeserializer::<DeError>::new(TEXT_KEY))?,
true,
),
DeEvent::End(e) => return Err(DeError::UnexpectedEnd(e.name().into_inner().to_vec())),
// SAFETY: The reader is guaranteed that we don't have unmatched tags
// If we here, then out deserializer has a bug
DeEvent::End(e) => unreachable!("{:?}", e),
DeEvent::Eof => return Err(DeError::UnexpectedEof),
};
Ok((
Expand Down Expand Up @@ -927,9 +929,11 @@ where
DeEvent::Start(e) if !self.filter.is_suitable(e, decoder)? => Ok(None),

// Stop iteration after reaching a closing tag
DeEvent::End(e) if e.name() == self.map.start.name() => Ok(None),
// This is a unmatched closing tag, so the XML is invalid
DeEvent::End(e) => Err(DeError::UnexpectedEnd(e.name().as_ref().to_owned())),
// The matching tag name is guaranteed by the reader
DeEvent::End(e) => {
debug_assert_eq!(self.map.start.name(), e.name());
Ok(None)
}
// We cannot get `Eof` legally, because we always inside of the
// opened tag `self.map.start`
DeEvent::Eof => Err(DeError::UnexpectedEof),
Expand Down
16 changes: 11 additions & 5 deletions src/de/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2619,7 +2619,7 @@ where
/// |Event |XML |Handling
/// |------------------|---------------------------|----------------------------------------
/// |[`DeEvent::Start`]|`<tag>...</tag>` |if `allow_start == true`, result determined by the second table, otherwise emits [`UnexpectedStart("tag")`](DeError::UnexpectedStart)
/// |[`DeEvent::End`] |`</any-tag>` |Emits [`UnexpectedEnd("any-tag")`](DeError::UnexpectedEnd)
/// |[`DeEvent::End`] |`</any-tag>` |This is impossible situation, the method will panic if it happens
/// |[`DeEvent::Text`] |`text content` or `<![CDATA[cdata content]]>` (probably mixed)|Returns event content unchanged
/// |[`DeEvent::Eof`] | |Emits [`UnexpectedEof`](DeError::UnexpectedEof)
///
Expand All @@ -2640,7 +2640,9 @@ where
// allow one nested level
DeEvent::Start(_) if allow_start => self.read_text(),
DeEvent::Start(e) => Err(DeError::UnexpectedStart(e.name().as_ref().to_owned())),
DeEvent::End(e) => Err(DeError::UnexpectedEnd(e.name().as_ref().to_owned())),
// SAFETY: The reader is guaranteed that we don't have unmatched tags
// If we here, then out deserializer has a bug
DeEvent::End(e) => unreachable!("{:?}", e),
DeEvent::Eof => Err(DeError::UnexpectedEof),
}
}
Expand Down Expand Up @@ -2816,7 +2818,9 @@ where
{
match self.next()? {
DeEvent::Start(e) => visitor.visit_map(ElementMapAccess::new(self, e, fields)?),
DeEvent::End(e) => Err(DeError::UnexpectedEnd(e.name().as_ref().to_owned())),
// SAFETY: The reader is guaranteed that we don't have unmatched tags
// If we here, then out deserializer has a bug
DeEvent::End(e) => unreachable!("{:?}", e),
DeEvent::Text(_) => Err(DeError::ExpectedStart),
DeEvent::Eof => Err(DeError::UnexpectedEof),
}
Expand All @@ -2836,7 +2840,7 @@ where
/// |Event |XML |Handling
/// |------------------|---------------------------|-------------------------------------------
/// |[`DeEvent::Start`]|`<tag>...</tag>` |Calls `visitor.visit_unit()`, consumes all events up to and including corresponding `End` event
/// |[`DeEvent::End`] |`</tag>` |Emits [`UnexpectedEnd("tag")`](DeError::UnexpectedEnd)
/// |[`DeEvent::End`] |`</tag>` |This is impossible situation, the method will panic if it happens
/// |[`DeEvent::Text`] |`text content` or `<![CDATA[cdata content]]>` (probably mixed)|Calls `visitor.visit_unit()`. The content is ignored
/// |[`DeEvent::Eof`] | |Emits [`UnexpectedEof`](DeError::UnexpectedEof)
fn deserialize_unit<V>(self, visitor: V) -> Result<V::Value, DeError>
Expand All @@ -2849,7 +2853,9 @@ where
visitor.visit_unit()
}
DeEvent::Text(_) => visitor.visit_unit(),
DeEvent::End(e) => Err(DeError::UnexpectedEnd(e.name().as_ref().to_owned())),
// SAFETY: The reader is guaranteed that we don't have unmatched tags
// If we here, then out deserializer has a bug
DeEvent::End(e) => unreachable!("{:?}", e),
DeEvent::Eof => Err(DeError::UnexpectedEof),
}
}
Expand Down
4 changes: 3 additions & 1 deletion src/de/var.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,9 @@ where
seed.deserialize(BorrowedStrDeserializer::<DeError>::new(TEXT_KEY))?,
true,
),
DeEvent::End(e) => return Err(DeError::UnexpectedEnd(e.name().into_inner().to_vec())),
// SAFETY: The reader is guaranteed that we don't have unmatched tags
// If we here, then out deserializer has a bug
DeEvent::End(e) => unreachable!("{:?}", e),
DeEvent::Eof => return Err(DeError::UnexpectedEof),
};
Ok((
Expand Down
13 changes: 0 additions & 13 deletions src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -196,14 +196,6 @@ pub mod serialize {
/// not expecting. This happens when you try to deserialize a primitive
/// value (numbers, strings, booleans) from an XML element.
UnexpectedStart(Vec<u8>),
/// Deserializer encounter an end tag with a specified name when it is
/// not expecting. Usually that should not be possible, because XML reader
/// is not able to produce such stream of events that lead to this error.
///
/// If you get this error this likely indicates and error in the `quick_xml`.
/// Please open an issue at <https://github.com/tafia/quick-xml>, provide
/// your Rust code and XML input.
UnexpectedEnd(Vec<u8>),
/// The [`Reader`] produced [`Event::Eof`] when it is not expecting,
/// for example, after producing [`Event::Start`] but before corresponding
/// [`Event::End`].
Expand Down Expand Up @@ -252,11 +244,6 @@ pub mod serialize {
write_byte_string(f, e)?;
f.write_str(")`")
}
DeError::UnexpectedEnd(e) => {
f.write_str("Unexpected `Event::End(")?;
write_byte_string(f, e)?;
f.write_str(")`")
}
DeError::UnexpectedEof => write!(f, "Unexpected `Event::Eof`"),
DeError::ExpectedStart => write!(f, "Expecting `Event::Start`"),
DeError::Unsupported(s) => write!(f, "Unsupported operation: {}", s),
Expand Down

0 comments on commit d9520c4

Please sign in to comment.