1#[cfg(feature = "std")]
2extern crate std;
3
4extern crate alloc;
5
6#[cfg(feature = "std")]
7use std::{io, num::NonZeroU64};
8
9use core::{error, fmt};
10
11use alloc::string::String;
12
13#[derive(Debug, Clone)]
14#[cfg(feature = "std")]
15pub struct Line {
16 pub message: String,
17 pub line_number: Option<NonZeroU64>,
18}
19
20#[cfg(feature = "std")]
21impl fmt::Display for Line {
22 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
23 match self.line_number {
24 Some(ln) => write!(f, "Line {}: {}", ln, self.message),
25 None => write!(f, "{}", self.message),
26 }
27 }
28}
29
30#[cfg(feature = "std")]
31impl error::Error for Line {}
32
33#[derive(Debug)]
34#[cfg(feature = "std")]
35pub enum TextParse {
36 Io(io::Error),
37 Lexer(Line),
38 Parser(Line),
39}
40
41#[cfg(feature = "std")]
42impl TextParse {
43 pub fn from_lexer(message: String, line_number: NonZeroU64) -> TextParse {
44 TextParse::Lexer(Line {
45 message,
46 line_number: Some(line_number),
47 })
48 }
49
50 pub fn from_parser(message: String, line_number: NonZeroU64) -> TextParse {
51 TextParse::Parser(Line {
52 message,
53 line_number: Some(line_number),
54 })
55 }
56
57 pub fn eof() -> TextParse {
58 TextParse::Parser(Line {
59 message: String::from("Unexpected end-of-file"),
60 line_number: None,
61 })
62 }
63}
64
65#[cfg(feature = "std")]
66impl From<io::Error> for TextParse {
67 fn from(err: io::Error) -> Self {
68 TextParse::Io(err)
69 }
70}
71
72#[cfg(feature = "std")]
73impl fmt::Display for TextParse {
74 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
75 match self {
76 Self::Io(msg) => write!(f, "{}", msg),
77 Self::Lexer(err) => write!(f, "{}", err),
78 Self::Parser(err) => write!(f, "{}", err),
79 }
80 }
81}
82
83#[cfg(feature = "std")]
84impl std::error::Error for TextParse {}
85
86#[derive(Debug)]
87#[cfg(feature = "std")]
88pub enum Write {
89 Validation(String),
90 Io(std::io::Error),
91}
92
93#[cfg(feature = "std")]
94impl From<io::Error> for Write {
95 fn from(err: io::Error) -> Self {
96 Write::Io(err)
97 }
98}
99
100#[cfg(feature = "std")]
101impl From<Validation> for Write {
102 fn from(val_err: Validation) -> Self {
103 Self::Validation(val_err.0)
104 }
105}
106
107#[cfg(feature = "std")]
108impl fmt::Display for Write {
109 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
110 match self {
111 Self::Validation(msg) => write!(f, "{}", msg),
112 Self::Io(err) => write!(f, "{}", err),
113 }
114 }
115}
116
117#[cfg(feature = "std")]
118impl std::error::Error for Write {}
119
120#[derive(Debug, PartialEq, Eq)]
121pub struct Validation(pub String);
122
123impl From<String> for Validation {
124 fn from(s: String) -> Self {
125 Self(s)
126 }
127}
128
129impl From<&str> for Validation {
130 fn from(s: &str) -> Self {
131 Self(String::from(s))
132 }
133}
134
135impl fmt::Display for Validation {
136 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
137 write!(f, "{}", self.0)
138 }
139}
140
141impl error::Error for Validation {}
142
143pub type ValidationResult = Result<(), Validation>;
145
146#[cfg(feature = "std")]
147pub type TextParseResult<T> = Result<T, TextParse>;
148
149#[cfg(feature = "std")]
150pub type WriteResult = Result<(), Write>;