1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
use std::error;
use std::fmt;

#[derive(Debug, Clone)]
pub enum ParseError {
    MissingObjectBrace,
    MissingArrayBrace,
    FileNotFound,
    Error,
}

impl fmt::Display for ParseError {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match self {
            ParseError::MissingArrayBrace => write!(f, "Missing array brace"),
            ParseError::MissingObjectBrace => write!(f, "Missing object brace"),
            ParseError::FileNotFound => write!(f, "File not found"),
            ParseError::Error => write!(f, "Could not parse json"),
        }
    }
}

impl error::Error for ParseError {
    fn source(&self) -> Option<&(dyn error::Error + 'static)> {
        None
    }
}

pub type ParseResult<T> = Result<T, ParseError>;

#[derive(Debug, Copy, Clone)]
pub struct Range {
    pub start: Position,
    pub end: Position,
}

#[derive(Debug, Copy, Clone)]
pub struct Position {
    pub line: usize,
    pub char: usize,
    pub idx: usize,
}

impl Default for Position {
    fn default() -> Position {
        Position {
            line: 0,
            char: 0,
            idx: 0,
        }
    }
}

impl Position {
    pub fn new(line: usize, char: usize, idx: usize) -> Position {
        Position { line, char, idx }
    }

    pub fn add(&self, line: usize, char: usize) -> Position {
        Position {
            line: self.line + line,
            char: self.char + char,
            idx: self.idx + char,
        }
    }
}