1use crate::reader::lexer::Token;
2use crate::Encoding;
3
4use std::borrow::Cow;
5use std::error::Error as _;
6use std::{error, fmt, io, str};
7
8use crate::common::{Position, TextPosition};
9use crate::util;
10
11#[derive(Debug)]
13pub enum ErrorKind {
14 Syntax(Cow<'static, str>),
16 Io(io::Error),
18 Utf8(str::Utf8Error),
20 UnexpectedEof,
22}
23
24#[derive(Debug, Clone, PartialEq)]
25#[non_exhaustive]
26pub(crate) enum SyntaxError {
27 CannotRedefineXmlnsPrefix,
28 CannotRedefineXmlPrefix,
29 EntityTooBig,
31 EmptyEntity,
32 NoRootElement,
33 ProcessingInstructionWithoutName,
34 UnbalancedRootElement,
35 UnexpectedEof,
36 UnexpectedOpeningTag,
37 UnclosedCdata,
39 UnexpectedQualifiedName(Token),
40 UnexpectedTokenOutsideRoot(Token),
41 UnexpectedToken(Token),
42 UnexpectedTokenInEntity(Token),
43 UnexpectedTokenInClosingTag(Token),
44 UnexpectedTokenInOpeningTag(Token),
45 InvalidQualifiedName(Box<str>),
46 UnboundAttribute(Box<str>),
47 UnboundElementPrefix(Box<str>),
48 UnexpectedClosingTag(Box<str>),
49 UnexpectedName(Box<str>),
50 UnexpectedProcessingInstruction(Box<str>, Token),
53 CannotUndefinePrefix(Box<str>),
54 InvalidCharacterEntity(u32),
55 InvalidDefaultNamespace(Box<str>),
56 InvalidNamePrefix(Box<str>),
57 InvalidNumericEntity(Box<str>),
58 InvalidStandaloneDeclaration(Box<str>),
59 InvalidXmlProcessingInstruction(Box<str>),
60 RedefinedAttribute(Box<str>),
61 UndefinedEntity(Box<str>),
62 UnexpectedEntity(Box<str>),
63 UnexpectedNameInsideXml(Box<str>),
64 UnsupportedEncoding(Box<str>),
65 UnknownMarkupDeclaration(Box<str>),
67 UnexpectedXmlVersion(Box<str>),
68 ConflictingEncoding(Encoding, Encoding),
69 UnexpectedTokenBefore(&'static str, char),
70 ExceededConfiguredLimit,
72}
73
74impl fmt::Display for SyntaxError {
75 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
76 self.to_cow().fmt(f)
77 }
78}
79
80impl SyntaxError {
81 #[inline(never)]
82 #[cold]
83 pub(crate) fn to_cow(&self) -> Cow<'static, str> {
84 match *self {
85 Self::CannotRedefineXmlnsPrefix => "Cannot redefine XMLNS prefix".into(),
86 Self::CannotRedefineXmlPrefix => "Default XMLNS prefix cannot be rebound to another value".into(),
87 Self::EmptyEntity => "Encountered empty entity".into(),
88 Self::EntityTooBig => "Entity too big".into(),
89 Self::NoRootElement => "Unexpected end of stream: no root element found".into(),
90 Self::ProcessingInstructionWithoutName => "Encountered processing instruction without a name".into(),
91 Self::UnbalancedRootElement => "Unexpected end of stream: still inside the root element".into(),
92 Self::UnclosedCdata => "Unclosed <![CDATA[".into(),
93 Self::UnexpectedEof => "Unexpected end of stream".into(),
94 Self::UnexpectedOpeningTag => "'<' is not allowed in attributes".into(),
95 Self::CannotUndefinePrefix(ref ln) => format!("Cannot undefine prefix '{ln}'").into(),
96 Self::ConflictingEncoding(a, b) => format!("Declared encoding {a}, but uses {b}").into(),
97 Self::InvalidCharacterEntity(num) => format!("Invalid character U+{num:04X}").into(),
98 Self::InvalidDefaultNamespace(ref name) => format!("Namespace '{name}' cannot be default").into(),
99 Self::InvalidNamePrefix(ref prefix) => format!("'{prefix}' cannot be an element name prefix").into(),
100 Self::InvalidNumericEntity(ref v) => format!("Invalid numeric entity: {v}").into(),
101 Self::InvalidQualifiedName(ref e) => format!("Qualified name is invalid: {e}").into(),
102 Self::InvalidStandaloneDeclaration(ref value) => format!("Invalid standalone declaration value: {value}").into(),
103 Self::InvalidXmlProcessingInstruction(ref name) => format!("Invalid processing instruction: <?{name} - \"<?xml\"-like PI is only valid at the beginning of the document").into(),
104 Self::RedefinedAttribute(ref name) => format!("Attribute '{name}' is redefined").into(),
105 Self::UnboundAttribute(ref name) => format!("Attribute {name} prefix is unbound").into(),
106 Self::UnboundElementPrefix(ref name) => format!("Element {name} prefix is unbound").into(),
107 Self::UndefinedEntity(ref v) => format!("Undefined entity: {v}").into(),
108 Self::UnexpectedClosingTag(ref expected_got) => format!("Unexpected closing tag: {expected_got}").into(),
109 Self::UnexpectedEntity(ref name) => format!("Unexpected entity: {name}").into(),
110 Self::UnexpectedName(ref name) => format!("Unexpected name: {name}").into(),
111 Self::UnexpectedNameInsideXml(ref name) => format!("Unexpected name inside XML declaration: {name}").into(),
112 Self::UnexpectedProcessingInstruction(ref buf, token) => format!("Unexpected token inside processing instruction: <?{buf}{token}").into(),
113 Self::UnexpectedQualifiedName(e) => format!("Unexpected token inside qualified name: {e}").into(),
114 Self::UnexpectedToken(token) => format!("Unexpected token: {token}").into(),
115 Self::UnexpectedTokenBefore(before, c) => format!("Unexpected token '{before}' before '{c}'").into(),
116 Self::UnexpectedTokenInClosingTag(token) => format!("Unexpected token inside closing tag: {token}").into(),
117 Self::UnexpectedTokenInEntity(token) => format!("Unexpected token inside entity: {token}").into(),
118 Self::UnexpectedTokenInOpeningTag(token) => format!("Unexpected token inside opening tag: {token}").into(),
119 Self::UnexpectedTokenOutsideRoot(token) => format!("Unexpected characters outside the root element: {token}").into(),
120 Self::UnexpectedXmlVersion(ref version) => format!("Invalid XML version: {version}").into(),
121 Self::UnknownMarkupDeclaration(ref v) => format!("Unknown markup declaration: {v}").into(),
122 Self::UnsupportedEncoding(ref v) => format!("Unsupported encoding: {v}").into(),
123 Self::ExceededConfiguredLimit => "This document is larger/more complex than allowed by the parser's configuration".into(),
124 }
125 }
126}
127
128#[derive(Clone, PartialEq, Eq, Debug)]
132pub struct Error {
133 pub(crate) pos: TextPosition,
134 pub(crate) kind: ErrorKind,
135}
136
137impl fmt::Display for Error {
138 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
139 use self::ErrorKind::{Io, Syntax, UnexpectedEof, Utf8};
140
141 write!(f, "{} ", self.pos)?;
142 match &self.kind {
143 Io(io_error) => io_error.fmt(f),
144 Utf8(reason) => reason.fmt(f),
145 Syntax(msg) => f.write_str(msg),
146 UnexpectedEof => f.write_str("Unexpected EOF"),
147 }
148 }
149}
150
151impl Position for Error {
152 #[inline]
153 fn position(&self) -> TextPosition { self.pos }
154}
155
156impl Error {
157 #[cold]
159 #[doc(hidden)]
160 #[allow(deprecated)]
161 #[must_use]
162 pub fn msg(&self) -> &str {
163 use self::ErrorKind::{Io, Syntax, UnexpectedEof, Utf8};
164 match &self.kind {
165 Io(io_error) => io_error.description(),
166 Utf8(reason) => reason.description(),
167 Syntax(msg) => msg.as_ref(),
168 UnexpectedEof => "Unexpected EOF",
169 }
170 }
171
172 #[must_use]
174 #[inline]
175 pub fn kind(&self) -> &ErrorKind {
176 &self.kind
177 }
178}
179
180impl error::Error for Error {
181 #[allow(deprecated)]
182 #[cold]
183 fn description(&self) -> &str { self.msg() }
184}
185
186impl<'a, P, M> From<(&'a P, M)> for Error where P: Position, M: Into<Cow<'static, str>> {
187 #[cold]
188 fn from(orig: (&'a P, M)) -> Self {
189 Self {
190 pos: orig.0.position(),
191 kind: ErrorKind::Syntax(orig.1.into()),
192 }
193 }
194}
195
196impl From<util::CharReadError> for Error {
197 #[cold]
198 fn from(e: util::CharReadError) -> Self {
199 use crate::util::CharReadError::{Io, UnexpectedEof, Utf8};
200 Self {
201 pos: TextPosition::new(),
202 kind: match e {
203 UnexpectedEof => ErrorKind::UnexpectedEof,
204 Utf8(reason) => ErrorKind::Utf8(reason),
205 Io(io_error) => ErrorKind::Io(io_error),
206 },
207 }
208 }
209}
210
211impl From<io::Error> for Error {
212 #[cold]
213 fn from(e: io::Error) -> Self {
214 Self {
215 pos: TextPosition::new(),
216 kind: ErrorKind::Io(e),
217 }
218 }
219}
220
221impl Clone for ErrorKind {
222 #[cold]
223 fn clone(&self) -> Self {
224 use self::ErrorKind::{Io, Syntax, UnexpectedEof, Utf8};
225 match self {
226 UnexpectedEof => UnexpectedEof,
227 Utf8(reason) => Utf8(*reason),
228 Io(io_error) => Io(io::Error::new(io_error.kind(), io_error.to_string())),
229 Syntax(msg) => Syntax(msg.clone()),
230 }
231 }
232}
233impl PartialEq for ErrorKind {
234 #[allow(deprecated)]
235 fn eq(&self, other: &Self) -> bool {
236 use self::ErrorKind::{Io, Syntax, UnexpectedEof, Utf8};
237 match (self, other) {
238 (UnexpectedEof, UnexpectedEof) => true,
239 (Utf8(left), Utf8(right)) => left == right,
240 (Io(left), Io(right)) =>
241 left.kind() == right.kind() &&
242 left.description() == right.description(),
243 (Syntax(left), Syntax(right)) =>
244 left == right,
245
246 (_, _) => false,
247 }
248 }
249}
250impl Eq for ErrorKind {}
251
252#[test]
253fn err_size() {
254 assert!(std::mem::size_of::<SyntaxError>() <= 24);
255}