Skip to main content

xrust/
xdmerror.rs

1//! XDM, XPath, XQuery, and XSLT errors.
2
3use core::str;
4use qualname::QName;
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    /// SXXP0003 - attribute declared more than once
38    DuplicateAttribute,
39    Unknown,
40}
41impl ErrorKind {
42    /// String representation of error
43    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/// An error returned by an XPath, XQuery or XSLT function/method
72#[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}