Skip to content

Commit 3a23ebe

Browse files
authored
Fix clippy::result_large_err (#1143)
Closes #1142
1 parent 28427c6 commit 3a23ebe

File tree

1 file changed

+31
-20
lines changed

1 file changed

+31
-20
lines changed

pest/src/error.rs

Lines changed: 31 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,12 @@ pub struct Error<R> {
3636
pub location: InputLocation,
3737
/// Line/column within the input string
3838
pub line_col: LineColLocation,
39+
inner: Box<ErrorInner<R>>,
40+
}
41+
42+
/// Private information for parse errors.
43+
#[derive(Clone, Debug, Eq, Hash, PartialEq)]
44+
struct ErrorInner<R> {
3945
path: Option<String>,
4046
line: String,
4147
continued_line: Option<String>,
@@ -200,11 +206,13 @@ impl<R: RuleType> Error<R> {
200206
Error {
201207
variant,
202208
location: InputLocation::Pos(pos.pos()),
203-
path: None,
204-
line,
205-
continued_line: None,
206209
line_col: LineColLocation::Pos(pos.line_col()),
207-
parse_attempts: None,
210+
inner: Box::new(ErrorInner {
211+
path: None,
212+
line,
213+
continued_line: None,
214+
parse_attempts: None,
215+
}),
208216
}
209217
}
210218

@@ -216,7 +224,7 @@ impl<R: RuleType> Error<R> {
216224
parse_attempts: ParseAttempts<R>,
217225
) -> Error<R> {
218226
let mut error = Self::new_from_pos(variant, pos);
219-
error.parse_attempts = Some(parse_attempts);
227+
error.inner.parse_attempts = Some(parse_attempts);
220228
error
221229
}
222230

@@ -279,11 +287,13 @@ impl<R: RuleType> Error<R> {
279287
Error {
280288
variant,
281289
location: InputLocation::Span((span.start(), end.pos())),
282-
path: None,
283-
line: start_line,
284-
continued_line,
285290
line_col: LineColLocation::Span(span.start_pos().line_col(), end_line_col),
286-
parse_attempts: None,
291+
inner: Box::new(ErrorInner {
292+
path: None,
293+
line: start_line,
294+
continued_line,
295+
parse_attempts: None,
296+
}),
287297
}
288298
}
289299

@@ -312,7 +322,7 @@ impl<R: RuleType> Error<R> {
312322
/// ).with_path("file.rs");
313323
/// ```
314324
pub fn with_path(mut self, path: &str) -> Error<R> {
315-
self.path = Some(path.to_owned());
325+
self.inner.path = Some(path.to_owned());
316326

317327
self
318328
}
@@ -343,12 +353,12 @@ impl<R: RuleType> Error<R> {
343353
/// assert_eq!(Some("file.rs"), error.path());
344354
/// ```
345355
pub fn path(&self) -> Option<&str> {
346-
self.path.as_deref()
356+
self.inner.path.as_deref()
347357
}
348358

349359
/// Returns the line that the error is on.
350360
pub fn line(&self) -> &str {
351-
self.line.as_str()
361+
self.inner.line.as_str()
352362
}
353363

354364
/// Renames all `Rule`s if this is a [`ParsingError`]. It does nothing when called on a
@@ -409,7 +419,7 @@ impl<R: RuleType> Error<R> {
409419
/// Get detailed information about errored rules sequence.
410420
/// Returns `Some(results)` only for `ParsingError`.
411421
pub fn parse_attempts(&self) -> Option<ParseAttempts<R>> {
412-
self.parse_attempts.clone()
422+
self.inner.parse_attempts.clone()
413423
}
414424

415425
/// Get error message based on parsing attempts.
@@ -420,7 +430,7 @@ impl<R: RuleType> Error<R> {
420430
rule_to_message: &RuleToMessageFn<R>,
421431
is_whitespace: &IsWhitespaceFn,
422432
) -> Option<Error<R>> {
423-
let attempts = if let Some(ref parse_attempts) = self.parse_attempts {
433+
let attempts = if let Some(ref parse_attempts) = self.inner.parse_attempts {
424434
parse_attempts.clone()
425435
} else {
426436
return None;
@@ -538,7 +548,7 @@ impl<R: RuleType> Error<R> {
538548
_ => None,
539549
};
540550
let offset = start - 1;
541-
let line_chars = self.line.chars();
551+
let line_chars = self.inner.line.chars();
542552

543553
for c in line_chars.take(offset) {
544554
match c {
@@ -605,12 +615,13 @@ impl<R: RuleType> Error<R> {
605615
pub(crate) fn format(&self) -> String {
606616
let spacing = self.spacing();
607617
let path = self
618+
.inner
608619
.path
609620
.as_ref()
610621
.map(|path| format!("{}:", path))
611622
.unwrap_or_default();
612623

613-
let pair = (self.line_col.clone(), &self.continued_line);
624+
let pair = (self.line_col.clone(), &self.inner.continued_line);
614625
if let (LineColLocation::Span(_, end), Some(ref continued_line)) = pair {
615626
let has_line_gap = end.0 - self.start().0 > 1;
616627
if has_line_gap {
@@ -629,7 +640,7 @@ impl<R: RuleType> Error<R> {
629640
ls = self.start().0,
630641
le = end.0,
631642
c = self.start().1,
632-
line = self.line,
643+
line = self.inner.line,
633644
continued_line = continued_line,
634645
underline = self.underline(),
635646
message = self.message()
@@ -649,7 +660,7 @@ impl<R: RuleType> Error<R> {
649660
ls = self.start().0,
650661
le = end.0,
651662
c = self.start().1,
652-
line = self.line,
663+
line = self.inner.line,
653664
continued_line = continued_line,
654665
underline = self.underline(),
655666
message = self.message()
@@ -667,7 +678,7 @@ impl<R: RuleType> Error<R> {
667678
p = path,
668679
l = self.start().0,
669680
c = self.start().1,
670-
line = self.line,
681+
line = self.inner.line,
671682
underline = self.underline(),
672683
message = self.message()
673684
)
@@ -753,7 +764,7 @@ mod miette_adapter {
753764

754765
impl<R: RuleType> Diagnostic for MietteAdapter<R> {
755766
fn source_code(&self) -> Option<&dyn SourceCode> {
756-
Some(&self.0.line)
767+
Some(&self.0.inner.line)
757768
}
758769

759770
fn labels(&self) -> Option<Box<dyn Iterator<Item = LabeledSpan>>> {

0 commit comments

Comments
 (0)