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
use std::io::{Error, ErrorKind};

use super::tokenize_error::TokenizeError;

#[derive(Debug, Clone, PartialEq, Eq)]
pub enum ParseError {
    TokenizeError(TokenizeError),
    IncorrectUseStatement {
        message: String,
        offset: usize
    },
    IncorrectObjectDefinition {
        message: String,
        offset: usize
    },
    IncorrectPropertyDefinition {
        message: String,
        offset: usize
    },
    IncorrectSyntax {
        message: String,
        offset: usize
    }
}

impl ParseError {
    pub fn get_message(&self) -> &str {
        match self {
            Self::TokenizeError(err) => &err.get_message(),
            Self::IncorrectUseStatement { message, .. } => message.as_str(),
            Self::IncorrectObjectDefinition { message, .. } => message.as_str(),
            Self::IncorrectPropertyDefinition { message, .. } => message.as_str(),
            Self::IncorrectSyntax { message, .. } => message.as_str()
        }
    }

    pub fn offset(mut self, num: usize) -> Self {
        match &mut self {
            Self::TokenizeError(err) => return Self::TokenizeError(err.clone().offset(num)),

            ParseError::IncorrectUseStatement { offset, .. } |
            ParseError::IncorrectObjectDefinition { offset, .. } |
            ParseError::IncorrectPropertyDefinition { offset, .. } |
            ParseError::IncorrectSyntax { offset, .. } => *offset += num
        }

        self
    }
}

impl Into<Error> for ParseError {
    fn into(self) -> Error {
        Error::new(ErrorKind::Other, self.get_message())
    }
}

impl From<TokenizeError> for ParseError {
    fn from(err: TokenizeError) -> Self {
        Self::TokenizeError(err)
    }
}