peanuts/src/error.rs

73 lines
2.4 KiB
Rust
Raw Normal View History

use std::{
collections::{HashMap, VecDeque},
num::ParseIntError,
2025-02-25 18:08:20 +00:00
str::Utf8Error,
};
2024-11-10 14:31:43 +00:00
2025-02-25 18:08:20 +00:00
use thiserror::Error;
use crate::element::{Content, Name, NamespaceDeclaration};
2024-06-27 20:22:16 +01:00
2025-02-25 18:08:20 +00:00
#[derive(Error, Debug)]
pub enum DeserializeError {
2025-02-25 18:08:20 +00:00
#[error("could not parse string {0:?} to requested value")]
FromStr(String),
2025-02-25 18:08:20 +00:00
#[error("unexpected attributes {0:?}")]
UnexpectedAttributes(HashMap<Name, String>),
2025-02-25 18:08:20 +00:00
#[error("unexpected element content: {0:?}")]
UnexpectedContent(VecDeque<Content>),
2025-02-25 18:08:20 +00:00
#[error("attribute `{0:?}` missing")]
MissingAttribute(Name),
2025-02-25 18:08:20 +00:00
#[error("incorrect localname: expected `{expected:?}`, found `{found:?}`")]
IncorrectName { expected: String, found: String },
#[error("incorrect namespace: expected `{expected:?}`, found `{found:?}`")]
IncorrectNamespace { expected: String, found: String },
#[error("unqualified namespace: expected `{expected:?}`")]
Unqualified { expected: String },
#[error("element missing expected child")]
MissingChild,
2025-02-25 18:08:20 +00:00
#[error("element missing expected text value")]
MissingValue,
}
2025-02-25 18:08:20 +00:00
#[derive(Error, Debug)]
2024-06-27 20:22:16 +01:00
pub enum Error {
2025-02-25 18:08:20 +00:00
#[error(transparent)]
ReadError(#[from] std::io::Error),
#[error(transparent)]
Utf8Error(#[from] Utf8Error),
#[error("nom parse error: {0}")]
2024-06-27 20:22:16 +01:00
ParseError(String),
2025-02-25 18:08:20 +00:00
#[error("unknown xml entity reference `&{0};`")]
2024-11-10 14:31:43 +00:00
EntityProcessError(String),
2025-02-25 18:08:20 +00:00
#[error(transparent)]
InvalidCharRef(CharRefError),
#[error("duplicate namespace declaration: {0:?}")]
DuplicateNameSpaceDeclaration(NamespaceDeclaration),
2025-02-25 18:08:20 +00:00
#[error("duplicate attribute: {0}")]
2024-11-10 14:31:43 +00:00
DuplicateAttribute(String),
2025-02-25 18:08:20 +00:00
#[error("mismatched end tag: expected `{0:?}`, found `{1:?}`")]
2024-11-19 16:26:59 +00:00
MismatchedEndTag(Name, Name),
2025-02-25 18:08:20 +00:00
#[error("not currently in any element")]
2024-11-10 22:28:55 +00:00
NotInElement(String),
2025-02-25 18:08:20 +00:00
#[error("extra unexpected data included in complete parse: `{0}`")]
ExtraData(String),
2025-02-25 18:08:20 +00:00
#[error("namespace `{0}` has not previously been declared")]
2024-11-20 15:10:36 +00:00
UndeclaredNamespace(String),
2025-02-25 18:08:20 +00:00
#[error(transparent)]
Deserialize(#[from] DeserializeError),
/// root element end tag already processed
2025-02-25 18:08:20 +00:00
#[error("root element has already been fully processed")]
RootElementEnded,
}
2025-02-25 18:08:20 +00:00
#[derive(Error, Debug)]
pub enum CharRefError {
#[error(transparent)]
ParseInt(#[from] ParseIntError),
#[error("u32 `{0}` does not represent a valid char")]
IntegerNotAChar(u32),
#[error("`{0}` is not a valid xml char")]
InvalidXMLChar(char),
2024-11-10 14:31:43 +00:00
}