xml_data/
errors.rs

1#![allow(missing_docs)] // names should be good enough
2//! Helper functions to generate common errors
3
4use crate::{
5	Error,
6};
7use std::fmt;
8
9pub(crate) enum ParseError {
10	UnexpectedEof { msg: String },
11	UnexpectedEnd,
12	UnexpectedDecl,
13	UnexpectedDocType,
14	UnexpectedPI,
15	UnexpectedText,
16	UnexpectedElement { tag: String },
17	UnexpectedAttribute { key: String },
18	InnerElementNotParsed { tag: String },
19	MissingElement { tag: String },
20	MissingUnknownElement,
21	MissingAttribute { key: String },
22}
23
24impl fmt::Debug for ParseError {
25	fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
26		match self {
27			Self::UnexpectedEof { msg } => write!(f, "unexpected eof: {}", msg),
28			Self::UnexpectedEnd => write!(f, "Unexpected end tag"),
29			Self::UnexpectedDecl => write!(f, "Unexpected decl <?xml ... ?>"),
30			Self::UnexpectedDocType => write!(f, "Unexpected <!DOCTYPE ...>"),
31			Self::UnexpectedPI => write!(f, "Unexpected processing instructions <?...?>"),
32			Self::UnexpectedText => write!(f, "Unexpected (non-whitespace) text/CDATA"),
33			Self::UnexpectedElement { tag } => write!(f, "Unexpected element: {}", tag),
34			Self::UnexpectedAttribute { key } => write!(f, "Unexpected attribute: {}", key),
35			Self::InnerElementNotParsed { tag } => write!(f, "Inner element {:?} wasn't fully parsed", tag),
36			Self::MissingElement { tag } => write!(f, "Missing element {:?}", tag),
37			Self::MissingUnknownElement => write!(f, "Missing element"),
38			Self::MissingAttribute { key } => write!(f, "Missing attribute {:?}", key),
39		}
40	}
41}
42
43impl fmt::Display for ParseError {
44	fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
45		fmt::Debug::fmt(self, f)
46	}
47}
48
49impl std::error::Error for ParseError {
50}
51
52pub fn unexpected_eof(msg: &str) -> Error {
53	ParseError::UnexpectedEof { msg: msg.into() }.into()
54}
55
56pub fn unexpected_end() -> Error {
57	ParseError::UnexpectedEnd.into()
58}
59
60pub fn unexpected_decl() -> Error {
61	ParseError::UnexpectedDecl.into()
62}
63
64pub fn unexpected_doctype() -> Error {
65	ParseError::UnexpectedDocType.into()
66}
67
68pub fn unexpected_pi() -> Error {
69	ParseError::UnexpectedPI.into()
70}
71
72pub fn unexpected_text() -> Error {
73	ParseError::UnexpectedText.into()
74}
75
76pub fn unexpected_element(tag: &str) -> Error {
77	ParseError::UnexpectedElement { tag: tag.into() }.into()
78}
79
80pub fn unexpected_attribute(key: &str) -> Error {
81	ParseError::UnexpectedAttribute { key: key.into() }.into()
82}
83
84pub fn inner_element_not_parsed(tag: &str) -> Error {
85	ParseError::InnerElementNotParsed { tag: tag.into() }.into()
86}
87
88pub fn missing_element(tag: &str) -> Error {
89	ParseError::MissingElement { tag: tag.into() }.into()
90}
91
92pub fn missing_unknown_element() -> Error {
93	ParseError::MissingUnknownElement.into()
94}
95
96pub fn missing_attribute(key: &str) -> Error {
97	ParseError::MissingAttribute { key: key.into() }.into()
98}