Skip to main content

sciforge_parser/html/
error.rs

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