alopex_dataframe/
error.rs1use std::path::PathBuf;
2
3#[derive(Debug, thiserror::Error)]
5pub enum DataFrameError {
6 #[error("I/O error{path}: {source}", path = path_display(.path))]
8 Io {
9 source: std::io::Error,
10 path: Option<PathBuf>,
11 },
12
13 #[error("schema mismatch: {message}")]
15 SchemaMismatch { message: String },
16
17 #[error(
19 "type mismatch{column}: expected {expected}, got {actual}",
20 column = column_display(.column)
21 )]
22 TypeMismatch {
23 column: Option<String>,
24 expected: String,
25 actual: String,
26 },
27
28 #[error("column not found: {name}")]
30 ColumnNotFound { name: String },
31
32 #[error("invalid operation: {message}")]
34 InvalidOperation { message: String },
35
36 #[error("invalid configuration option '{option}': {message}")]
38 Configuration { option: String, message: String },
39
40 #[error("arrow error: {source}")]
42 Arrow { source: arrow::error::ArrowError },
43
44 #[error("parquet error: {source}")]
46 Parquet {
47 source: parquet::errors::ParquetError,
48 },
49}
50
51pub type Result<T> = std::result::Result<T, DataFrameError>;
53
54impl DataFrameError {
55 pub fn io(source: std::io::Error) -> Self {
57 Self::Io { source, path: None }
58 }
59
60 pub fn io_with_path(source: std::io::Error, path: impl Into<PathBuf>) -> Self {
62 Self::Io {
63 source,
64 path: Some(path.into()),
65 }
66 }
67
68 pub fn schema_mismatch(message: impl Into<String>) -> Self {
70 Self::SchemaMismatch {
71 message: message.into(),
72 }
73 }
74
75 pub fn type_mismatch(
77 column: impl Into<Option<String>>,
78 expected: impl Into<String>,
79 actual: impl Into<String>,
80 ) -> Self {
81 Self::TypeMismatch {
82 column: column.into(),
83 expected: expected.into(),
84 actual: actual.into(),
85 }
86 }
87
88 pub fn column_not_found(name: impl Into<String>) -> Self {
90 Self::ColumnNotFound { name: name.into() }
91 }
92
93 pub fn invalid_operation(message: impl Into<String>) -> Self {
95 Self::InvalidOperation {
96 message: message.into(),
97 }
98 }
99
100 pub fn configuration(option: impl Into<String>, message: impl Into<String>) -> Self {
102 Self::Configuration {
103 option: option.into(),
104 message: message.into(),
105 }
106 }
107}
108
109fn column_display(column: &Option<String>) -> String {
110 column
111 .as_ref()
112 .map(|c| format!(" for column '{c}'"))
113 .unwrap_or_default()
114}
115
116fn path_display(path: &Option<PathBuf>) -> String {
117 path.as_ref()
118 .map(|p| format!(" for path '{}'", p.display()))
119 .unwrap_or_default()
120}