Skip to main content

sciforge_parser/yaml/
error.rs

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