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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
use std::borrow::Borrow;
use std::borrow::BorrowMut;
use std::fmt::Display;
use std::fmt::Formatter;
use std::fmt::Result as FmtResult;
use std::fmt::Write;
use std::ops::Deref;
use std::ops::DerefMut;
use pest::iterators::Pair;
use crate::ast::*;
use crate::error::Result;
use crate::parser::FromPair;
use crate::parser::Rule;
#[derive(Clone, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
pub struct Line<T> {
inner: T,
qualifiers: Option<QualifierList>,
comment: Option<Comment>,
}
impl<T> Line<T> {
pub fn and_comment<C>(self, comment: C) -> Self
where
C: Into<Option<Comment>>
{
Self {
inner: self.inner,
qualifiers: self.qualifiers,
comment: comment.into(),
}
}
pub fn and_qualifiers<Q>(self, qualifiers: Q) -> Self
where
Q: Into<Option<QualifierList>>
{
Self {
inner: self.inner,
qualifiers: qualifiers.into(),
comment: self.comment,
}
}
pub fn qualifiers(&self) -> Option<&QualifierList> {
self.qualifiers.as_ref()
}
pub fn qualifiers_mut(&mut self) -> Option<&mut QualifierList> {
self.qualifiers.as_mut()
}
pub fn comment(&self) -> Option<&Comment> {
self.comment.as_ref()
}
pub fn comment_mut(&mut self) -> Option<&mut Comment> {
self.comment.as_mut()
}
pub fn as_inner(&self) -> &T {
&self.inner
}
pub fn into_inner(self) -> T {
self.inner
}
}
impl<T> AsRef<T> for Line<T> {
fn as_ref(&self) -> &T {
&self.inner
}
}
impl<T> AsMut<T> for Line<T> {
fn as_mut(&mut self) -> &mut T {
&mut self.inner
}
}
impl<T> Borrow<T> for Line<T> {
fn borrow(&self) -> &T {
&self.inner
}
}
impl<T> BorrowMut<T> for Line<T> {
fn borrow_mut(&mut self) -> &mut T {
&mut self.inner
}
}
impl<T> Deref for Line<T> {
type Target = T;
fn deref(&self) -> &T {
&self.inner
}
}
impl<T> DerefMut for Line<T> {
fn deref_mut(&mut self) -> &mut T {
&mut self.inner
}
}
impl<T> Display for Line<T>
where
T: Display,
{
fn fmt(&self, f: &mut Formatter) -> FmtResult {
self.inner.fmt(f)?;
if let Some(ref qualifiers) = self.qualifiers {
f.write_char(' ').and(qualifiers.fmt(f))?;
}
if let Some(ref comment) = self.comment {
f.write_char(' ').and(comment.fmt(f))?;
}
f.write_char('\n')
}
}
impl<T> From<T> for Line<T> {
fn from(inner: T) -> Self {
Line {
inner,
qualifiers: None,
comment: None,
}
}
}
pub type Eol = Line<()>;
impl<'i> FromPair<'i> for Eol {
const RULE: Rule = Rule::EOL;
unsafe fn from_pair_unchecked(pair: Pair<'i, Rule>) -> Result<Self> {
let mut inner = pair.into_inner();
let opt1 = inner.next();
let opt2 = inner.next();
match (opt1, opt2) {
(Some(pair1), Some(pair2)) => {
let comment = Comment::from_pair_unchecked(pair2)?;
let qualifiers = QualifierList::from_pair_unchecked(pair1)?;
Ok(Eol::with_qualifiers(qualifiers).and_comment(comment))
}
(Some(pair1), None) => match pair1.as_rule() {
Rule::QualifierList => {
QualifierList::from_pair_unchecked(pair1).map(Eol::with_qualifiers)
}
Rule::HiddenComment => {
Comment::from_pair_unchecked(pair1).map(Eol::with_comment)
}
_ => unreachable!(),
},
(None, _) => Ok(Eol::new()),
}
}
}
impl Default for Eol {
fn default() -> Self {
Line {
inner: (),
qualifiers: None,
comment: None,
}
}
}
impl Eol {
pub fn new() -> Self {
Default::default()
}
pub fn with_comment(comment: Comment) -> Self {
Self::new().and_comment(comment)
}
pub fn with_qualifiers(qualifiers: QualifierList) -> Self {
Self::new().and_qualifiers(qualifiers)
}
pub fn and_inner<T>(self, inner: T) -> Line<T> {
Line {
inner,
qualifiers: self.qualifiers,
comment: self.comment,
}
}
}
impl From<Comment> for Eol {
fn from(comment: Comment) -> Self {
Self::with_comment(comment)
}
}
impl From<QualifierList> for Eol {
fn from(qualifiers: QualifierList) -> Self {
Self::with_qualifiers(qualifiers)
}
}