[−][src]Trait manger::Consumable
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!");
Provided methods
pub fn consume_how_many_from(
source: &str
) -> Result<(Self, &str, usize), ConsumeError>
[src]
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.
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]
Notable traits for ConsumeIter<'a, T>
impl<'a, T> Iterator for ConsumeIter<'a, T> where
T: Consumable, type Item = T;
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);
Implementations on Foreign Types
impl Consumable for char
[src]
pub fn consume_from(s: &str) -> Result<(Self, &str), ConsumeError>
[src]
impl<L, R> Consumable for Either<L, R> where
L: Consumable,
R: Consumable,
[src]
L: Consumable,
R: Consumable,
pub fn consume_from(s: &str) -> Result<(Self, &str), ConsumeError>
[src]
impl Consumable for f32
[src]
pub fn consume_from(source: &str) -> Result<(Self, &str), ConsumeError>
[src]
impl<T: Consumable> Consumable for Option<T>
[src]
pub fn consume_from(source: &str) -> Result<(Option<T>, &str), ConsumeError>
[src]
impl<T: Consumable> Consumable for Box<T>
[src]
pub fn consume_from(s: &str) -> Result<(Box<T>, &str), ConsumeError>
[src]
impl<T: Consumable> Consumable for Vec<T>
[src]
pub fn consume_from(s: &str) -> Result<(Vec<T>, &str), ConsumeError>
[src]
impl<A, B> Consumable for (A, B) where
A: Consumable,
B: Consumable,
[src]
A: Consumable,
B: Consumable,
pub fn consume_from(source: &str) -> Result<(Self, &str), ConsumeError>
[src]
impl<A, B, C> Consumable for (A, B, C) where
A: Consumable,
B: Consumable,
C: Consumable,
[src]
A: Consumable,
B: Consumable,
C: Consumable,
pub fn consume_from(source: &str) -> Result<(Self, &str), ConsumeError>
[src]
impl<A, B, C, D> Consumable for (A, B, C, D) where
A: Consumable,
B: Consumable,
C: Consumable,
D: Consumable,
[src]
A: Consumable,
B: Consumable,
C: Consumable,
D: Consumable,
pub fn consume_from(source: &str) -> Result<(Self, &str), ConsumeError>
[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]
A: Consumable,
B: Consumable,
C: Consumable,
D: Consumable,
E: Consumable,
pub fn consume_from(source: &str) -> Result<(Self, &str), ConsumeError>
[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]
A: Consumable,
B: Consumable,
C: Consumable,
D: Consumable,
E: Consumable,
F: Consumable,
pub fn consume_from(source: &str) -> Result<(Self, &str), ConsumeError>
[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]
A: Consumable,
B: Consumable,
C: Consumable,
D: Consumable,
E: Consumable,
F: Consumable,
G: Consumable,
pub fn consume_from(source: &str) -> Result<(Self, &str), ConsumeError>
[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]
A: Consumable,
B: Consumable,
C: Consumable,
D: Consumable,
E: Consumable,
F: Consumable,
G: Consumable,
H: Consumable,
pub fn consume_from(source: &str) -> Result<(Self, &str), ConsumeError>
[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]
A: Consumable,
B: Consumable,
C: Consumable,
D: Consumable,
E: Consumable,
F: Consumable,
G: Consumable,
H: Consumable,
I: Consumable,
pub fn consume_from(source: &str) -> Result<(Self, &str), ConsumeError>
[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]
A: Consumable,
B: Consumable,
C: Consumable,
D: Consumable,
E: Consumable,
F: Consumable,
G: Consumable,
H: Consumable,
I: Consumable,
J: Consumable,