Skip to content

Commit b24d5f6

Browse files
Ertanicalerque
authored andcommitted
refactor(fluent-syntax): Make Span a wrapper around std::ops::Range<usize>
1 parent 7f95815 commit b24d5f6

File tree

6 files changed

+39
-42
lines changed

6 files changed

+39
-42
lines changed

fluent-syntax/src/ast/mod.rs

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ mod helper;
8888

8989
#[cfg(feature = "serde")]
9090
use serde::{Deserialize, Serialize};
91+
use std::ops::Deref;
9192
#[cfg(feature = "spans")]
9293
use std::ops::Range;
9394

@@ -1427,15 +1428,15 @@ pub enum InlineExpression<S> {
14271428

14281429
#[cfg(feature = "spans")]
14291430
impl<S> InlineExpression<S> {
1430-
pub fn get_span(&self) -> Span {
1431+
pub fn get_span(&self) -> &Span {
14311432
match self {
14321433
InlineExpression::StringLiteral { span, .. }
14331434
| InlineExpression::TermReference { span, .. }
14341435
| InlineExpression::VariableReference { span, .. }
14351436
| InlineExpression::Placeable { span, .. }
14361437
| InlineExpression::NumberLiteral { span, .. }
14371438
| InlineExpression::FunctionReference { span, .. }
1438-
| InlineExpression::MessageReference { span, .. } => *span,
1439+
| InlineExpression::MessageReference { span, .. } => span,
14391440
}
14401441
}
14411442
}
@@ -1559,19 +1560,15 @@ pub enum Expression<S> {
15591560
/// assert_eq!(id.span, Span { start: 0, end: 11 }); // the span of hello-world identifier
15601561
/// ```
15611562
#[cfg(feature = "spans")]
1562-
#[derive(Debug, Clone, Copy, Default, PartialEq)]
1563+
#[derive(Debug, Clone, Default, PartialEq)]
15631564
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
1564-
pub struct Span {
1565-
pub start: usize,
1566-
pub end: usize,
1567-
}
1565+
pub struct Span(pub Range<usize>);
15681566

15691567
#[cfg(feature = "spans")]
1570-
impl Span {
1571-
pub fn new(range: Range<usize>) -> Self {
1572-
Self {
1573-
start: range.start,
1574-
end: range.end,
1575-
}
1568+
impl Deref for Span {
1569+
type Target = Range<usize>;
1570+
1571+
fn deref(&self) -> &Self::Target {
1572+
&self.0
15761573
}
1577-
}
1574+
}

fluent-syntax/src/parser/comment.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ where
5454
ast::Comment {
5555
content,
5656
#[cfg(feature = "spans")]
57-
span: ast::Span::new(start_pos..self.ptr),
57+
span: ast::Span(start_pos..self.ptr),
5858
},
5959
level,
6060
))

fluent-syntax/src/parser/core.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ where
7070
body.push(ast::Entry::Junk {
7171
content,
7272
#[cfg(feature = "spans")]
73-
span: ast::Span::new(entry_start..self.ptr),
73+
span: ast::Span(entry_start..self.ptr),
7474
});
7575
}
7676
}
@@ -84,14 +84,14 @@ where
8484
Ok(ast::Resource {
8585
body,
8686
#[cfg(feature = "spans")]
87-
span: ast::Span::new(0..self.length),
87+
span: ast::Span(0..self.length),
8888
})
8989
} else {
9090
Err((
9191
ast::Resource {
9292
body,
9393
#[cfg(feature = "spans")]
94-
span: ast::Span::new(0..self.length),
94+
span: ast::Span(0..self.length),
9595
},
9696
errors,
9797
))
@@ -139,7 +139,7 @@ where
139139
attributes,
140140
comment: None,
141141
#[cfg(feature = "spans")]
142-
span: ast::Span::new(entry_start..self.ptr),
142+
span: ast::Span(entry_start..self.ptr),
143143
})
144144
}
145145

@@ -163,7 +163,7 @@ where
163163
attributes,
164164
comment: None,
165165
#[cfg(feature = "spans")]
166-
span: ast::Span::new(entry_start..self.ptr),
166+
span: ast::Span(entry_start..self.ptr),
167167
})
168168
} else {
169169
error!(
@@ -207,7 +207,7 @@ where
207207
id,
208208
value: pattern,
209209
#[cfg(feature = "spans")]
210-
span: ast::Span::new(self.ptr - 1..self.ptr),
210+
span: ast::Span(self.ptr - 1..self.ptr),
211211
}),
212212
None => error!(ErrorKind::MissingValue, self.ptr),
213213
}
@@ -229,7 +229,7 @@ where
229229
ast::Identifier {
230230
name,
231231
#[cfg(feature = "spans")]
232-
span: ast::Span::new(start..self.ptr),
232+
span: ast::Span(start..self.ptr),
233233
}
234234
}
235235

@@ -316,7 +316,7 @@ where
316316
value,
317317
default,
318318
#[cfg(feature = "spans")]
319-
span: ast::Span::new((if default { start - 1 } else { start })..self.ptr),
319+
span: ast::Span((if default { start - 1 } else { start })..self.ptr),
320320
});
321321
self.skip_blank();
322322
} else {

fluent-syntax/src/parser/expression.rs

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ where
2323
return Ok(ast::Expression::Inline(
2424
exp,
2525
#[cfg(feature = "spans")]
26-
ast::Span::new(start_span..self.ptr),
26+
ast::Span(start_span..self.ptr),
2727
));
2828
}
2929

@@ -68,7 +68,7 @@ where
6868
selector: exp,
6969
variants,
7070
#[cfg(feature = "spans")]
71-
span: ast::Span::new(start_span..self.ptr),
71+
span: ast::Span(start_span..self.ptr),
7272
})
7373
}
7474

@@ -112,15 +112,15 @@ where
112112
Ok(ast::InlineExpression::StringLiteral {
113113
value: slice,
114114
#[cfg(feature = "spans")]
115-
span: ast::Span::new(start..self.ptr),
115+
span: ast::Span(start..self.ptr),
116116
})
117117
}
118118
Some(b) if b.is_ascii_digit() => {
119119
let num = self.get_number_literal()?;
120120
Ok(ast::InlineExpression::NumberLiteral {
121121
value: num,
122122
#[cfg(feature = "spans")]
123-
span: ast::Span::new(start..self.ptr),
123+
span: ast::Span(start..self.ptr),
124124
})
125125
}
126126
Some(b'-') if !only_literal => {
@@ -135,15 +135,15 @@ where
135135
attribute,
136136
arguments,
137137
#[cfg(feature = "spans")]
138-
span: ast::Span::new(start..self.ptr),
138+
span: ast::Span(start..self.ptr),
139139
})
140140
} else {
141141
self.ptr -= 1;
142142
let num = self.get_number_literal()?;
143143
Ok(ast::InlineExpression::NumberLiteral {
144144
value: num,
145145
#[cfg(feature = "spans")]
146-
span: ast::Span::new(start..self.ptr),
146+
span: ast::Span(start..self.ptr),
147147
})
148148
}
149149
}
@@ -153,7 +153,7 @@ where
153153
Ok(ast::InlineExpression::VariableReference {
154154
id,
155155
#[cfg(feature = "spans")]
156-
span: ast::Span::new(start..self.ptr),
156+
span: ast::Span(start..self.ptr),
157157
})
158158
}
159159
Some(b) if b.is_ascii_alphabetic() => {
@@ -169,15 +169,15 @@ where
169169
id,
170170
arguments,
171171
#[cfg(feature = "spans")]
172-
span: ast::Span::new(start..self.ptr),
172+
span: ast::Span(start..self.ptr),
173173
})
174174
} else {
175175
let attribute = self.get_attribute_accessor()?;
176176
Ok(ast::InlineExpression::MessageReference {
177177
id,
178178
attribute,
179179
#[cfg(feature = "spans")]
180-
span: ast::Span::new(start..self.ptr),
180+
span: ast::Span(start..self.ptr),
181181
})
182182
}
183183
}
@@ -187,7 +187,7 @@ where
187187
Ok(ast::InlineExpression::Placeable {
188188
expression: Box::new(exp),
189189
#[cfg(feature = "spans")]
190-
span: ast::Span::new(start..self.ptr),
190+
span: ast::Span(start..self.ptr),
191191
})
192192
}
193193
_ if only_literal => error!(ErrorKind::ExpectedLiteral, self.ptr),
@@ -239,11 +239,11 @@ where
239239
name: ast::Identifier {
240240
name: id.name.clone(),
241241
#[cfg(feature = "spans")]
242-
span: id.span,
242+
span: id.span.clone(),
243243
},
244244
value: val,
245245
#[cfg(feature = "spans")]
246-
span: ast::Span::new(id.span.start..self.ptr),
246+
span: ast::Span(id.span.start..self.ptr),
247247
});
248248
} else {
249249
if !argument_names.is_empty() {
@@ -269,7 +269,7 @@ where
269269
positional,
270270
named,
271271
#[cfg(feature = "spans")]
272-
span: ast::Span::new(start..self.ptr),
272+
span: ast::Span(start..self.ptr),
273273
}))
274274
}
275275
}

fluent-syntax/src/parser/pattern.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ where
147147
PatternElementPlaceholders::Placeable(expression, range) => {
148148
ast::PatternElement::Placeable {
149149
expression,
150-
span: ast::Span::new(range),
150+
span: ast::Span(range),
151151
}
152152
}
153153
#[cfg(not(feature = "spans"))]
@@ -170,15 +170,15 @@ where
170170
ast::PatternElement::TextElement {
171171
value,
172172
#[cfg(feature = "spans")]
173-
span: ast::Span::new(start..end),
173+
span: ast::Span(start..end),
174174
}
175175
}
176176
})
177177
.collect();
178178
return Ok(Some(ast::Pattern {
179179
elements,
180180
#[cfg(feature = "spans")]
181-
span: ast::Span::new(start_pos..self.ptr),
181+
span: ast::Span(start_pos..self.ptr),
182182
}));
183183
}
184184

fluent-syntax/src/parser/runtime.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ where
3737
body.push(ast::Entry::Junk {
3838
content,
3939
#[cfg(feature = "spans")]
40-
span: ast::Span::new(entry_start..self.ptr),
40+
span: ast::Span(entry_start..self.ptr),
4141
});
4242
}
4343
}
@@ -48,14 +48,14 @@ where
4848
Ok(ast::Resource {
4949
body,
5050
#[cfg(feature = "spans")]
51-
span: ast::Span::new(0..self.length),
51+
span: ast::Span(0..self.length),
5252
})
5353
} else {
5454
Err((
5555
ast::Resource {
5656
body,
5757
#[cfg(feature = "spans")]
58-
span: ast::Span::new(0..self.length),
58+
span: ast::Span(0..self.length),
5959
},
6060
errors,
6161
))

0 commit comments

Comments
 (0)