Skip to content

Commit

Permalink
feat: add simple Position/Span From implementations for LineColLocati…
Browse files Browse the repository at this point in the history
…on (#860)

* feat: add simple From Pos/Span From impls for LCL

* feat: add basic tests for From impls
  • Loading branch information
HoloTheDrunk authored May 26, 2023
1 parent 9176af2 commit a4c93a7
Showing 1 changed file with 35 additions and 0 deletions.
35 changes: 35 additions & 0 deletions pest/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,19 @@ pub enum LineColLocation {
Span((usize, usize), (usize, usize)),
}

impl From<Position<'_>> for LineColLocation {
fn from(value: Position<'_>) -> Self {
Self::Pos(value.line_col())
}
}

impl From<Span<'_>> for LineColLocation {
fn from(value: Span<'_>) -> Self {
let (start, end) = value.split();
Self::Span(start.line_col(), end.line_col())
}
}

impl<R: RuleType> Error<R> {
/// Creates `Error` from `ErrorVariant` and `Position`.
///
Expand Down Expand Up @@ -892,4 +905,26 @@ mod tests {
.join("\n")
);
}

#[test]
fn pos_to_lcl_conversion() {
let input = "input";

let pos = Position::new(input, 2).unwrap();

assert_eq!(LineColLocation::Pos(pos.line_col()), pos.into());
}

#[test]
fn span_to_lcl_conversion() {
let input = "input";

let span = Span::new(input, 2, 4).unwrap();
let (start, end) = span.split();

assert_eq!(
LineColLocation::Span(start.line_col(), end.line_col()),
span.into()
);
}
}

0 comments on commit a4c93a7

Please sign in to comment.