diff --git a/Cargo.toml b/Cargo.toml index 052dd6f..266b995 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "abortable_parser" -version = "0.2.2" +version = "0.2.3" authors = ["Jeremy Wall "] description = "A parser combinator library with an emphasis on error handling" repository = "https://github.com/zaphar/abortable_parser" diff --git a/src/iter.rs b/src/iter.rs index bed36fc..a8bbe8c 100644 --- a/src/iter.rs +++ b/src/iter.rs @@ -83,7 +83,19 @@ impl<'a, T: Debug + 'a> Clone for SliceIter<'a, T> { } } -impl<'a, T: Debug + 'a> InputIter for SliceIter<'a, T> {} +impl<'a, T: Debug + 'a> InputIter for SliceIter<'a, T> { + fn curr(&self) -> Self::Item { + if self.offset >= self.source.len() { + self.source.get(self.source.len() - 1).unwrap() + } else { + if self.offset == 0 { + self.source.get(self.offset).unwrap() + } else { + self.source.get(self.offset - 1).unwrap() + } + } + } +} impl<'a, T: Debug + 'a> Span<&'a [T]> for SliceIter<'a, T> { fn span(&self, idx: SpanRange) -> &'a [T] { @@ -189,7 +201,19 @@ impl<'a> Clone for StrIter<'a> { } } -impl<'a> InputIter for StrIter<'a> {} +impl<'a> InputIter for StrIter<'a> { + fn curr(&self) -> Self::Item { + if self.offset >= self.source.len() { + self.source.as_bytes().get(self.source.len() - 1).unwrap() + } else { + if self.offset == 0 { + self.source.as_bytes().get(self.offset).unwrap() + } else { + self.source.as_bytes().get(self.offset - 1).unwrap() + } + } + } +} impl<'a> From<&'a str> for StrIter<'a> { fn from(source: &'a str) -> Self { diff --git a/src/lib.rs b/src/lib.rs index 137a7ec..31a25db 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -145,7 +145,9 @@ pub trait Peekable { } /// A Cloneable Iterator that can report an offset as a count of processed Items. -pub trait InputIter: Iterator + Clone + Offsetable {} +pub trait InputIter: Iterator + Clone + Offsetable { + fn curr(&self) -> Self::Item; +} /// The custom error type for use in `Result::{Fail, Abort}`. /// Stores a wrapped err that must implement Display as well as an offset and diff --git a/src/test.rs b/src/test.rs index 391d431..56cc77d 100644 --- a/src/test.rs +++ b/src/test.rs @@ -30,8 +30,10 @@ fn test_slice_iter() { None => break, Some(b) => b, }; + assert_eq!(*b as char, *iter.curr() as char); out.push(b.clone()); } + assert_eq!(*iter.curr(), 'o' as u8); assert_eq!(3, out.len()); assert_eq!('f' as u8, out[0]); assert_eq!('o' as u8, out[1]);