1use std::error::Error;
4
5#[derive(Debug)]
7pub enum ParseErrorType {
8 InvalidSyntaxRange(usize, usize),
10
11 InvalidStitchCount(usize),
13
14 UnableToReadFromReader(Box<dyn Error>),
16}
17
18impl std::fmt::Display for ParseErrorType {
19 fn fmt(&self, out: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
20 match self {
21 ParseErrorType::InvalidStitchCount(count) => write!(out, "{{ \"type\" : \"Invalid stitch count\", \"count\" : {} }}", count),
22 ParseErrorType::InvalidSyntaxRange(range_start, range_end) => write!(
23 out,
24 "{{ \"type\" : \"Invalid syntax range\", \"start\" : {}, \"end\" : {} }}",
25 range_start, range_end
26 ),
27 ParseErrorType::UnableToReadFromReader(error) => write!(out, "{{ \"type\" : \"Error reading from stream\", \"underlying\" : \"{}\" }}", error),
28 }
29 }
30}
31
32#[derive(Debug)]
36pub struct ParseError {
37 error_type: Box<ParseErrorType>,
38 line_number: usize,
39}
40
41impl ParseError {
42 pub fn new(error_type: ParseErrorType, line_number: usize) -> ParseError {
49 ParseError {
50 error_type: Box::new(error_type),
51 line_number,
52 }
53 }
54
55 pub fn error_type(&self) -> &ParseErrorType {
57 &self.error_type
58 }
59
60 pub fn line_number(&self) -> usize {
62 self.line_number
63 }
64}
65
66impl std::fmt::Display for ParseError {
67 fn fmt(&self, out: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
68 write!(out, "{{ \"error\":{}, \"line\":{} }}", self.error_type, self.line_number)
69 }
70}
71
72impl std::error::Error for ParseError {}