use arrow_schema::DataType;
use thiserror::Error;
#[derive(Debug, Clone, Error)]
pub enum SchemaError {
#[error("schema type mismatch: expected {expected}, got {actual}")]
TypeMismatch {
expected: DataType,
actual: DataType,
},
#[error("missing required field: {field_name}")]
MissingField {
field_name: String,
},
#[error("invalid schema: {message}")]
InvalidSchema {
message: String,
},
}
impl SchemaError {
pub fn type_mismatch(expected: DataType, actual: DataType) -> Self {
Self::TypeMismatch { expected, actual }
}
pub fn missing_field(field_name: impl Into<String>) -> Self {
Self::MissingField {
field_name: field_name.into(),
}
}
pub fn invalid(message: impl Into<String>) -> Self {
Self::InvalidSchema {
message: message.into(),
}
}
}
#[cfg(feature = "views")]
#[derive(Debug, Error)]
pub enum ViewAccessError {
#[error("index {index} out of bounds (len {len}){}", field_name.map(|n| format!(" for field '{n}'")).unwrap_or_default())]
OutOfBounds {
index: usize,
len: usize,
field_name: Option<&'static str>,
},
#[error("unexpected null at index {index}{}", field_name.map(|n| format!(" for field '{n}'")).unwrap_or_default())]
UnexpectedNull {
index: usize,
field_name: Option<&'static str>,
},
#[error("type mismatch: expected {expected}, got {actual}{}", field_name.map(|n| format!(" for field '{n}'")).unwrap_or_default())]
TypeMismatch {
expected: DataType,
actual: DataType,
field_name: Option<&'static str>,
},
#[error("custom validation error: {0}")]
Custom(Box<dyn std::error::Error + Send + Sync + 'static>),
}
#[cfg(feature = "views")]
impl From<core::convert::Infallible> for ViewAccessError {
fn from(x: core::convert::Infallible) -> Self {
match x {}
}
}
#[cfg(feature = "views")]
impl From<std::array::TryFromSliceError> for ViewAccessError {
fn from(e: std::array::TryFromSliceError) -> Self {
ViewAccessError::Custom(Box::new(e))
}
}