diff --git a/src/event/error.rs b/src/event/error.rs new file mode 100644 index 0000000..cdf5d70 --- /dev/null +++ b/src/event/error.rs @@ -0,0 +1,93 @@ +/* + * This Source Code Form is subject to the terms of the + * Mozilla Public License, v. 2.0. If a copy of the MPL + * was not distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/. + */ + +use std::{ + fmt::{self, Debug}, + io, + str::Utf8Error, +}; + +use crate::{reader::ReaderError, scanner::error::ScanError}; + +pub type ParseResult = std::result::Result; + +#[derive(Debug)] +pub enum ParseError +{ + CorruptStream, + DuplicateVersion, + DuplicateTagDirective, + UndefinedTag, + MissingDocumentStart, + MissingBlockEntry, + MissingNode, + MissingKey, + MissingFlowSequenceEntryOrEnd, + MissingFlowMappingEntryOrEnd, + + Scanner(ScanError), + UTF8(Utf8Error), + IO(io::Error), + + UnexpectedEOF, +} + +impl From for ParseError +{ + fn from(e: ScanError) -> Self + { + Self::Scanner(e) + } +} + +impl From for ParseError +{ + fn from(e: ReaderError) -> Self + { + match e + { + ReaderError::UTF8(e) => Self::UTF8(e), + ReaderError::IO(e) => Self::IO(e), + ReaderError::Scanner(e) => Self::Scanner(e), + } + } +} + +impl PartialEq for ParseError +{ + fn eq(&self, other: &Self) -> bool + { + match (self, other) + { + (Self::Scanner(s), Self::Scanner(o)) => s == o, + (Self::UTF8(s), Self::UTF8(o)) => s == o, + (Self::IO(s), Self::IO(o)) => s.kind() == o.kind(), + _ => core::mem::discriminant(self) == core::mem::discriminant(other), + } + } +} + +impl fmt::Display for ParseError +{ + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result + { + Debug::fmt(self, f) + } +} + +impl std::error::Error for ParseError +{ + fn source(&self) -> Option<&(dyn std::error::Error + 'static)> + { + match self + { + Self::Scanner(e) => Some(e), + Self::UTF8(e) => Some(e), + Self::IO(e) => Some(e), + _ => None, + } + } +} diff --git a/src/event/mod.rs b/src/event/mod.rs index 30b299d..3bb9163 100644 --- a/src/event/mod.rs +++ b/src/event/mod.rs @@ -5,3 +5,5 @@ */ mod state; + +pub mod error;