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