Skip to main content

xml_arrow/
error.rs

1use std::fmt;
2
3/// Result with [`Error`].
4pub type Result<T> = std::result::Result<T, Error>;
5
6/// XML-to-Arrow conversion error.
7#[derive(Debug)]
8pub enum Error {
9    /// Invalid schema, path, or options.
10    Configuration(String),
11    /// XML parsing error.
12    Xml(roxmltree::Error),
13    /// Unsupported XML feature.
14    UnsupportedXml(&'static str),
15    /// Invalid or missing field value.
16    Field {
17        /// Column name.
18        name: String,
19        /// Relative XML path.
20        path: String,
21        /// Failure reason.
22        message: String,
23    },
24    /// Flush before appending more rows.
25    BatchFull,
26    /// Arrow error.
27    Arrow(arrow_schema::ArrowError),
28}
29
30impl fmt::Display for Error {
31    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
32        match self {
33            Self::Configuration(message) => write!(f, "invalid configuration: {message}"),
34            Self::Xml(source) => write!(f, "XML: {source}"),
35            Self::UnsupportedXml(message) => write!(f, "unsupported XML: {message}"),
36            Self::Field {
37                name,
38                path,
39                message,
40            } => write!(f, "field {name:?} at {path:?}: {message}"),
41            Self::BatchFull => f.write_str("batch is full; flush before appending"),
42            Self::Arrow(source) => write!(f, "Arrow: {source}"),
43        }
44    }
45}
46
47impl std::error::Error for Error {
48    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
49        match self {
50            Self::Xml(source) => Some(source),
51            Self::Arrow(source) => Some(source),
52            _ => None,
53        }
54    }
55}
56
57impl From<roxmltree::Error> for Error {
58    fn from(source: roxmltree::Error) -> Self {
59        Self::Xml(source)
60    }
61}
62
63impl From<arrow_schema::ArrowError> for Error {
64    fn from(source: arrow_schema::ArrowError) -> Self {
65        Self::Arrow(source)
66    }
67}