Skip to main content

u_insight/
error.rs

1//! Error types for u-insight.
2
3use std::fmt;
4
5/// All errors produced by u-insight operations.
6#[derive(Debug, Clone, PartialEq)]
7pub enum InsightError {
8    /// CSV parsing failed.
9    CsvParse { line: usize, message: String },
10    /// JSON parsing failed.
11    JsonParse { message: String },
12    /// Column contains missing values where none are allowed.
13    MissingValues { column: String, count: usize },
14    /// Insufficient data rows/samples for the requested operation.
15    InsufficientData { min_required: usize, actual: usize },
16    /// Invalid parameter value for the requested operation.
17    InvalidParameter { name: String, message: String },
18    /// Data is degenerate (constant columns, singular matrix, etc.).
19    DegenerateData { reason: String },
20    /// Internal computation failed (eigenvalue decomposition, matrix construction, etc.).
21    ComputationFailed { operation: String, detail: String },
22    /// Column not found in DataFrame.
23    ColumnNotFound { name: String },
24    /// Dimension mismatch.
25    DimensionMismatch { expected: usize, actual: usize },
26    /// I/O error during file reading.
27    Io(String),
28}
29
30impl fmt::Display for InsightError {
31    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
32        match self {
33            Self::CsvParse { line, message } => {
34                write!(f, "CSV parse error at line {line}: {message}")
35            }
36            Self::JsonParse { message } => {
37                write!(f, "JSON parse error: {message}")
38            }
39            Self::MissingValues { column, count } => {
40                write!(f, "column '{column}' has {count} missing values")
41            }
42            Self::InsufficientData {
43                min_required,
44                actual,
45            } => {
46                write!(f, "need at least {min_required} rows, got {actual}")
47            }
48            Self::InvalidParameter { name, message } => {
49                write!(f, "invalid parameter '{name}': {message}")
50            }
51            Self::DegenerateData { reason } => {
52                write!(f, "degenerate data: {reason}")
53            }
54            Self::ComputationFailed { operation, detail } => {
55                write!(f, "{operation} failed: {detail}")
56            }
57            Self::ColumnNotFound { name } => {
58                write!(f, "column '{name}' not found")
59            }
60            Self::DimensionMismatch { expected, actual } => {
61                write!(f, "expected {expected} elements, got {actual}")
62            }
63            Self::Io(msg) => write!(f, "I/O error: {msg}"),
64        }
65    }
66}
67
68impl std::error::Error for InsightError {}
69
70impl From<std::io::Error> for InsightError {
71    fn from(e: std::io::Error) -> Self {
72        Self::Io(e.to_string())
73    }
74}