Skip to main content

tvui/
error.rs

1use 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)]
5pub struct ParseError {
6    pub line: Option<usize>,
7    pub message: String,
8}
9
10impl ParseError {
11    pub(crate) fn at(line: usize, message: impl Into<String>) -> Self {
12        Self {
13            line: Some(line),
14            message: message.into(),
15        }
16    }
17
18    pub(crate) fn whole(message: impl Into<String>) -> Self {
19        Self {
20            line: None,
21            message: message.into(),
22        }
23    }
24}
25
26impl fmt::Display for ParseError {
27    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
28        match self.line {
29            Some(line) => write!(f, "line {line}: {}", self.message),
30            None => f.write_str(&self.message),
31        }
32    }
33}
34
35impl std::error::Error for ParseError {}