use std::path::PathBuf;
pub type Result<T> = std::result::Result<T, Error>;
#[derive(Debug, thiserror::Error)]
pub enum Error {
#[error("{0}")]
Payload(String),
#[error("{0}")]
Layout(String),
#[error("{0}")]
Option(String),
#[error("Template error: {0}")]
Template(String),
#[error("PDF error: {0}")]
Pdf(String),
#[error("Signature error: {0}")]
Signature(String),
#[error("Feature disabled: {0}")]
FeatureDisabled(&'static str),
#[error("Template directory not found: {0}")]
TemplateDirNotFound(PathBuf),
#[error("I/O error: {0}")]
Io(#[from] std::io::Error),
#[error("JSON error: {0}")]
Json(#[from] serde_json::Error),
#[error("CSV error: {0}")]
Csv(#[from] csv::Error),
#[error("{0}")]
Message(String),
}
#[cfg(feature = "xlsx")]
impl From<rust_xlsxwriter::XlsxError> for Error {
fn from(error: rust_xlsxwriter::XlsxError) -> Self {
Self::Message(format!("XLSX error: {error}"))
}
}
#[cfg(feature = "arrow")]
impl From<arrow::error::ArrowError> for Error {
fn from(error: arrow::error::ArrowError) -> Self {
Self::option(format!("Arrow error: {error}"))
}
}
#[cfg(feature = "polars")]
impl From<polars::prelude::PolarsError> for Error {
fn from(error: polars::prelude::PolarsError) -> Self {
Self::option(format!("Polars error: {error}"))
}
}
#[cfg(feature = "duckdb")]
impl From<duckdb::Error> for Error {
fn from(error: duckdb::Error) -> Self {
Self::option(format!("DuckDB error: {error}"))
}
}
#[cfg(feature = "sqlite")]
impl From<rusqlite::Error> for Error {
fn from(error: rusqlite::Error) -> Self {
Self::option(format!("SQLite error: {error}"))
}
}
impl Error {
pub fn payload(message: impl Into<String>) -> Self {
Self::Payload(message.into())
}
pub fn layout(message: impl Into<String>) -> Self {
Self::Layout(message.into())
}
pub fn option(message: impl Into<String>) -> Self {
Self::Option(message.into())
}
pub fn template(message: impl Into<String>) -> Self {
Self::Template(message.into())
}
pub fn pdf(message: impl Into<String>) -> Self {
Self::Pdf(message.into())
}
pub fn signature(message: impl Into<String>) -> Self {
Self::Signature(message.into())
}
pub fn message(message: impl Into<String>) -> Self {
Self::Message(message.into())
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn constructors_and_display_cover_every_variant() {
assert_eq!(Error::payload("p").to_string(), "p");
assert_eq!(Error::layout("l").to_string(), "l");
assert_eq!(Error::option("o").to_string(), "o");
assert_eq!(Error::template("t").to_string(), "Template error: t");
assert_eq!(Error::pdf("p").to_string(), "PDF error: p");
assert_eq!(Error::signature("s").to_string(), "Signature error: s");
assert_eq!(Error::message("m").to_string(), "m");
assert_eq!(Error::FeatureDisabled("x").to_string(), "Feature disabled: x");
assert!(Error::TemplateDirNotFound(PathBuf::from("/tmp/x")).to_string().contains("/tmp/x"));
let io = Error::from(std::io::Error::other("io"));
assert!(io.to_string().contains("I/O error"));
let json = Error::from(serde_json::from_str::<serde_json::Value>("{").unwrap_err());
assert!(json.to_string().contains("JSON error"));
let mut csv = csv::Reader::from_reader("a,b\n1\n".as_bytes());
assert!(csv.records().any(|record| record.is_err()));
}
}