1use core::str;
4use qualname::QName;
5use std::fmt;
6use std::fmt::Formatter;
7
8#[derive(Copy, Clone, Debug, PartialEq)]
10pub enum ErrorKind {
11 StaticAbsent,
12 DynamicAbsent,
14 StaticSyntax,
16 TypeError,
18 StaticData,
20 StaticUndefined,
22 StaticNamespace,
24 StaticBadFunction,
26 MixedTypes,
28 NotNodes,
30 ContextNotNode,
32 Terminated,
34 NotImplemented,
36 ParseError,
37 DuplicateAttribute,
39 LimitExceeded,
41 NotPermitted,
42 Unknown,
43}
44impl ErrorKind {
45 pub fn to_string(&self) -> &'static str {
47 match *self {
48 ErrorKind::StaticAbsent => "a component of the static context is absent",
49 ErrorKind::DynamicAbsent => "a component of the dynamic context is absent",
50 ErrorKind::StaticSyntax => "syntax error",
51 ErrorKind::TypeError => "type error",
52 ErrorKind::StaticData => "wrong static type",
53 ErrorKind::StaticUndefined => "undefined name",
54 ErrorKind::StaticNamespace => "namespace axis not supported",
55 ErrorKind::StaticBadFunction => "function call name and arity do not match",
56 ErrorKind::MixedTypes => "result of path operator contains both nodes and non-nodes",
57 ErrorKind::NotNodes => "path expression is not a sequence of nodes",
58 ErrorKind::ContextNotNode => "context item is not a node for an axis step",
59 ErrorKind::Terminated => "application has voluntarily terminated processing",
60 ErrorKind::NotImplemented => "not implemented",
61 ErrorKind::Unknown => "unknown",
62 ErrorKind::ParseError => "XML Parse error",
63 ErrorKind::DuplicateAttribute => "XML parse error - attribute declared more than once",
64 ErrorKind::LimitExceeded => "implementation-defined limit exceeded",
65 ErrorKind::NotPermitted => "not permitted",
66 }
67 }
68}
69
70impl fmt::Display for ErrorKind {
71 fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
72 write!(f, "{}", self.to_string())
73 }
74}
75
76#[derive(Clone)]
78pub struct Error {
79 pub kind: ErrorKind,
80 pub message: String,
81 pub code: Option<QName>,
82}
83
84impl std::error::Error for Error {}
85
86impl Error {
87 pub fn new(kind: ErrorKind, message: impl Into<String>) -> Self {
88 Error {
89 kind,
90 message: message.into(),
91 code: None,
92 }
93 }
94 pub fn new_with_code(kind: ErrorKind, message: impl Into<String>, code: Option<QName>) -> Self {
95 Error {
96 kind,
97 message: message.into(),
98 code,
99 }
100 }
101}
102
103impl fmt::Debug for Error {
104 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
105 f.write_str(&self.message)
106 }
107}
108
109impl fmt::Display for Error {
110 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
111 f.write_str(&self.message)
112 }
113}