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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
use super::token::*;
use std::fmt::Debug;
#[derive(Clone, Copy, Debug, PartialEq, Hash, Eq)]
pub struct Span(pub usize, pub usize);
pub trait Spanned {
fn span(&self) -> Span;
}
impl std::ops::Add for Span {
type Output = Self;
fn add(self, rhs: Self) -> Self {
Self(self.0.min(rhs.0), self.1.max(rhs.1))
}
}
impl std::ops::AddAssign for Span {
fn add_assign(&mut self, rhs: Self) {
*self = Self {
0: self.0.min(rhs.0),
1: self.1.max(rhs.1),
}
}
}
impl From<Span> for std::ops::Range<usize> {
fn from(span: Span) -> Self {
span.0..span.1
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct File<'input> {
pub(crate) start_percent: bool,
pub(crate) lines: Vec<(Line<'input>, Newline)>,
pub(crate) last_line: Option<Line<'input>>,
pub(crate) end_percent: bool,
pub(crate) span: Span,
}
impl<'input> File<'input> {
pub fn iter(&self) -> impl Iterator<Item = &Line<'input>> {
self.lines
.iter()
.map(|(line, _)| line)
.chain(self.last_line.iter())
}
pub fn iter_fields(&self) -> impl Iterator<Item = &Field<'input>> {
self.iter().map(|line| line.iter_fields()).flatten()
}
pub fn iter_bytes(&self) -> impl Iterator<Item = &u8> {
self.iter().map(|line| line.iter_bytes()).flatten()
}
}
impl<'input> Spanned for File<'input> {
fn span(&self) -> Span {
self.span
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Snippet<'input> {
pub(crate) lines: Vec<(Line<'input>, Newline)>,
pub(crate) last_line: Option<Line<'input>>,
pub(crate) span: Span,
}
impl<'input> Snippet<'input> {
pub fn iter(&self) -> impl Iterator<Item = &Line<'input>> {
self.lines
.iter()
.map(|(line, _)| line)
.chain(self.last_line.iter())
}
pub fn iter_fields(&self) -> impl Iterator<Item = &Field<'input>> {
self.iter().map(|line| line.iter_fields()).flatten()
}
pub fn iter_bytes(&self) -> impl Iterator<Item = &u8> {
self.iter().map(|line| line.iter_bytes()).flatten()
}
}
impl<'input> Spanned for Snippet<'input> {
fn span(&self) -> Span {
self.span
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Line<'input> {
pub(crate) line_components: Vec<LineComponent<'input>>,
pub(crate) checksum: Option<Checksum>,
pub(crate) comment: Option<Comment<'input>>,
pub(crate) span: Span,
}
impl<'input> Spanned for Line<'input> {
fn span(&self) -> Span {
self.span
}
}
impl<'input> Line<'input> {
pub fn iter_fields(&self) -> impl Iterator<Item = &Field<'input>> {
self.line_components.iter().filter_map(|c| c.field.as_ref())
}
pub fn validate_checksum(&self) -> Option<Result<(), u8>> {
if let Some(Checksum {
inner: checksum, ..
}) = self.checksum.as_ref()
{
let computed_checksum = self.compute_checksum();
if computed_checksum != *checksum {
return Some(Err(computed_checksum));
} else {
return Some(Ok(()));
}
}
None
}
pub fn iter_bytes(&self) -> impl Iterator<Item = &u8> {
self.line_components
.iter()
.map(|c| c.iter_bytes())
.flatten()
}
pub fn compute_checksum(&self) -> u8 {
let take = if let Some(checksum) = &self.checksum {
checksum.span.0
} else if let Some(comment) = &self.comment {
comment.pos
} else {
self.span.1
} - self.span.0;
self.iter_bytes().take(take).fold(0u8, |acc, b| acc ^ b)
}
}