facet_csv/
error.rs

1//! CSV parsing error types.
2
3use alloc::string::String;
4use core::fmt;
5
6/// Error type for CSV parsing.
7#[derive(Debug, Clone)]
8pub struct CsvError {
9    kind: CsvErrorKind,
10    /// Source span of the error, if available.
11    #[allow(dead_code)]
12    span: Option<facet_reflect::Span>,
13}
14
15impl CsvError {
16    /// Create a new error with the given kind.
17    pub fn new(kind: CsvErrorKind) -> Self {
18        Self { kind, span: None }
19    }
20
21    /// Create a new error with the given kind and span.
22    pub fn with_span(kind: CsvErrorKind, span: facet_reflect::Span) -> Self {
23        Self {
24            kind,
25            span: Some(span),
26        }
27    }
28
29    /// Get the error kind.
30    pub fn kind(&self) -> &CsvErrorKind {
31        &self.kind
32    }
33}
34
35impl fmt::Display for CsvError {
36    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
37        match &self.kind {
38            CsvErrorKind::UnexpectedEof { expected } => {
39                write!(f, "unexpected end of input, expected {expected}")
40            }
41            CsvErrorKind::InvalidValue { message } => write!(f, "invalid value: {message}"),
42            CsvErrorKind::UnsupportedType { type_name } => {
43                write!(f, "unsupported type for CSV: {type_name}")
44            }
45            CsvErrorKind::TooFewFields { expected, got } => {
46                write!(f, "too few fields: expected {expected}, got {got}")
47            }
48            CsvErrorKind::TooManyFields { expected, got } => {
49                write!(f, "too many fields: expected {expected}, got {got}")
50            }
51            CsvErrorKind::InvalidUtf8 { message } => {
52                write!(f, "invalid UTF-8: {message}")
53            }
54        }
55    }
56}
57
58impl std::error::Error for CsvError {}
59
60/// Specific kinds of CSV errors.
61#[derive(Debug, Clone)]
62pub enum CsvErrorKind {
63    /// Unexpected end of input.
64    UnexpectedEof {
65        /// What was expected at this point.
66        expected: &'static str,
67    },
68    /// Invalid value.
69    InvalidValue {
70        /// Error message.
71        message: String,
72    },
73    /// Unsupported type for CSV format.
74    UnsupportedType {
75        /// Name of the unsupported type.
76        type_name: &'static str,
77    },
78    /// Too few fields in the CSV row.
79    TooFewFields {
80        /// Expected number of fields.
81        expected: usize,
82        /// Actual number of fields.
83        got: usize,
84    },
85    /// Too many fields in the CSV row.
86    TooManyFields {
87        /// Expected number of fields.
88        expected: usize,
89        /// Actual number of fields.
90        got: usize,
91    },
92    /// Invalid UTF-8 in input.
93    InvalidUtf8 {
94        /// The UTF-8 error details.
95        message: String,
96    },
97}
98
99impl From<CsvErrorKind> for CsvError {
100    fn from(kind: CsvErrorKind) -> Self {
101        Self::new(kind)
102    }
103}