tref/
error.rs

1use std::error::Error;
2use std::fmt;
3
4#[derive(Debug)]
5/// Parse TREF document error.
6pub struct ParseTreeError {
7    message: String,
8    line: usize
9}
10
11impl ParseTreeError {
12    /// Create new parse tree error.
13    /// 
14    /// # Arguments
15    /// 
16    /// * `msg` - Error message.
17    /// * `line` - Document line where the error hapened.
18    /// 
19    /// # Return
20    /// 
21    /// * An error model.
22    ///
23    pub fn new(msg: &str, line: usize) -> Self {
24        ParseTreeError {
25            message: String::from(msg),
26            line
27        }
28    }
29
30    /// Get error line.
31    /// 
32    /// # Return
33    /// 
34    /// * Line.
35    ///
36    pub fn line(&self) -> usize {
37        self.line
38    }
39}
40
41impl fmt::Display for ParseTreeError {
42    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
43        write!(f, "`{}` at line {}", self.message, self.line + 1)
44    }
45}
46
47impl Error for ParseTreeError {
48    fn description(&self) -> &str {
49        &self.message
50    }
51}
52
53#[derive(Debug)]
54/// Serializer error.
55pub struct SerializeTreeError {
56    message: String,
57    line: usize,
58    statement: Option<String>
59}
60
61impl SerializeTreeError {
62    /// Create new serializer error.
63    /// 
64    /// # Arguments
65    /// 
66    /// * `msg` - Error message.
67    /// * `line` - File line.
68    /// * `statement` - Statement that caused the problem.
69    /// 
70    /// # Return
71    /// 
72    /// * An error model.
73    ///
74    pub fn new(message: &str, line: usize, statement: Option<String>) -> Self {
75        SerializeTreeError {
76            message: String::from(message),
77            line,
78            statement
79        }
80    }
81
82    /// Get error line.
83    /// 
84    /// # Return
85    /// 
86    /// * Line.
87    ///
88    pub fn line(&self) -> usize {
89        self.line
90    }
91
92    /// Get statement that caused the error.
93    /// 
94    /// # Return
95    /// 
96    /// * Statement.
97    ///
98    pub fn statement(&self) -> &Option<String> {
99        &self.statement
100    }
101}
102
103impl fmt::Display for SerializeTreeError {
104    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
105        if let Some(statement) = &self.statement {
106            write!(f, "`{}` at line {} with statement {}", self.message, self.line, statement)
107        }
108        else {
109            write!(f, "`{}` at line {}", self.message, self.line)
110        }
111    }
112}
113
114impl Error for SerializeTreeError {
115    fn description(&self) -> &str {
116        &self.message
117    }
118}