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    /// XPDY0130
40    LimitExceeded,
41    NotPermitted,
42    Unknown,
43}
44impl ErrorKind {
45    /// String representation of error
46    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/// An error returned by an XPath, XQuery or XSLT function/method
77#[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}