Skip to content

Commit

Permalink
Adds location to LazyValue and unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
desaikd committed Feb 4, 2025
1 parent 536bbd0 commit 9ce749b
Showing 1 changed file with 33 additions and 0 deletions.
33 changes: 33 additions & 0 deletions src/lazy/value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -260,6 +261,14 @@ impl<'top, D: Decoder> LazyValue<'top, D> {
pub fn read(&self) -> IonResult<ValueRef<'top, D>> {
self.expanded_value.read_resolved()
}

pub fn location(&self) -> Option<(usize, usize)> {
self.expanded_value.location()
}

pub fn range(&self) -> Option<Range<usize>> {
self.expanded_value.range()
}
}

impl<'top, D: Decoder> TryFrom<LazyValue<'top, D>> for Element {
Expand Down Expand Up @@ -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(())
}
}

0 comments on commit 9ce749b

Please sign in to comment.