pub struct OneOrMore<T> { /* private fields */ }Expand description
Collection struct which stores one or more items of type T.
This collection is used within the manger crate
to express consuming one of more of an item from a string.
This would be equivalent to the + operator in
EBNF syntax or
RegEx.
§Note
While OneOrMore is not iterable, the
into_iter, into_vec,
ref_vec and mut_vec
can be used to iterate/create iterators over the items contained within the structs
and do further data manipulation.
§Examples
use manger::{Consumable, consume_struct};
use manger::common::OneOrMore;
let source = "(2)(3)(7)";
// EncasedInteger will be consuming strings like "(123)" and "(42)"
struct EncasedInteger { value: u32 };
consume_struct!(
EncasedInteger => [
> '(',
value: u32,
> ')';
]
);
let (encased_integers, _) = <OneOrMore<EncasedInteger>>::consume_from(source)?;
let product: u32 = encased_integers
.into_iter()
.map(|encased_integer| encased_integer.value)
.product();
assert_eq!(product, 42);Implementations§
Source§impl<T> OneOrMore<T>
impl<T> OneOrMore<T>
Sourcepub fn head(&self) -> &T
pub fn head(&self) -> &T
Getter for the first item of a OneOrMore<T>.
This will return a reference to the item that is first consumed and therefore always contains an item.
§Examples
use manger::Consumable;
use manger::common::OneOrMore;
let (items, _) = <OneOrMore<char>>::consume_from("aBcdEFg")?;
assert_eq!(*items.head(), 'a');Sourcepub fn tail(&self) -> &Vec<T>
pub fn tail(&self) -> &Vec<T>
Getter for the non-first items of a OneOrMore<T>.
This will return references to the items that is were consumed
after the first item and will be in order of they position within the
source string. The returned vector possibly has _NO items.
§Examples
use manger::Consumable;
use manger::common::OneOrMore;
let (items, _) = <OneOrMore<char>>::consume_from("aBcdEFg")?;
assert_eq!(&items.tail().iter().collect::<String>(), "BcdEFg");Sourcepub fn into_vec(self) -> Vec<T>
pub fn into_vec(self) -> Vec<T>
Take ownership self of type OneOrMore<T> and return a Vec<T> owning all
the items self used to contain.
Since the ownership of the items contained within OneOrMore<T> will be transfered
into the vector. The OneOrMore<T> instance cannot be used anymore afterwards.
§Examples
use manger::Consumable;
use manger::common::OneOrMore;
let (items, _) = <OneOrMore<char>>::consume_from("aBcdEFg")?;
let uppercased: String = items
.into_vec()
.into_iter()
.filter(|character| character.is_ascii_uppercase())
.collect();
assert_eq!(uppercased, "BEF");Sourcepub fn ref_vec(&self) -> Vec<&T>
pub fn ref_vec(&self) -> Vec<&T>
Returns a vector with references to the items in the OneOrMore<T>.
This will not take ownership of the the items in self.
§Examples
use manger::Consumable;
use manger::common::OneOrMore;
let (items, _) = <OneOrMore<char>>::consume_from("aBcdEFg")?;
let uppercased: String = items
.ref_vec()
.into_iter()
.filter(|character| character.is_ascii_uppercase())
.collect();
assert_eq!(uppercased, "BEF");Sourcepub fn mut_vec(&mut self) -> Vec<&mut T>
pub fn mut_vec(&mut self) -> Vec<&mut T>
Returns a vector with mutable references to the items in the OneOrMore<T>.
This will not take ownership of the the items in self.
§Examples
use manger::Consumable;
use manger::common::OneOrMore;
let (mut items, _) = <OneOrMore<char>>::consume_from("aBcdEFg")?;
items
.mut_vec()
.iter_mut()
.filter(|character| character.is_ascii_uppercase())
.for_each(|character| **character = character.to_ascii_lowercase());
let lowercased: String = items.into_iter().collect();
assert_eq!(lowercased, "abcdefg");Trait Implementations§
Source§impl<T: Consumable> Consumable for OneOrMore<T>
impl<T: Consumable> Consumable for OneOrMore<T>
Source§fn consume_from(s: &str) -> Result<(Self, &str), ConsumeError>
fn consume_from(s: &str) -> Result<(Self, &str), ConsumeError>
source to form an item of Self. When consuming is
succesful, it returns the item along with the unconsumed part of the source.
When consuming is unsuccesful it returns the corresponding error. Read moreSource§fn consume_how_many_from(
source: &str,
) -> Result<(Self, &str, usize), ConsumeError>
fn consume_how_many_from( source: &str, ) -> Result<(Self, &str, usize), ConsumeError>
source to form an item of Self. When consuming is
succesful, it returns the item along with the unconsumed part of the source
and the amount of consumed characters.
When consuming is unsuccesful it returns the corresponding error. Read moreSource§fn consume_iter<'a>(source: &'a str) -> ConsumeIter<'a, Self> ⓘ
fn consume_iter<'a>(source: &'a str) -> ConsumeIter<'a, Self> ⓘ
Source§impl<T: Consumable> IntoIterator for OneOrMore<T>
impl<T: Consumable> IntoIterator for OneOrMore<T>
Auto Trait Implementations§
impl<T> Freeze for OneOrMore<T>where
T: Freeze,
impl<T> RefUnwindSafe for OneOrMore<T>where
T: RefUnwindSafe,
impl<T> Send for OneOrMore<T>where
T: Send,
impl<T> Sync for OneOrMore<T>where
T: Sync,
impl<T> Unpin for OneOrMore<T>where
T: Unpin,
impl<T> UnwindSafe for OneOrMore<T>where
T: UnwindSafe,
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more