1use std::fmt;
4
5#[derive(Debug, Clone, PartialEq)]
7pub enum InsightError {
8 CsvParse { line: usize, message: String },
10 JsonParse { message: String },
12 MissingValues { column: String, count: usize },
14 InsufficientData { min_required: usize, actual: usize },
16 InvalidParameter { name: String, message: String },
18 DegenerateData { reason: String },
20 ComputationFailed { operation: String, detail: String },
22 ColumnNotFound { name: String },
24 DimensionMismatch { expected: usize, actual: usize },
26 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}