implement Clone and other improvements for Error

This commit is contained in:
cel 🌸 2025-02-25 20:39:06 +00:00
parent c4e204db64
commit 2b399fb59d
1 changed files with 25 additions and 12 deletions

View File

@ -2,13 +2,17 @@ use std::{
collections::{HashMap, VecDeque}, collections::{HashMap, VecDeque},
num::ParseIntError, num::ParseIntError,
str::Utf8Error, str::Utf8Error,
sync::Arc,
}; };
use thiserror::Error; use thiserror::Error;
use crate::element::{Content, Name, NamespaceDeclaration}; use crate::{
element::{Content, Name, NamespaceDeclaration},
Element,
};
#[derive(Error, Debug)] #[derive(Error, Debug, Clone)]
pub enum DeserializeError { pub enum DeserializeError {
#[error("could not parse string {0:?} to requested value")] #[error("could not parse string {0:?} to requested value")]
FromStr(String), FromStr(String),
@ -28,19 +32,23 @@ pub enum DeserializeError {
MissingChild, MissingChild,
#[error("element missing expected text value")] #[error("element missing expected text value")]
MissingValue, MissingValue,
// not used by crate (yet), but may be used by consumers implementing FromElement
#[error("unexpected element: {0:?}")]
UnexpectedElement(Element),
} }
#[derive(Error, Debug)] #[derive(Error, Debug, Clone)]
pub enum Error { pub enum Error {
#[error(transparent)] #[error("io: {0}")]
ReadError(#[from] std::io::Error), // TODO: is this okay?
#[error(transparent)] ReadError(Arc<std::io::Error>),
#[error("utf8 conversion: {0}")]
Utf8Error(#[from] Utf8Error), Utf8Error(#[from] Utf8Error),
#[error("nom parse error: {0}")] #[error("nom parsing: {0}")]
ParseError(String), ParseError(String),
#[error("unknown xml entity reference `&{0};`")] #[error("unknown xml entity reference `&{0};`")]
EntityProcessError(String), EntityProcessError(String),
#[error(transparent)] #[error("invalid character reference: {0}")]
InvalidCharRef(CharRefError), InvalidCharRef(CharRefError),
#[error("duplicate namespace declaration: {0:?}")] #[error("duplicate namespace declaration: {0:?}")]
DuplicateNameSpaceDeclaration(NamespaceDeclaration), DuplicateNameSpaceDeclaration(NamespaceDeclaration),
@ -54,16 +62,21 @@ pub enum Error {
ExtraData(String), ExtraData(String),
#[error("namespace `{0}` has not previously been declared")] #[error("namespace `{0}` has not previously been declared")]
UndeclaredNamespace(String), UndeclaredNamespace(String),
#[error(transparent)] #[error("deserialization error: {0}")]
Deserialize(#[from] DeserializeError), Deserialize(#[from] DeserializeError),
/// root element end tag already processed
#[error("root element has already been fully processed")] #[error("root element has already been fully processed")]
RootElementEnded, RootElementEnded,
} }
#[derive(Error, Debug)] impl From<std::io::Error> for Error {
fn from(e: std::io::Error) -> Self {
Self::ReadError(Arc::new(e))
}
}
#[derive(Error, Debug, Clone)]
pub enum CharRefError { pub enum CharRefError {
#[error(transparent)] #[error("int parsing: {0}")]
ParseInt(#[from] ParseIntError), ParseInt(#[from] ParseIntError),
#[error("u32 `{0}` does not represent a valid char")] #[error("u32 `{0}` does not represent a valid char")]
IntegerNotAChar(u32), IntegerNotAChar(u32),