boa/syntax/lexer/
error.rs1use super::Position;
9use std::{error::Error as StdError, fmt, io};
10
11#[derive(Debug)]
12pub enum Error {
13 IO(io::Error),
16
17 Syntax(Box<str>, Position),
24}
25
26impl From<io::Error> for Error {
27 fn from(err: io::Error) -> Self {
28 Self::IO(err)
29 }
30}
31
32impl Error {
33 pub(super) fn syntax<M, P>(err: M, pos: P) -> Self
35 where
36 M: Into<Box<str>>,
37 P: Into<Position>,
38 {
39 Self::Syntax(err.into(), pos.into())
40 }
41}
42
43impl fmt::Display for Error {
44 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
45 match self {
46 Self::IO(e) => write!(f, "I/O error: {}", e),
47 Self::Syntax(e, pos) => write!(f, "Syntax Error: {} at position: {}", e, pos),
48 }
49 }
50}
51
52impl StdError for Error {
53 fn source(&self) -> Option<&(dyn StdError + 'static)> {
54 match self {
55 Self::IO(err) => Some(err),
56 Self::Syntax(_, _) => None,
57 }
58 }
59}