mirror of
https://github.com/zaphar/abortable_parser.git
synced 2025-07-21 20:29:49 -04:00
FEATURE: Add a curr method to the InputIter trait.
This commit is contained in:
parent
4c210c3c08
commit
9b1e5a74f3
@ -1,6 +1,6 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "abortable_parser"
|
name = "abortable_parser"
|
||||||
version = "0.2.2"
|
version = "0.2.3"
|
||||||
authors = ["Jeremy Wall <jeremy@marzhillstudios.com>"]
|
authors = ["Jeremy Wall <jeremy@marzhillstudios.com>"]
|
||||||
description = "A parser combinator library with an emphasis on error handling"
|
description = "A parser combinator library with an emphasis on error handling"
|
||||||
repository = "https://github.com/zaphar/abortable_parser"
|
repository = "https://github.com/zaphar/abortable_parser"
|
||||||
|
28
src/iter.rs
28
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> {
|
impl<'a, T: Debug + 'a> Span<&'a [T]> for SliceIter<'a, T> {
|
||||||
fn span(&self, idx: SpanRange) -> &'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> {
|
impl<'a> From<&'a str> for StrIter<'a> {
|
||||||
fn from(source: &'a str) -> Self {
|
fn from(source: &'a str) -> Self {
|
||||||
|
@ -145,7 +145,9 @@ pub trait Peekable<O> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// A Cloneable Iterator that can report an offset as a count of processed Items.
|
/// 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}`.
|
/// The custom error type for use in `Result::{Fail, Abort}`.
|
||||||
/// Stores a wrapped err that must implement Display as well as an offset and
|
/// Stores a wrapped err that must implement Display as well as an offset and
|
||||||
|
@ -30,8 +30,10 @@ fn test_slice_iter() {
|
|||||||
None => break,
|
None => break,
|
||||||
Some(b) => b,
|
Some(b) => b,
|
||||||
};
|
};
|
||||||
|
assert_eq!(*b as char, *iter.curr() as char);
|
||||||
out.push(b.clone());
|
out.push(b.clone());
|
||||||
}
|
}
|
||||||
|
assert_eq!(*iter.curr(), 'o' as u8);
|
||||||
assert_eq!(3, out.len());
|
assert_eq!(3, out.len());
|
||||||
assert_eq!('f' as u8, out[0]);
|
assert_eq!('f' as u8, out[0]);
|
||||||
assert_eq!('o' as u8, out[1]);
|
assert_eq!('o' as u8, out[1]);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user