diff --git a/pest/src/error.rs b/pest/src/error.rs index eb3e0870..df3f5448 100644 --- a/pest/src/error.rs +++ b/pest/src/error.rs @@ -74,6 +74,19 @@ pub enum LineColLocation { Span((usize, usize), (usize, usize)), } +impl From> for LineColLocation { + fn from(value: Position<'_>) -> Self { + Self::Pos(value.line_col()) + } +} + +impl From> for LineColLocation { + fn from(value: Span<'_>) -> Self { + let (start, end) = value.split(); + Self::Span(start.line_col(), end.line_col()) + } +} + impl Error { /// Creates `Error` from `ErrorVariant` and `Position`. /// @@ -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() + ); + } }