From 38e11c74772e3588e5352d216361f0514366d2cc Mon Sep 17 00:00:00 2001 From: RedPhoenixQ Date: Tue, 1 Oct 2024 20:09:04 +0200 Subject: [PATCH] Return SyntaxError from BangType --- src/reader/buffered_reader.rs | 2 +- src/reader/mod.rs | 26 +++++++++++++++----------- src/reader/slice_reader.rs | 2 +- src/reader/state.rs | 2 +- 4 files changed, 18 insertions(+), 14 deletions(-) diff --git a/src/reader/buffered_reader.rs b/src/reader/buffered_reader.rs index 6446ed8a..0136a55e 100644 --- a/src/reader/buffered_reader.rs +++ b/src/reader/buffered_reader.rs @@ -192,7 +192,7 @@ macro_rules! impl_buffered_source { } *position += read; - Err(bang_type.to_err()) + Err(bang_type.to_err().into()) } #[inline] diff --git a/src/reader/mod.rs b/src/reader/mod.rs index fb63b2da..a05e5bc5 100644 --- a/src/reader/mod.rs +++ b/src/reader/mod.rs @@ -6,7 +6,7 @@ use std::io; use std::ops::Range; use crate::encoding::Decoder; -use crate::errors::{Error, Result, SyntaxError}; +use crate::errors::{Error, SyntaxError}; use crate::events::Event; use crate::parser::{ElementParser, Parser, PiParser}; use crate::reader::state::ReaderState; @@ -894,7 +894,7 @@ impl Reader { /// Read text into the given buffer, and return an event that borrows from /// either that buffer or from the input itself, based on the type of the /// reader. - fn read_event_impl<'i, B>(&mut self, mut buf: B) -> Result> + fn read_event_impl<'i, B>(&mut self, mut buf: B) -> Result, Error> where R: XmlSource<'i, B>, { @@ -903,7 +903,7 @@ impl Reader { /// Private function to read until `>` is found. This function expects that /// it was called just after encounter a `<` symbol. - fn read_until_close<'i, B>(&mut self, buf: B) -> Result> + fn read_until_close<'i, B>(&mut self, buf: B) -> Result, Error> where R: XmlSource<'i, B>, { @@ -979,7 +979,7 @@ trait XmlSource<'r, B> { /// reader which provides bytes fed into the parser. /// /// [events]: crate::events::Event - fn read_with

(&mut self, parser: P, buf: B, position: &mut u64) -> Result<&'r [u8]> + fn read_with

(&mut self, parser: P, buf: B, position: &mut u64) -> Result<&'r [u8], Error> where P: Parser; @@ -998,7 +998,11 @@ trait XmlSource<'r, B> { /// - `position`: Will be increased by amount of bytes consumed /// /// [events]: crate::events::Event - fn read_bang_element(&mut self, buf: B, position: &mut u64) -> Result<(BangType, &'r [u8])>; + fn read_bang_element( + &mut self, + buf: B, + position: &mut u64, + ) -> Result<(BangType, &'r [u8]), Error>; /// Consume and discard all the whitespace until the next non-whitespace /// character or EOF. @@ -1024,12 +1028,12 @@ enum BangType { } impl BangType { #[inline(always)] - const fn new(byte: Option) -> Result { + const fn new(byte: Option) -> Result { Ok(match byte { Some(b'[') => Self::CData, Some(b'-') => Self::Comment, Some(b'D') | Some(b'd') => Self::DocType(0), - _ => return Err(Error::Syntax(SyntaxError::InvalidBangMarkup)), + _ => return Err(SyntaxError::InvalidBangMarkup), }) } @@ -1101,11 +1105,11 @@ impl BangType { None } #[inline] - const fn to_err(&self) -> Error { + const fn to_err(&self) -> SyntaxError { match self { - Self::CData => Error::Syntax(SyntaxError::UnclosedCData), - Self::Comment => Error::Syntax(SyntaxError::UnclosedComment), - Self::DocType(_) => Error::Syntax(SyntaxError::UnclosedDoctype), + Self::CData => SyntaxError::UnclosedCData, + Self::Comment => SyntaxError::UnclosedComment, + Self::DocType(_) => SyntaxError::UnclosedDoctype, } } } diff --git a/src/reader/slice_reader.rs b/src/reader/slice_reader.rs index 4aadc7bf..08287592 100644 --- a/src/reader/slice_reader.rs +++ b/src/reader/slice_reader.rs @@ -316,7 +316,7 @@ impl<'a> XmlSource<'a, ()> for &'a [u8] { } *position += self.len() as u64; - Err(bang_type.to_err()) + Err(bang_type.to_err().into()) } #[inline] diff --git a/src/reader/state.rs b/src/reader/state.rs index f906c5b6..565008ab 100644 --- a/src/reader/state.rs +++ b/src/reader/state.rs @@ -165,7 +165,7 @@ impl ReaderState { // ^^^^^ - `buf` does not contain `<` and `>`, but `self.offset` is after `>`. // ^------- We report error at that position, so we need to subtract 2 and buf len self.last_error_offset = self.offset - len as u64 - 2; - Err(bang_type.to_err()) + Err(bang_type.to_err().into()) } } }