OneOrMore

Struct OneOrMore 

Source
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>

Source

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');
Source

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");
Source

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");
Source

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");
Source

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>

Source§

fn consume_from(s: &str) -> Result<(Self, &str), ConsumeError>

Attempt consume from 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 more
Source§

fn consume_how_many_from( source: &str, ) -> Result<(Self, &str, usize), ConsumeError>

Attempt consume from 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 more
Source§

fn consume_iter<'a>(source: &'a str) -> ConsumeIter<'a, Self>

Fetch a iterator of source to inorderly consume items of Self. Read more
Source§

impl<T: Debug> Debug for OneOrMore<T>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<T: Consumable> IntoIterator for OneOrMore<T>

Source§

type Item = T

The type of the elements being iterated over.
Source§

type IntoIter = IntoIter<<OneOrMore<T> as IntoIterator>::Item>

Which kind of iterator are we turning this into?
Source§

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more

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> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts 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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts 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
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.