gtk_ui_builder/parser/
parse_error.rs1use std::io::{Error, ErrorKind};
2
3use super::tokenize_error::TokenizeError;
4
5#[derive(Debug, Clone, PartialEq, Eq)]
6pub enum ParseError {
7 TokenizeError(TokenizeError),
8 IncorrectUseStatement {
9 message: String,
10 offset: usize
11 },
12 IncorrectObjectDefinition {
13 message: String,
14 offset: usize
15 },
16 IncorrectPropertyDefinition {
17 message: String,
18 offset: usize
19 },
20 IncorrectSyntax {
21 message: String,
22 offset: usize
23 },
24 IncorrectEventDefinition {
25 message: String,
26 offset: usize
27 }
28}
29
30impl ParseError {
31 pub fn get_message(&self) -> &str {
32 match self {
33 Self::TokenizeError(err) => &err.get_message(),
34
35 Self::IncorrectUseStatement { message, .. } |
36 Self::IncorrectObjectDefinition { message, .. } |
37 Self::IncorrectPropertyDefinition { message, .. } |
38 Self::IncorrectSyntax { message, .. } |
39 Self::IncorrectEventDefinition { message, .. } => message.as_str()
40 }
41 }
42
43 pub fn offset(mut self, num: usize) -> Self {
44 match &mut self {
45 Self::TokenizeError(err) => return Self::TokenizeError(err.clone().offset(num)),
46
47 Self::IncorrectUseStatement { offset, .. } |
48 Self::IncorrectObjectDefinition { offset, .. } |
49 Self::IncorrectPropertyDefinition { offset, .. } |
50 Self::IncorrectSyntax { offset, .. } |
51 Self::IncorrectEventDefinition { offset, .. } => *offset += num
52 }
53
54 self
55 }
56}
57
58impl Into<Error> for ParseError {
59 fn into(self) -> Error {
60 Error::new(ErrorKind::Other, self.get_message())
61 }
62}
63
64impl From<TokenizeError> for ParseError {
65 fn from(err: TokenizeError) -> Self {
66 Self::TokenizeError(err)
67 }
68}