Trait konst::parsing::ParserFor [−][src]
This is supported on crate feature
parsing_no_proc
only.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 ParserFor 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::{ParserFor, Parser, ParseValueResult};
const PAIR: Pair = {
let parser = Parser::from_str("100,200");
unwrap_ctx!(parse_with!(parser, Pair)).0
};
assert_eq!(PAIR, Pair(100, 200));
#[derive(Debug, PartialEq)]
struct Pair(u32, u64);
impl ParserFor 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_u8(b',')}
try_rebind!{(let right, parser) = parse_with!(parser, u64)}
Ok((Pair(left, right), parser))
}
}