Skip to main content

einvoice_xpath_eval/
error.rs

1//! Static and dynamic errors, with the error codes of XPath 2.0, F&O 2.0 and XSLT 2.0
2//! that Saxon-HE reports.
3
4use std::fmt;
5
6use einvoice_xdm::{ErrorCode as XdmCode, XdmError};
7
8/// The error code of a failed compilation or evaluation.
9#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
10#[non_exhaustive]
11pub enum ErrorCode {
12    /// `XPST0008`: a variable that is not in scope (static).
13    Xpst0008,
14    /// `XPST0017`: no function with that name and arity (static).
15    Xpst0017,
16    /// `XPDY0002`: the context item is absent.
17    Xpdy0002,
18    /// `XPDY0050`: the root of the tree containing the context item is not a document node.
19    Xpdy0050,
20    /// `XPTY0004`: type error.
21    Xpty0004,
22    /// `XPTY0018`: the last step of a path returns both nodes and atomic values.
23    Xpty0018,
24    /// `XPTY0019`: a step of a path (other than the last) returns an atomic value.
25    Xpty0019,
26    /// `XPTY0020`: the context item of an axis step is not a node.
27    Xpty0020,
28    /// `FOAR0001`: division by zero.
29    Foar0001,
30    /// `FOAR0002`: numeric operation overflow.
31    Foar0002,
32    /// `FOCA0002`: invalid lexical value.
33    Foca0002,
34    /// `FOCH0001`: a code point that is not a valid XML character.
35    Foch0001,
36    /// `FODT0001`: date/time value out of range.
37    Fodt0001,
38    /// `FORG0001`: invalid value for a cast or constructor.
39    Forg0001,
40    /// `FORG0006`: invalid argument type (effective boolean value, `sum`).
41    Forg0006,
42    /// `FORX0001`: invalid regular expression flags.
43    Forx0001,
44    /// `FORX0002`: invalid regular expression.
45    Forx0002,
46    /// `FORX0003`: the regular expression matches a zero-length string.
47    Forx0003,
48    /// `FORX0004`: invalid replacement string.
49    Forx0004,
50    /// `XTDE1360`: `current()` where no current item exists (XSLT).
51    Xtde1360,
52    /// `XTDE1390`: the argument of `system-property()` is not a valid `QName` (XSLT).
53    Xtde1390,
54    /// An error reported by the host (the XSLT layer): variables, user functions,
55    /// `document()`. The code is in the message.
56    Host,
57    /// Not an XPath error: a valid construct or value that this crate refuses on purpose
58    /// rather than evaluating it differently from the reference engine.
59    Unsupported,
60    /// Not an XPath error: a broken invariant of this crate (a bug).
61    Internal,
62}
63
64impl ErrorCode {
65    /// The code as written in the specifications, e.g. `"XPTY0004"`.
66    #[must_use]
67    pub const fn as_str(self) -> &'static str {
68        match self {
69            Self::Xpst0008 => "XPST0008",
70            Self::Xpst0017 => "XPST0017",
71            Self::Xpdy0002 => "XPDY0002",
72            Self::Xpdy0050 => "XPDY0050",
73            Self::Xpty0004 => "XPTY0004",
74            Self::Xpty0018 => "XPTY0018",
75            Self::Xpty0019 => "XPTY0019",
76            Self::Xpty0020 => "XPTY0020",
77            Self::Foar0001 => "FOAR0001",
78            Self::Foar0002 => "FOAR0002",
79            Self::Foca0002 => "FOCA0002",
80            Self::Foch0001 => "FOCH0001",
81            Self::Fodt0001 => "FODT0001",
82            Self::Forg0001 => "FORG0001",
83            Self::Forg0006 => "FORG0006",
84            Self::Forx0001 => "FORX0001",
85            Self::Forx0002 => "FORX0002",
86            Self::Forx0003 => "FORX0003",
87            Self::Forx0004 => "FORX0004",
88            Self::Xtde1360 => "XTDE1360",
89            Self::Xtde1390 => "XTDE1390",
90            Self::Host => "HOST",
91            Self::Unsupported => "UNSUPPORTED",
92            Self::Internal => "INTERNAL",
93        }
94    }
95
96    /// Whether the error is raised when an expression is compiled rather than evaluated.
97    #[must_use]
98    pub const fn is_static(self) -> bool {
99        matches!(self, Self::Xpst0008 | Self::Xpst0017)
100    }
101}
102
103impl fmt::Display for ErrorCode {
104    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
105        f.write_str(self.as_str())
106    }
107}
108
109/// A failed compilation or evaluation.
110#[derive(Debug, Clone, PartialEq, Eq)]
111pub struct Error {
112    code: ErrorCode,
113    message: String,
114}
115
116impl Error {
117    /// A new error.
118    #[must_use]
119    pub fn new(code: ErrorCode, message: impl Into<String>) -> Self {
120        Self {
121            code,
122            message: message.into(),
123        }
124    }
125
126    /// The error code.
127    #[must_use]
128    pub const fn code(&self) -> ErrorCode {
129        self.code
130    }
131
132    /// The explanation.
133    #[must_use]
134    pub fn message(&self) -> &str {
135        &self.message
136    }
137}
138
139impl fmt::Display for Error {
140    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
141        write!(f, "{}: {}", self.code, self.message)
142    }
143}
144
145impl std::error::Error for Error {}
146
147impl From<XdmError> for Error {
148    fn from(e: XdmError) -> Self {
149        let code = match e.code() {
150            XdmCode::Foar0001 => ErrorCode::Foar0001,
151            XdmCode::Foar0002 => ErrorCode::Foar0002,
152            XdmCode::Foca0002 => ErrorCode::Foca0002,
153            XdmCode::Fodt0001 => ErrorCode::Fodt0001,
154            XdmCode::Forg0001 => ErrorCode::Forg0001,
155            XdmCode::Forg0006 => ErrorCode::Forg0006,
156            XdmCode::Forx0001 => ErrorCode::Forx0001,
157            XdmCode::Forx0002 => ErrorCode::Forx0002,
158            XdmCode::Forx0003 => ErrorCode::Forx0003,
159            XdmCode::Forx0004 => ErrorCode::Forx0004,
160            XdmCode::Xpty0004 => ErrorCode::Xpty0004,
161            _ => ErrorCode::Unsupported,
162        };
163        Self::new(code, e.message())
164    }
165}
166
167/// The result of a compilation or evaluation.
168pub type Result<T> = std::result::Result<T, Error>;
169
170pub(crate) fn err<T>(code: ErrorCode, message: impl Into<String>) -> Result<T> {
171    Err(Error::new(code, message))
172}