Skip to main content

sciforge_parser/csv/
error.rs

1#[derive(Clone, Copy, Debug, PartialEq, Eq)]
2pub enum CsvErrorKind {
3    UnexpectedQuote,
4    UnterminatedQuotedField,
5    InvalidUtf8,
6    TrailingCharactersAfterQuote,
7    MaxRowsExceeded,
8    MaxColumnsExceeded,
9    MaxFieldLengthExceeded,
10    MaxNodeCountExceeded,
11}
12
13#[derive(Clone, Copy, Debug, PartialEq, Eq)]
14pub struct CsvErrorPosition {
15    pub line: usize,
16    pub column: usize,
17}
18
19#[derive(Clone, Copy, Debug, PartialEq, Eq)]
20pub struct CsvError {
21    pub kind: CsvErrorKind,
22    pub offset: usize,
23}
24
25impl CsvError {
26    pub(crate) const fn new(kind: CsvErrorKind, offset: usize) -> Self {
27        Self { kind, offset }
28    }
29
30    pub fn line_column(&self, input: &[u8]) -> CsvErrorPosition {
31        let end = core::cmp::min(self.offset, input.len());
32        let mut line = 1usize;
33        let mut col = 1usize;
34        let mut idx = 0usize;
35
36        while idx < end {
37            if input[idx] == b'\n' {
38                line += 1;
39                col = 1;
40            } else {
41                col += 1;
42            }
43            idx += 1;
44        }
45
46        CsvErrorPosition { line, column: col }
47    }
48}