Skip to main content

sciforge_parser/markdown/
error.rs

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