1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
use super::Context;
use super::ErrorLevel;
use crate::StrictnessLevel;
use std::cmp::Ordering;
use std::error;
use std::fmt;
#[derive(PartialEq, Clone, Eq)]
pub struct PDBError {
level: ErrorLevel,
short_description: String,
long_description: String,
context: Context,
}
impl PDBError {
pub fn new(
level: ErrorLevel,
short_desc: &str,
long_descr: &str,
context: Context,
) -> PDBError {
PDBError {
level,
short_description: short_desc.to_owned(),
long_description: long_descr.to_owned(),
context,
}
}
pub fn level(&self) -> ErrorLevel {
self.level
}
pub fn fails(&self, level: StrictnessLevel) -> bool {
self.level.fails(level)
}
pub fn short_description(&self) -> &str {
&self.short_description
}
}
impl fmt::Debug for PDBError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(
f,
"{}: {}\n{}\n{}\n",
self.level, self.short_description, self.context, self.long_description
)
}
}
impl fmt::Display for PDBError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{:?}", self)
}
}
impl error::Error for PDBError {}
impl PartialOrd for PDBError {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
Some(self.cmp(other))
}
}
impl Ord for PDBError {
fn cmp(&self, other: &Self) -> Ordering {
self.level.cmp(&other.level)
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::Position;
#[test]
fn create_empty_error() {
let a = PDBError::new(ErrorLevel::GeneralWarning, "test", "test", Context::none());
println!("{}", a);
assert_eq!(format!("{}", a), "GeneralWarning: test\n\ntest\n");
assert_eq!(a.level(), ErrorLevel::GeneralWarning);
assert!(!a.fails(StrictnessLevel::Loose));
}
#[test]
fn create_full_line_error() {
let a = PDBError::new(
ErrorLevel::StrictWarning,
"test",
"test",
Context::full_line(1, "testing line"),
);
println!("{}", a);
assert_eq!(
format!("{}", a),
"StrictWarning: test\n\n |\n1 | testing line\n |\n\ntest\n"
);
assert_eq!(a.level(), ErrorLevel::StrictWarning);
assert!(a.fails(StrictnessLevel::Strict));
}
#[test]
fn create_range_error() {
let pos1 = Position {
text: "hello world\nthis is a multiline\npiece of teXt",
line: 1,
column: 0,
};
let pos2 = Position {
text: "",
line: 4,
column: 13,
};
let a = PDBError::new(
ErrorLevel::LooseWarning,
"test",
"test error",
Context::range(&pos1, &pos2),
);
println!("{}", a);
assert_eq!(format!("{}", a), "LooseWarning: test\n\n |\n1 | hello world\n2 | this is a multiline\n3 | piece of teXt\n |\ntest error\n");
assert_eq!(a.level(), ErrorLevel::LooseWarning);
assert!(a.fails(StrictnessLevel::Strict));
assert_eq!(pos2.text, "");
assert_eq!(pos2.line, 4);
assert_eq!(pos2.column, 13);
}
#[test]
fn ordering_and_equality() {
let a = PDBError::new(ErrorLevel::GeneralWarning, "test", "test", Context::none());
let b = PDBError::new(ErrorLevel::LooseWarning, "test", "test", Context::none());
let c = PDBError::new(ErrorLevel::LooseWarning, "test", "test", Context::none());
let d = PDBError::new(ErrorLevel::BreakingError, "test", "test", Context::none());
assert_ne!(a, b);
assert_eq!(b, c);
assert_ne!(c, d);
assert!(a > b);
assert!(c > d);
assert!(c < a);
}
}