use alloc::string::String;
use core::fmt;
#[derive(Debug, Clone)]
pub struct CsvError {
kind: CsvErrorKind,
#[allow(dead_code)]
span: Option<facet_reflect::Span>,
}
impl CsvError {
pub const fn new(kind: CsvErrorKind) -> Self {
Self { kind, span: None }
}
pub const fn with_span(kind: CsvErrorKind, span: facet_reflect::Span) -> Self {
Self {
kind,
span: Some(span),
}
}
pub const fn kind(&self) -> &CsvErrorKind {
&self.kind
}
}
impl fmt::Display for CsvError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match &self.kind {
CsvErrorKind::UnexpectedEof { expected } => {
write!(f, "unexpected end of input, expected {expected}")
}
CsvErrorKind::InvalidValue { message } => write!(f, "invalid value: {message}"),
CsvErrorKind::UnsupportedType { type_name } => {
write!(f, "unsupported type for CSV: {type_name}")
}
CsvErrorKind::TooFewFields { expected, got } => {
write!(f, "too few fields: expected {expected}, got {got}")
}
CsvErrorKind::TooManyFields { expected, got } => {
write!(f, "too many fields: expected {expected}, got {got}")
}
CsvErrorKind::InvalidUtf8 { message } => {
write!(f, "invalid UTF-8: {message}")
}
}
}
}
impl std::error::Error for CsvError {}
#[derive(Debug, Clone)]
pub enum CsvErrorKind {
UnexpectedEof {
expected: &'static str,
},
InvalidValue {
message: String,
},
UnsupportedType {
type_name: &'static str,
},
TooFewFields {
expected: usize,
got: usize,
},
TooManyFields {
expected: usize,
got: usize,
},
InvalidUtf8 {
message: String,
},
}
impl From<CsvErrorKind> for CsvError {
fn from(kind: CsvErrorKind) -> Self {
Self::new(kind)
}
}