From b7a5dd48ea020fc5fd76cc7f18f01f419233ac11 Mon Sep 17 00:00:00 2001 From: Tom Alexander Date: Tue, 22 Aug 2023 23:32:27 -0400 Subject: [PATCH] Impl missing traits. --- src/parser/org_source.rs | 43 ++++++++++++++++++++++++++++++---------- 1 file changed, 32 insertions(+), 11 deletions(-) diff --git a/src/parser/org_source.rs b/src/parser/org_source.rs index 3f43c657..b765366c 100644 --- a/src/parser/org_source.rs +++ b/src/parser/org_source.rs @@ -12,7 +12,7 @@ use nom::Slice; pub struct OrgSource<'s> { full_source: &'s str, start: usize, - end: usize, //exclusive + end: usize, // exclusive preceding_line_break: Option, } @@ -113,7 +113,7 @@ impl<'s> std::fmt::Display for OrgSource<'s> { impl<'s> InputLength for OrgSource<'s> { fn input_len(&self) -> usize { - todo!() + self.end - self.start } } @@ -125,28 +125,28 @@ impl<'s> InputIter for OrgSource<'s> { type IterElem = <&'s str as InputIter>::IterElem; fn iter_indices(&self) -> Self::Iter { - todo!() + Into::<&str>::into(self).char_indices() } fn iter_elements(&self) -> Self::IterElem { - todo!() + Into::<&str>::into(self).iter_elements() } fn position

(&self, predicate: P) -> Option where P: Fn(Self::Item) -> bool, { - todo!() + Into::<&str>::into(self).position(predicate) } fn slice_index(&self, count: usize) -> Result { - todo!() + Into::<&str>::into(self).slice_index(count) } } impl<'s> Offset for OrgSource<'s> { fn offset(&self, second: &Self) -> usize { - todo!() + second.start - self.start } } @@ -160,7 +160,10 @@ impl<'s> InputTakeAtPosition for OrgSource<'s> { where P: Fn(Self::Item) -> bool, { - todo!() + match Into::<&str>::into(self).position(predicate) { + Some(idx) => Ok(self.take_split(idx)), + None => Err(nom::Err::Incomplete(nom::Needed::new(1))), + } } fn split_at_position1>( @@ -171,7 +174,11 @@ impl<'s> InputTakeAtPosition for OrgSource<'s> { where P: Fn(Self::Item) -> bool, { - todo!() + match Into::<&str>::into(self).position(predicate) { + Some(0) => Err(nom::Err::Error(E::from_error_kind(self.clone(), e))), + Some(idx) => Ok(self.take_split(idx)), + None => Err(nom::Err::Incomplete(nom::Needed::new(1))), + } } fn split_at_position_complete>( @@ -181,7 +188,10 @@ impl<'s> InputTakeAtPosition for OrgSource<'s> { where P: Fn(Self::Item) -> bool, { - todo!() + match self.split_at_position(predicate) { + Err(nom::Err::Incomplete(_)) => Ok(self.take_split(self.input_len())), + res => res, + } } fn split_at_position1_complete>( @@ -192,7 +202,18 @@ impl<'s> InputTakeAtPosition for OrgSource<'s> { where P: Fn(Self::Item) -> bool, { - todo!() + let window = Into::<&str>::into(self); + match window.position(predicate) { + Some(0) => Err(nom::Err::Error(E::from_error_kind(self.clone(), e))), + Some(n) => Ok(self.take_split(n)), + None => { + if window.input_len() == 0 { + Err(nom::Err::Error(E::from_error_kind(self.clone(), e))) + } else { + Ok(self.take_split(self.input_len())) + } + } + } } }