Skip to main content

moxy_token/lex/
error.rs

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