Skip to main content

isis_report_lib/
error.rs

1//! Error type shared by the public API.
2
3use std::path::PathBuf;
4
5/// Result alias used across the crate.
6pub type Result<T> = std::result::Result<T, Error>;
7
8/// Everything the library can fail with.
9///
10/// Messages are intentionally about the contract (field names, shapes,
11/// options), never about report values: payloads are untrusted input.
12#[derive(Debug, thiserror::Error)]
13pub enum Error {
14    #[error("{0}")]
15    Payload(String),
16    #[error("{0}")]
17    Layout(String),
18    #[error("{0}")]
19    Option(String),
20    #[error("Template error: {0}")]
21    Template(String),
22    #[error("PDF error: {0}")]
23    Pdf(String),
24    #[error("Signature error: {0}")]
25    Signature(String),
26    #[error("Feature disabled: {0}")]
27    FeatureDisabled(&'static str),
28    #[error("Template directory not found: {0}")]
29    TemplateDirNotFound(PathBuf),
30    #[error("I/O error: {0}")]
31    Io(#[from] std::io::Error),
32    #[error("JSON error: {0}")]
33    Json(#[from] serde_json::Error),
34    #[error("CSV error: {0}")]
35    Csv(#[from] csv::Error),
36    #[error("{0}")]
37    Message(String),
38}
39
40#[cfg(feature = "xlsx")]
41impl From<rust_xlsxwriter::XlsxError> for Error {
42    fn from(error: rust_xlsxwriter::XlsxError) -> Self {
43        Self::Message(format!("XLSX error: {error}"))
44    }
45}
46
47#[cfg(feature = "arrow")]
48impl From<arrow::error::ArrowError> for Error {
49    fn from(error: arrow::error::ArrowError) -> Self {
50        Self::option(format!("Arrow error: {error}"))
51    }
52}
53
54#[cfg(feature = "polars")]
55impl From<polars::prelude::PolarsError> for Error {
56    fn from(error: polars::prelude::PolarsError) -> Self {
57        Self::option(format!("Polars error: {error}"))
58    }
59}
60
61#[cfg(feature = "duckdb")]
62impl From<duckdb::Error> for Error {
63    fn from(error: duckdb::Error) -> Self {
64        Self::option(format!("DuckDB error: {error}"))
65    }
66}
67
68#[cfg(feature = "sqlite")]
69impl From<rusqlite::Error> for Error {
70    fn from(error: rusqlite::Error) -> Self {
71        Self::option(format!("SQLite error: {error}"))
72    }
73}
74
75impl Error {
76    pub fn payload(message: impl Into<String>) -> Self {
77        Self::Payload(message.into())
78    }
79
80    pub fn layout(message: impl Into<String>) -> Self {
81        Self::Layout(message.into())
82    }
83
84    pub fn option(message: impl Into<String>) -> Self {
85        Self::Option(message.into())
86    }
87
88    pub fn template(message: impl Into<String>) -> Self {
89        Self::Template(message.into())
90    }
91
92    pub fn pdf(message: impl Into<String>) -> Self {
93        Self::Pdf(message.into())
94    }
95
96    pub fn signature(message: impl Into<String>) -> Self {
97        Self::Signature(message.into())
98    }
99
100    pub fn message(message: impl Into<String>) -> Self {
101        Self::Message(message.into())
102    }
103}
104
105#[cfg(test)]
106mod tests {
107    use super::*;
108
109    #[test]
110    fn constructors_and_display_cover_every_variant() {
111        assert_eq!(Error::payload("p").to_string(), "p");
112        assert_eq!(Error::layout("l").to_string(), "l");
113        assert_eq!(Error::option("o").to_string(), "o");
114        assert_eq!(Error::template("t").to_string(), "Template error: t");
115        assert_eq!(Error::pdf("p").to_string(), "PDF error: p");
116        assert_eq!(Error::signature("s").to_string(), "Signature error: s");
117        assert_eq!(Error::message("m").to_string(), "m");
118        assert_eq!(Error::FeatureDisabled("x").to_string(), "Feature disabled: x");
119        assert!(Error::TemplateDirNotFound(PathBuf::from("/tmp/x")).to_string().contains("/tmp/x"));
120
121        let io = Error::from(std::io::Error::other("io"));
122        assert!(io.to_string().contains("I/O error"));
123        let json = Error::from(serde_json::from_str::<serde_json::Value>("{").unwrap_err());
124        assert!(json.to_string().contains("JSON error"));
125        let mut csv = csv::Reader::from_reader("a,b\n1\n".as_bytes());
126        assert!(csv.records().any(|record| record.is_err()));
127    }
128}