[][src]Trait manger::Consumable

pub trait Consumable: Sized {
    pub fn consume_from(source: &str) -> Result<(Self, &str), ConsumeError>;

    pub fn consume_how_many_from(
        source: &str
    ) -> Result<(Self, &str, usize), ConsumeError> { ... }
pub fn consume_iter<'a>(source: &'a str) -> ConsumeIter<'a, Self>

Notable traits for ConsumeIter<'a, T>

impl<'a, T> Iterator for ConsumeIter<'a, T> where
    T: Consumable
type Item = T;
{ ... } }

Trait that defines whether a trait can be interpretted for a source string or not. It is the trait that defines most behaviour for manger.

Consumable allows for taking a part of the start of a source string and turn it into a instance of Self and the unconsumed part of the source.

Implementation

Most of the implementations for this trait can be done with the consume_struct or consume_enum. It is also the preferred way to implement Consumable for most types since it handles error handling properly as well.

Custom implementations

This trait can be implemented for types by implementing the consume_from function. The consume_from function takes a source string and outputs the instance of Self and the unconsumed part of the source or will return how the consuming failed.

It is highly suggested that the implementation of consume_from takes into account utf-8, since most functions in manger work with the utf-8 standard. This can be more easily done crates like utf8-slice, which allows for using utf-8 character indicices in slices instead of using byte indices.

Examples

use manger::{ Consumable, consume_struct };

let source = "[3][4][-5]";

struct EncasedInteger(i32);
consume_struct!(
    EncasedInteger => [
        > '[',
        value: i32,
        > ']';
        (value)
    ]
);

let product: i32 = EncasedInteger::consume_iter(source)
    .map(|EncasedInteger(value)| value)
    .product();

assert_eq!(product, -60);

Required methods

pub fn consume_from(source: &str) -> Result<(Self, &str), ConsumeError>[src]

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.

This is the core function to implement when implementing the Consumable trait.

Implementation note

It is highly recommended to take into account UTF-8 characters. This is reasonably easy with .chars() on &str or with the crate utf8-slice.

Examples

use manger::Consumable;

let source = "42 is the answer!";

let (answer, unconsumed) = u32::consume_from(source)?;

assert_eq!(answer, 42);
assert_eq!(unconsumed, " is the answer!");
Loading content...

Provided methods

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

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.

Note

This counts UTF-8 characters and not byte indices. This can create some confusion when slicing afterwards. One can use a crate such as utf8-slice to compensate for this.

Examples

use manger::Consumable;

let source = "42 is the answer!";

let (answer, unconsumed, consumed_amount) = u32::consume_how_many_from(source)?;

assert_eq!(answer, 42);
assert_eq!(unconsumed, " is the answer!");
assert_eq!(consumed_amount, 2);

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

Notable traits for ConsumeIter<'a, T>

impl<'a, T> Iterator for ConsumeIter<'a, T> where
    T: Consumable
type Item = T;
[src]

Fetch a iterator of source to inorderly consume items of Self.

Examples

use manger::{ Consumable, consume_struct };

let source = "(3)(4)(5)";

struct EncasedInteger(u32);
consume_struct!(
    EncasedInteger => [
        > '(',
        value: u32,
        > ')';
        (value)
    ]
);

let product: u32 = EncasedInteger::consume_iter(source)
    .map(|EncasedInteger(value)| value)
    .product();

assert_eq!(product, 60);
Loading content...

Implementations on Foreign Types

impl Consumable for char[src]

impl<L, R> Consumable for Either<L, R> where
    L: Consumable,
    R: Consumable
[src]

impl Consumable for f32[src]

impl<T: Consumable> Consumable for Option<T>[src]

impl<T: Consumable> Consumable for Box<T>[src]

impl<T: Consumable> Consumable for Vec<T>[src]

impl<A, B> Consumable for (A, B) where
    A: Consumable,
    B: Consumable
[src]

impl<A, B, C> Consumable for (A, B, C) where
    A: Consumable,
    B: Consumable,
    C: Consumable
[src]

impl<A, B, C, D> Consumable for (A, B, C, D) where
    A: Consumable,
    B: Consumable,
    C: Consumable,
    D: Consumable
[src]

impl<A, B, C, D, E> Consumable for (A, B, C, D, E) where
    A: Consumable,
    B: Consumable,
    C: Consumable,
    D: Consumable,
    E: Consumable
[src]

impl<A, B, C, D, E, F> Consumable for (A, B, C, D, E, F) where
    A: Consumable,
    B: Consumable,
    C: Consumable,
    D: Consumable,
    E: Consumable,
    F: Consumable
[src]

impl<A, B, C, D, E, F, G> Consumable for (A, B, C, D, E, F, G) where
    A: Consumable,
    B: Consumable,
    C: Consumable,
    D: Consumable,
    E: Consumable,
    F: Consumable,
    G: Consumable
[src]

impl<A, B, C, D, E, F, G, H> Consumable for (A, B, C, D, E, F, G, H) where
    A: Consumable,
    B: Consumable,
    C: Consumable,
    D: Consumable,
    E: Consumable,
    F: Consumable,
    G: Consumable,
    H: Consumable
[src]

impl<A, B, C, D, E, F, G, H, I> Consumable for (A, B, C, D, E, F, G, H, I) where
    A: Consumable,
    B: Consumable,
    C: Consumable,
    D: Consumable,
    E: Consumable,
    F: Consumable,
    G: Consumable,
    H: Consumable,
    I: Consumable
[src]

impl<A, B, C, D, E, F, G, H, I, J> Consumable for (A, B, C, D, E, F, G, H, I, J) where
    A: Consumable,
    B: Consumable,
    C: Consumable,
    D: Consumable,
    E: Consumable,
    F: Consumable,
    G: Consumable,
    H: Consumable,
    I: Consumable,
    J: Consumable
[src]

impl Consumable for u8[src]

impl Consumable for u16[src]

impl Consumable for u32[src]

impl Consumable for u64[src]

impl Consumable for u128[src]

impl Consumable for usize[src]

impl Consumable for i8[src]

impl Consumable for i16[src]

impl Consumable for i32[src]

impl Consumable for i64[src]

impl Consumable for i128[src]

impl Consumable for isize[src]

Loading content...

Implementors

impl Consumable for manger::chars::alpha::A[src]

impl Consumable for manger::chars::alpha::B[src]

impl Consumable for manger::chars::alpha::C[src]

impl Consumable for manger::chars::alpha::D[src]

impl Consumable for manger::chars::alpha::E[src]

impl Consumable for manger::chars::alpha::F[src]

impl Consumable for manger::chars::alpha::G[src]

impl Consumable for manger::chars::alpha::H[src]

impl Consumable for manger::chars::alpha::I[src]

impl Consumable for manger::chars::alpha::J[src]

impl Consumable for manger::chars::alpha::K[src]

impl Consumable for manger::chars::alpha::L[src]

impl Consumable for manger::chars::alpha::M[src]

impl Consumable for manger::chars::alpha::N[src]

impl Consumable for manger::chars::alpha::O[src]

impl Consumable for manger::chars::alpha::P[src]

impl Consumable for manger::chars::alpha::Q[src]

impl Consumable for manger::chars::alpha::R[src]

impl Consumable for manger::chars::alpha::S[src]

impl Consumable for manger::chars::alpha::T[src]

impl Consumable for manger::chars::alpha::U[src]

impl Consumable for manger::chars::alpha::V[src]

impl Consumable for manger::chars::alpha::W[src]

impl Consumable for manger::chars::alpha::X[src]

impl Consumable for manger::chars::alpha::Y[src]

impl Consumable for manger::chars::alpha::Z[src]

impl Consumable for Digit[src]

impl Consumable for Sign[src]

impl Consumable for manger::chars::alpha::lower::A[src]

impl Consumable for manger::chars::alpha::lower::B[src]

impl Consumable for manger::chars::alpha::lower::C[src]

impl Consumable for manger::chars::alpha::lower::D[src]

impl Consumable for manger::chars::alpha::lower::E[src]

impl Consumable for manger::chars::alpha::lower::F[src]

impl Consumable for manger::chars::alpha::lower::G[src]

impl Consumable for manger::chars::alpha::lower::H[src]

impl Consumable for manger::chars::alpha::lower::I[src]

impl Consumable for manger::chars::alpha::lower::J[src]

impl Consumable for manger::chars::alpha::lower::K[src]

impl Consumable for manger::chars::alpha::lower::L[src]

impl Consumable for manger::chars::alpha::lower::M[src]

impl Consumable for manger::chars::alpha::lower::N[src]

impl Consumable for manger::chars::alpha::lower::O[src]

impl Consumable for manger::chars::alpha::lower::P[src]

impl Consumable for manger::chars::alpha::lower::Q[src]

impl Consumable for manger::chars::alpha::lower::R[src]

impl Consumable for manger::chars::alpha::lower::S[src]

impl Consumable for manger::chars::alpha::lower::T[src]

impl Consumable for manger::chars::alpha::lower::U[src]

impl Consumable for manger::chars::alpha::lower::V[src]

impl Consumable for manger::chars::alpha::lower::W[src]

impl Consumable for manger::chars::alpha::lower::X[src]

impl Consumable for manger::chars::alpha::lower::Y[src]

impl Consumable for manger::chars::alpha::lower::Z[src]

impl Consumable for manger::chars::alpha::upper::A[src]

impl Consumable for manger::chars::alpha::upper::B[src]

impl Consumable for manger::chars::alpha::upper::C[src]

impl Consumable for manger::chars::alpha::upper::D[src]

impl Consumable for manger::chars::alpha::upper::E[src]

impl Consumable for manger::chars::alpha::upper::F[src]

impl Consumable for manger::chars::alpha::upper::G[src]

impl Consumable for manger::chars::alpha::upper::H[src]

impl Consumable for manger::chars::alpha::upper::I[src]

impl Consumable for manger::chars::alpha::upper::J[src]

impl Consumable for manger::chars::alpha::upper::K[src]

impl Consumable for manger::chars::alpha::upper::L[src]

impl Consumable for manger::chars::alpha::upper::M[src]

impl Consumable for manger::chars::alpha::upper::N[src]

impl Consumable for manger::chars::alpha::upper::O[src]

impl Consumable for manger::chars::alpha::upper::P[src]

impl Consumable for manger::chars::alpha::upper::Q[src]

impl Consumable for manger::chars::alpha::upper::R[src]

impl Consumable for manger::chars::alpha::upper::S[src]

impl Consumable for manger::chars::alpha::upper::T[src]

impl Consumable for manger::chars::alpha::upper::U[src]

impl Consumable for manger::chars::alpha::upper::V[src]

impl Consumable for manger::chars::alpha::upper::W[src]

impl Consumable for manger::chars::alpha::upper::X[src]

impl Consumable for manger::chars::alpha::upper::Y[src]

impl Consumable for manger::chars::alpha::upper::Z[src]

impl Consumable for Eight[src]

impl Consumable for Five[src]

impl Consumable for Four[src]

impl Consumable for Nine[src]

impl Consumable for One[src]

impl Consumable for Seven[src]

impl Consumable for Six[src]

impl Consumable for Three[src]

impl Consumable for Two[src]

impl Consumable for Zero[src]

impl Consumable for Ampersand[src]

impl Consumable for Asterisk[src]

impl Consumable for At[src]

impl Consumable for Backslash[src]

impl Consumable for Caret[src]

impl Consumable for CloseBrace[src]

impl Consumable for CloseBracket[src]

impl Consumable for CloseParenthese[src]

impl Consumable for Colon[src]

impl Consumable for Comma[src]

impl Consumable for Dollar[src]

impl Consumable for DoubleQuotes[src]

impl Consumable for Equals[src]

impl Consumable for Exclamation[src]

impl Consumable for Grave[src]

impl Consumable for Hash[src]

impl Consumable for Hyphen[src]

impl Consumable for LessThan[src]

impl Consumable for MoreThan[src]

impl Consumable for NewLine[src]

impl Consumable for OpenBrace[src]

impl Consumable for OpenBracket[src]

impl Consumable for OpenParenthese[src]

impl Consumable for Percent[src]

impl Consumable for Period[src]

impl Consumable for Plus[src]

impl Consumable for Question[src]

impl Consumable for Semicolon[src]

impl Consumable for SingleQuote[src]

impl Consumable for Slash[src]

impl Consumable for Space[src]

impl Consumable for Tab[src]

impl Consumable for Tilde[src]

impl Consumable for Underscore[src]

impl Consumable for VerticalBar[src]

impl Consumable for CatchAll[src]

impl Consumable for Whitespace[src]

impl<T: Consumable> Consumable for OneOrMore<T>[src]

Loading content...