Skip to main content

blackline_core/
error.rs

1//! Shared error type for package-level operations.
2
3use std::path::PathBuf;
4
5/// Errors produced by the shared OPC / XML layers.
6#[derive(Debug, thiserror::Error)]
7pub enum CoreError {
8    /// Filesystem I/O failure, annotated with the path involved.
9    #[error("I/O error on {path}: {source}")]
10    Io {
11        /// The path being read or written.
12        path: PathBuf,
13        /// The underlying I/O error.
14        #[source]
15        source: std::io::Error,
16    },
17
18    /// The file is not a readable ZIP/OPC package.
19    #[error("not a valid ZIP/OPC package: {0}")]
20    Zip(String),
21
22    /// An XML part failed to parse.
23    #[error("XML parse error in {part}: {message}")]
24    Xml {
25        /// Package part name (e.g. `word/document.xml`).
26        part: String,
27        /// Parser message.
28        message: String,
29    },
30
31    /// A required package part is missing.
32    #[error("missing package part: {0}")]
33    MissingPart(String),
34
35    /// A node path did not resolve.
36    #[error("XML path not found: {0}")]
37    Path(String),
38
39    /// An XML formula or selector failed to parse or evaluate.
40    #[error("XML formula error: {0}")]
41    Formula(String),
42
43    /// An RFC 5261 XML patch failed to parse or apply.
44    #[error("XML patch error: {0}")]
45    Patch(String),
46
47    /// An XQuery Update expression failed to parse or compile.
48    #[error("XML update error: {0}")]
49    Update(String),
50
51    /// Any other invariant violation.
52    #[error("{0}")]
53    Invalid(String),
54}
55
56impl CoreError {
57    /// Construct an [`CoreError::Io`] from a path and source error.
58    pub fn io(path: impl Into<PathBuf>, source: std::io::Error) -> Self {
59        CoreError::Io {
60            path: path.into(),
61            source,
62        }
63    }
64
65    /// Construct an [`CoreError::Xml`] from a part name and message.
66    pub fn xml(part: impl Into<String>, message: impl Into<String>) -> Self {
67        CoreError::Xml {
68            part: part.into(),
69            message: message.into(),
70        }
71    }
72
73    /// Construct an [`CoreError::Invalid`].
74    pub fn invalid(msg: impl Into<String>) -> Self {
75        CoreError::Invalid(msg.into())
76    }
77
78    /// Construct an [`CoreError::Formula`].
79    pub fn formula(msg: impl Into<String>) -> Self {
80        CoreError::Formula(msg.into())
81    }
82
83    /// Construct an [`CoreError::Patch`].
84    pub fn patch(msg: impl Into<String>) -> Self {
85        CoreError::Patch(msg.into())
86    }
87
88    /// Construct an [`CoreError::Update`].
89    pub fn update(msg: impl Into<String>) -> Self {
90        CoreError::Update(msg.into())
91    }
92}
93
94impl From<String> for CoreError {
95    fn from(msg: String) -> Self {
96        CoreError::Invalid(msg)
97    }
98}