Trait konst::parsing::HasParser

source ·
pub trait HasParser: Sized {
    type Parser;
}
Expand description

Gets a type that parses Self with a parse_with method.

Implementing this trait allows parsing a type with the parse_with macro.

§Implementing this trait

You can implement this trait like this:

impl HasParser for SomeType {
    // This is usually `Self` for user-defined types.
    type Parser = SomeParser;
}

Then SomeParser is expected to have a parse_with associated function with this signature:

impl SomeParser {
    const fn parse_with<'a>(
        _: konst::Parser<'a>
    ) -> Result<(This, konst::Parser<'a>), SomeErrorType>
}

§Example

use konst::{parse_with, try_rebind, unwrap_ctx};

use konst::parsing::{HasParser, Parser, ParseValueResult};

const PAIR: Pair = {
    let parser = Parser::new("100,200");
    unwrap_ctx!(parse_with!(parser, Pair)).0
};

assert_eq!(PAIR, Pair(100, 200));


#[derive(Debug, PartialEq)]
struct Pair(u32, u64);

impl HasParser for Pair {
    type Parser = Self;
}

impl Pair {
    const fn parse_with(mut parser: Parser<'_>) -> ParseValueResult<'_, Self> {
        try_rebind!{(let left, parser) = parse_with!(parser, u32)}
        try_rebind!{parser = parser.strip_prefix(',')}
        try_rebind!{(let right, parser) = parse_with!(parser, u64)}

        Ok((Pair(left, right), parser))
    }
}

Required Associated Types§

source

type Parser

The type that parses Self with its parse_with associated function.

This is usually Self for user-defined types.

Object Safety§

This trait is not object safe.

Implementations on Foreign Types§

source§

impl HasParser for bool

source§

impl HasParser for i8

source§

impl HasParser for i16

source§

impl HasParser for i32

source§

impl HasParser for i64

source§

impl HasParser for i128

source§

impl HasParser for isize

source§

impl HasParser for u8

source§

impl HasParser for u16

source§

impl HasParser for u32

source§

impl HasParser for u64

source§

impl HasParser for u128

source§

impl HasParser for usize

Implementors§