1extern crate std;
2
3use std::{error, fmt, io, num::NonZeroU64, string::String};
4
5#[derive(Debug, Clone)]
6pub struct Line {
7 pub message: String,
8 pub line_number: Option<NonZeroU64>,
9}
10
11impl fmt::Display for Line {
12 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
13 match self.line_number {
14 Some(ln) => write!(f, "Line {}: {}", ln, self.message),
15 None => write!(f, "{}", self.message),
16 }
17 }
18}
19
20impl error::Error for Line {}
21
22#[derive(Debug)]
23pub enum TextParse {
24 Io(io::Error),
25 Lexer(Line),
26 Parser(Line),
27}
28
29impl TextParse {
30 pub fn from_lexer(message: String, line_number: NonZeroU64) -> TextParse {
31 TextParse::Lexer(Line {
32 message,
33 line_number: Some(line_number),
34 })
35 }
36
37 pub fn from_parser(message: String, line_number: NonZeroU64) -> TextParse {
38 TextParse::Parser(Line {
39 message,
40 line_number: Some(line_number),
41 })
42 }
43
44 pub fn eof() -> TextParse {
45 TextParse::Parser(Line {
46 message: String::from("Unexpected end-of-file"),
47 line_number: None,
48 })
49 }
50}
51
52impl From<io::Error> for TextParse {
53 fn from(err: io::Error) -> Self {
54 TextParse::Io(err)
55 }
56}
57
58impl fmt::Display for TextParse {
59 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
60 match self {
61 Self::Io(msg) => write!(f, "{}", msg),
62 Self::Lexer(err) => write!(f, "{}", err),
63 Self::Parser(err) => write!(f, "{}", err),
64 }
65 }
66}
67
68impl std::error::Error for TextParse {}
69
70#[derive(Debug)]
71pub enum Write {
72 Validation(String),
73 Io(std::io::Error),
74}
75
76impl From<io::Error> for Write {
77 fn from(err: io::Error) -> Self {
78 Write::Io(err)
79 }
80}
81
82impl fmt::Display for Write {
83 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
84 match self {
85 Self::Validation(msg) => write!(f, "{}", msg),
86 Self::Io(err) => write!(f, "{}", err),
87 }
88 }
89}
90
91impl std::error::Error for Write {}