gix_config/parse/
error.rs1use std::fmt::Display;
2
3use crate::parse::Error;
4
5#[derive(PartialEq, Debug, Clone, Copy)]
7pub(crate) enum ParseNode {
8 SectionHeader,
9 Name,
10 Value,
11}
12
13impl Display for ParseNode {
14 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
15 match self {
16 Self::SectionHeader => write!(f, "section header"),
17 Self::Name => write!(f, "name"),
18 Self::Value => write!(f, "value"),
19 }
20 }
21}
22
23impl Error {
24 #[must_use]
27 pub const fn line_number(&self) -> usize {
28 self.line_number + 1
29 }
30
31 #[must_use]
33 pub fn remaining_data(&self) -> &[u8] {
34 &self.parsed_until
35 }
36}
37
38impl Display for Error {
39 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
40 write!(
41 f,
42 "Got an unexpected token on line {} while trying to parse a {}: ",
43 self.line_number + 1,
44 self.last_attempted_parser,
45 )?;
46
47 let data_size = self.parsed_until.len();
48 let data = std::str::from_utf8(&self.parsed_until);
49 match (data, data_size) {
50 (Ok(data), _) if data_size > 10 => {
51 write!(
52 f,
53 "'{}' ... ({} characters omitted)",
54 &data.chars().take(10).collect::<String>(),
55 data_size - 10
56 )
57 }
58 (Ok(data), _) => write!(f, "'{data}'"),
59 (Err(_), _) => self.parsed_until.fmt(f),
60 }
61 }
62}
63
64impl std::error::Error for Error {}