FEATURE: Add a curr method to the InputIter trait.

This commit is contained in:
Jeremy Wall 2019-02-01 18:47:32 -06:00
parent 4c210c3c08
commit 9b1e5a74f3
4 changed files with 32 additions and 4 deletions

View File

@ -1,6 +1,6 @@
[package]
name = "abortable_parser"
version = "0.2.2"
version = "0.2.3"
authors = ["Jeremy Wall <jeremy@marzhillstudios.com>"]
description = "A parser combinator library with an emphasis on error handling"
repository = "https://github.com/zaphar/abortable_parser"

View File

@ -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 {

View File

@ -145,7 +145,9 @@ pub trait Peekable<O> {
}
/// 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

View File

@ -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]);