xrust/
xdmerror.rs

1//! XDM, XPath, XQuery, and XSLT errors.
2
3use crate::qname::QualifiedName;
4use core::str;
5use std::fmt;
6use std::fmt::Formatter;
7
8/// Errors defined in XPath
9#[derive(Copy, Clone, Debug, PartialEq)]
10pub enum ErrorKind {
11    StaticAbsent,
12    /// XPST0001
13    DynamicAbsent,
14    /// XPDY0002
15    StaticSyntax,
16    /// XPST0003
17    TypeError,
18    /// XPTY0004
19    StaticData,
20    /// XPST0005
21    StaticUndefined,
22    /// XPST0008
23    StaticNamespace,
24    /// XPST0010
25    StaticBadFunction,
26    /// XPST0017
27    MixedTypes,
28    /// XPTY0018
29    NotNodes,
30    /// XPTY0019
31    ContextNotNode,
32    /// XPTY0020
33    Terminated,
34    /// XTMM9000 - (http://)www.w3.org/2005/xqt-errors
35    NotImplemented,
36    ParseError,
37    Unknown,
38}
39impl ErrorKind {
40    /// String representation of error
41    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/// An error returned by an XPath, XQuery or XSLT function/method
69#[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}