json_pop/
error.rs

1use crate::parser;
2use std::fmt;
3use std::ops::Range;
4
5/// This is the error type returned by the parser.
6/// It's bounded by the lifetime of the source string
7/// which was parsed
8#[derive(Debug)]
9pub enum JsonPopError<'a> {
10    Parse(crate::parser::ParseError<'a>),
11    Io(std::io::Error),
12    /// This type is a never variation unless the testsuite is being run.
13    TestError(crate::extra::test_utils::TestError<'a>),
14}
15
16/// A top level error returned by processes or tests..
17/// It's not bounded by the lifetime of the string being parsed. 
18/// we should add error codes to these.
19///
20/// That will be a breaking change.
21#[derive(Debug)]
22pub enum TopLevelError {
23    TestingError,
24    ParseError,
25    Io(std::io::Error),
26}
27
28impl<'a> From<JsonPopError<'a>> for TopLevelError {
29    fn from(it: JsonPopError<'a>) -> TopLevelError {
30        match it {
31            JsonPopError::Parse(_) => {
32                // Convert to an error without the associated lifetimes.
33                TopLevelError::ParseError
34            }
35            JsonPopError::Io(err) => TopLevelError::Io(err),
36            JsonPopError::TestError(_) => TopLevelError::TestingError,
37        }
38    }
39}
40
41/// This error lives inside the the parsers Error type.
42/// So it's a sub-error of a parse error.
43#[derive(Debug)]
44pub enum CompilationError {
45    LexicalError { range: Range<usize> },
46    NumericalError { range: Range<usize> },
47    UnterminatedStringLiteral { range: Range<usize> },
48}
49
50impl fmt::Display for CompilationError {
51    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
52        write!(f, "{:#?}", self)
53    }
54}
55
56impl<'a> fmt::Display for JsonPopError<'a> {
57    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
58        write!(f, "{:#?}", self)
59    }
60}
61
62impl<'a> From<std::io::Error> for JsonPopError<'a> {
63    fn from(err: std::io::Error) -> Self {
64        JsonPopError::Io(err)
65    }
66}
67
68impl<'a> From<parser::ParseError<'a>> for JsonPopError<'a> {
69    fn from(err: parser::ParseError<'a>) -> Self {
70        JsonPopError::Parse(err)
71    }
72}