konst::parsing

Trait 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.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so 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§