From 9ce749be6738c6ae8f9ebda74f89f922dab6c69a Mon Sep 17 00:00:00 2001 From: Khushboo Desai Date: Tue, 4 Feb 2025 09:51:19 -0800 Subject: [PATCH] Adds location to `LazyValue` and unit tests --- src/lazy/value.rs | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/src/lazy/value.rs b/src/lazy/value.rs index ebd9574f..cf1c0e06 100644 --- a/src/lazy/value.rs +++ b/src/lazy/value.rs @@ -8,6 +8,7 @@ use crate::{ try_or_some_err, Annotations, Element, ExpandedValueSource, IntoAnnotatedElement, IonError, IonResult, IonType, SymbolRef, SymbolTable, Value, }; +use std::ops::Range; /// A value in a binary Ion stream whose header has been parsed but whose body (i.e. its data) has /// not. A `LazyValue` is immutable; its data can be read any number of times. @@ -260,6 +261,14 @@ impl<'top, D: Decoder> LazyValue<'top, D> { pub fn read(&self) -> IonResult> { self.expanded_value.read_resolved() } + + pub fn location(&self) -> Option<(usize, usize)> { + self.expanded_value.location() + } + + pub fn range(&self) -> Option> { + self.expanded_value.range() + } } impl<'top, D: Decoder> TryFrom> for Element { @@ -550,4 +559,28 @@ mod tests { assert!(matches!(result, Err(crate::IonError::Incomplete(_)))); Ok(()) } + + #[rstest] + #[case::newlines("{foo: 1, bar: 2}\r\n\n\"hello\"", (4,1))] + #[case::tabs("{foo: 1, bar: 2}\n\t\t\t\"hello\"", (2,4))] + #[case::mix_tabs_and_newlines("{foo: 1, bar: 2}\n\t\n\"hello\"", (3,1))] + fn location_test_for_second_tlv( + #[case] ion_text: &str, + #[case] expected_location: (usize, usize), + ) -> IonResult<()> { + let mut reader = Reader::new(v1_0::Text, ion_text)?; + let result1 = reader.expect_next(); + assert!(result1.is_ok()); + if let Ok(lazy_value1) = result1 { + let _val = lazy_value1.read(); + assert_eq!(lazy_value1.location().unwrap(), (1, 1)); + } + let result2 = reader.expect_next(); + assert!(result2.is_ok()); + if let Ok(lazy_value2) = result2 { + let _val = lazy_value2.read(); + assert_eq!(lazy_value2.location().unwrap(), expected_location); + } + Ok(()) + } }