1
use std::fmt;
2

            
3
/// A theme or keymap file problem; `line` is 1-based and absent for whole-file problems.
4
#[derive(Debug, Clone, PartialEq, Eq)]
5
pub struct ParseError {
6
    pub line: Option<usize>,
7
    pub message: String,
8
}
9

            
10
impl ParseError {
11
791
    pub(crate) fn at(line: usize, message: impl Into<String>) -> Self {
12
791
        Self {
13
791
            line: Some(line),
14
791
            message: message.into(),
15
791
        }
16
791
    }
17

            
18
12
    pub(crate) fn whole(message: impl Into<String>) -> Self {
19
12
        Self {
20
12
            line: None,
21
12
            message: message.into(),
22
12
        }
23
12
    }
24
}
25

            
26
impl fmt::Display for ParseError {
27
767
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
28
767
        match self.line {
29
760
            Some(line) => write!(f, "line {line}: {}", self.message),
30
7
            None => f.write_str(&self.message),
31
        }
32
767
    }
33
}
34

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