json_position_parser/
types.rs

1use std::{error, fmt, ops::Add};
2
3#[derive(Debug, Clone)]
4pub enum ParseError {
5    MissingObjectBrace,
6    MissingArrayBrace,
7    InvalidType,
8    FileNotFound,
9    Error,
10}
11
12impl fmt::Display for ParseError {
13    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
14        match self {
15            ParseError::InvalidType => write!(f, "Found invalid type"),
16            ParseError::MissingArrayBrace => write!(f, "Missing array brace"),
17            ParseError::MissingObjectBrace => write!(f, "Missing object brace"),
18            ParseError::FileNotFound => write!(f, "File not found"),
19            ParseError::Error => write!(f, "Could not parse json"),
20        }
21    }
22}
23
24impl error::Error for ParseError {
25    fn source(&self) -> Option<&(dyn error::Error + 'static)> {
26        None
27    }
28}
29
30pub type ParseResult<T> = Result<T, ParseError>;
31
32#[derive(Debug, Copy, Clone)]
33pub struct Range {
34    pub start: Position,
35    pub end: Position,
36}
37
38impl Range {
39    pub fn new(start: Position, end: Position) -> Range {
40        Range { start, end }
41    }
42}
43
44#[derive(Debug, Copy, Clone)]
45pub struct Position {
46    pub line: usize,
47    pub char: usize,
48    pub idx: usize,
49}
50
51impl Default for Position {
52    fn default() -> Position {
53        Position {
54            line: 0,
55            char: 0,
56            idx: 0,
57        }
58    }
59}
60
61impl Position {
62    pub fn new(line: usize, char: usize, idx: usize) -> Position {
63        Position { line, char, idx }
64    }
65}
66
67impl Add for Position {
68    type Output = Self;
69    fn add(self, other: Self) -> Self::Output {
70        Self {
71            line: self.line + other.line,
72            char: self.char + other.char,
73            idx: self.idx + other.idx,
74        }
75    }
76}