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

#[derive(Debug, Clone, PartialEq, Eq)]
pub enum TokenizeError {
    IncorrectChar {
        message: String,
        wrong_string: String,
        offset: usize
    },
    IncorrectString {
        message: String,
        begin: usize,
        end: usize,
        wrong_string: String
    },
    IncorrectBrackets {
        message: String,
        begin: usize,
        end: usize,
        wrong_string: String
    }
}

impl TokenizeError {
    pub fn get_message(&self) -> &str {
        match self {
            Self::IncorrectChar     { message, .. } => message,
            Self::IncorrectString   { message, .. } => message,
            Self::IncorrectBrackets { message, .. } => message
        }.as_str()
    }

    pub fn offset(mut self, num: usize) -> Self {
        match &mut self {
            TokenizeError::IncorrectChar { offset, .. } => *offset += num,

            TokenizeError::IncorrectString { begin, end, .. } |
            TokenizeError::IncorrectBrackets { begin, end, .. } => {
                *begin += num;
                *end += num;
            }
        }

        self
    }
}

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