einvoice_xpath_eval/
error.rs1use std::fmt;
5
6use einvoice_xdm::{ErrorCode as XdmCode, XdmError};
7
8#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
10#[non_exhaustive]
11pub enum ErrorCode {
12 Xpst0008,
14 Xpst0017,
16 Xpdy0002,
18 Xpdy0050,
20 Xpty0004,
22 Xpty0018,
24 Xpty0019,
26 Xpty0020,
28 Foar0001,
30 Foar0002,
32 Foca0002,
34 Foch0001,
36 Fodt0001,
38 Forg0001,
40 Forg0006,
42 Forx0001,
44 Forx0002,
46 Forx0003,
48 Forx0004,
50 Xtde1360,
52 Xtde1390,
54 Host,
57 Unsupported,
60 Internal,
62}
63
64impl ErrorCode {
65 #[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 #[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#[derive(Debug, Clone, PartialEq, Eq)]
111pub struct Error {
112 code: ErrorCode,
113 message: String,
114}
115
116impl Error {
117 #[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 #[must_use]
128 pub const fn code(&self) -> ErrorCode {
129 self.code
130 }
131
132 #[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
167pub 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}