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§
Object Safety§
This trait is not object safe.