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 67 68 69 70 71 72 73 74 75 76
use std::{error, fmt, ops::Add}; #[derive(Debug, Clone)] pub enum ParseError { MissingObjectBrace, MissingArrayBrace, InvalidType, FileNotFound, Error, } impl fmt::Display for ParseError { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { match self { ParseError::InvalidType => write!(f, "Found invalid type"), 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, } impl Range { pub fn new(start: Position, end: Position) -> Range { Range { start, end } } } #[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 } } } impl Add for Position { type Output = Self; fn add(self, other: Self) -> Self::Output { Self { line: self.line + other.line, char: self.char + other.char, idx: self.idx + other.idx, } } }