1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
use std::fmt::Display;

use crate::parse::Error;

/// A list of parsers that parsing can fail on. This is used for pretty-printing errors
#[derive(PartialEq, Debug, Clone, Copy)]
pub(crate) enum ParseNode {
    SectionHeader,
    Name,
    Value,
}

impl Display for ParseNode {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::SectionHeader => write!(f, "section header"),
            Self::Name => write!(f, "name"),
            Self::Value => write!(f, "value"),
        }
    }
}

impl Error {
    /// The one-indexed line number where the error occurred. This is determined
    /// by the number of newlines that were successfully parsed.
    #[must_use]
    pub const fn line_number(&self) -> usize {
        self.line_number + 1
    }

    /// The data that was left unparsed, which contains the cause of the parse error.
    #[must_use]
    pub fn remaining_data(&self) -> &[u8] {
        &self.parsed_until
    }
}

impl Display for Error {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(
            f,
            "Got an unexpected token on line {} while trying to parse a {}: ",
            self.line_number + 1,
            self.last_attempted_parser,
        )?;

        let data_size = self.parsed_until.len();
        let data = std::str::from_utf8(&self.parsed_until);
        match (data, data_size) {
            (Ok(data), _) if data_size > 10 => {
                write!(
                    f,
                    "'{}' ... ({} characters omitted)",
                    &data.chars().take(10).collect::<String>(),
                    data_size - 10
                )
            }
            (Ok(data), _) => write!(f, "'{data}'"),
            (Err(_), _) => self.parsed_until.fmt(f),
        }
    }
}

impl std::error::Error for Error {}