Skip to main content

sciforge_parser/toml/
error.rs

1#[derive(Clone, Copy, Debug, PartialEq, Eq)]
2pub enum TomlErrorKind {
3    Eof,
4    UnexpectedToken,
5    InvalidKey,
6    InvalidString,
7    InvalidNumber,
8    InvalidBool,
9    InvalidEscape,
10    DuplicateKey,
11    UnterminatedString,
12    MaxDepthExceeded,
13    MaxStringLengthExceeded,
14    MaxArrayLengthExceeded,
15    MaxTableLengthExceeded,
16    MaxNodeCountExceeded,
17}
18
19#[derive(Clone, Copy, Debug, PartialEq, Eq)]
20pub struct TomlErrorPosition {
21    pub line: usize,
22    pub column: usize,
23}
24
25#[derive(Clone, Copy, Debug, PartialEq, Eq)]
26pub struct TomlError {
27    pub kind: TomlErrorKind,
28    pub offset: usize,
29}
30
31impl TomlError {
32    pub(crate) const fn new(kind: TomlErrorKind, offset: usize) -> Self {
33        Self { kind, offset }
34    }
35
36    pub fn line_column(&self, input: &[u8]) -> TomlErrorPosition {
37        let end = core::cmp::min(self.offset, input.len());
38        let mut line = 1usize;
39        let mut col = 1usize;
40        let mut idx = 0usize;
41
42        while idx < end {
43            if input[idx] == b'\n' {
44                line += 1;
45                col = 1;
46            } else {
47                col += 1;
48            }
49            idx += 1;
50        }
51
52        TomlErrorPosition { line, column: col }
53    }
54}