isis-report-lib 0.2.1

Tabular reports with hierarchical layouts: HTML, PDF, XLSX, CSV and plain text
//! Error type shared by the public API.

use std::path::PathBuf;

/// Result alias used across the crate.
pub type Result<T> = std::result::Result<T, Error>;

/// Everything the library can fail with.
///
/// Messages are intentionally about the contract (field names, shapes,
/// options), never about report values: payloads are untrusted input.
#[derive(Debug, thiserror::Error)]
pub enum Error {
    /// Invalid payload shape or values.
    #[error("{0}")]
    Payload(String),
    /// Layout building failure.
    #[error("{0}")]
    Layout(String),
    /// Invalid option or argument.
    #[error("{0}")]
    Option(String),
    /// Template resolution or rendering failure.
    #[error("Template error: {0}")]
    Template(String),
    /// PDF backend failure.
    #[error("PDF error: {0}")]
    Pdf(String),
    /// Signature or verification failure.
    #[error("Signature error: {0}")]
    Signature(String),
    /// The requested feature was compiled out.
    #[error("Feature disabled: {0}")]
    FeatureDisabled(&'static str),
    /// The configured template directory does not exist.
    #[error("Template directory not found: {0}")]
    TemplateDirNotFound(PathBuf),
    /// Filesystem error.
    #[error("I/O error: {0}")]
    Io(#[from] std::io::Error),
    /// JSON serialization/deserialization error.
    #[error("JSON error: {0}")]
    Json(#[from] serde_json::Error),
    /// CSV parsing/serialization error.
    #[error("CSV error: {0}")]
    Csv(#[from] csv::Error),
    /// Any other error with a ready message.
    #[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 {
    /// Create a payload error.
    pub fn payload(message: impl Into<String>) -> Self {
        Self::Payload(message.into())
    }

    /// Create a layout error.
    pub fn layout(message: impl Into<String>) -> Self {
        Self::Layout(message.into())
    }

    /// Create an option error.
    pub fn option(message: impl Into<String>) -> Self {
        Self::Option(message.into())
    }

    /// Create a template error.
    pub fn template(message: impl Into<String>) -> Self {
        Self::Template(message.into())
    }

    /// Create a PDF error.
    pub fn pdf(message: impl Into<String>) -> Self {
        Self::Pdf(message.into())
    }

    /// Create a signature error.
    pub fn signature(message: impl Into<String>) -> Self {
        Self::Signature(message.into())
    }

    /// Create a generic message error.
    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()));
    }
}