Skip to main content

moxy_token/lex/
error.rs

1use crate::Span;
2
3#[derive(Debug, Clone)]
4pub struct LexError {
5    span: Span,
6    message: Option<String>,
7}
8
9impl LexError {
10    pub fn new(span: Span) -> Self {
11        Self { span, message: None }
12    }
13
14    pub fn message(mut self, message: impl std::fmt::Display) -> Self {
15        self.message = Some(message.to_string());
16        self
17    }
18
19    pub fn span(&self) -> Span {
20        self.span
21    }
22
23    pub fn is_reject(&self) -> bool {
24        self.message.is_none()
25    }
26}
27
28impl<T> From<LexError> for Result<T, LexError> {
29    fn from(value: LexError) -> Self {
30        Self::Err(value)
31    }
32}
33
34impl std::fmt::Display for LexError {
35    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
36        write!(f, "{}", self.message.as_deref().unwrap_or("string could not be parsed"))
37    }
38}
39
40impl std::error::Error for LexError {}