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 Unknown,
40}
41impl ErrorKind {
42 pub fn to_string(&self) -> &'static str {
44 match *self {
45 ErrorKind::StaticAbsent => "a component of the static context is absent",
46 ErrorKind::DynamicAbsent => "a component of the dynamic context is absent",
47 ErrorKind::StaticSyntax => "syntax error",
48 ErrorKind::TypeError => "type error",
49 ErrorKind::StaticData => "wrong static type",
50 ErrorKind::StaticUndefined => "undefined name",
51 ErrorKind::StaticNamespace => "namespace axis not supported",
52 ErrorKind::StaticBadFunction => "function call name and arity do not match",
53 ErrorKind::MixedTypes => "result of path operator contains both nodes and non-nodes",
54 ErrorKind::NotNodes => "path expression is not a sequence of nodes",
55 ErrorKind::ContextNotNode => "context item is not a node for an axis step",
56 ErrorKind::Terminated => "application has voluntarily terminated processing",
57 ErrorKind::NotImplemented => "not implemented",
58 ErrorKind::Unknown => "unknown",
59 ErrorKind::ParseError => "XML Parse error",
60 ErrorKind::DuplicateAttribute => "XML parse error - attribute declared more than once",
61 }
62 }
63}
64
65impl fmt::Display for ErrorKind {
66 fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
67 write!(f, "{}", self.to_string())
68 }
69}
70
71#[derive(Clone)]
73pub struct Error {
74 pub kind: ErrorKind,
75 pub message: String,
76 pub code: Option<QName>,
77}
78
79impl std::error::Error for Error {}
80
81impl Error {
82 pub fn new(kind: ErrorKind, message: impl Into<String>) -> Self {
83 Error {
84 kind,
85 message: message.into(),
86 code: None,
87 }
88 }
89 pub fn new_with_code(kind: ErrorKind, message: impl Into<String>, code: Option<QName>) -> Self {
90 Error {
91 kind,
92 message: message.into(),
93 code,
94 }
95 }
96}
97
98impl fmt::Debug for Error {
99 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
100 f.write_str(&self.message)
101 }
102}
103
104impl fmt::Display for Error {
105 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
106 f.write_str(&self.message)
107 }
108}