strongly typed ?
Stargateur opened this issue · comments
Should we consider make the parser strongly typed:
#[repr(transparent)]
struct Alpha {
c: char,
}
pub fn alpha<I, E>(input: I) -> IResult<I, Alpha, E>
where
I: InputIter + Slice<RangeFrom<usize>>,
<I as InputIter>::Item: AsChar,
E: ParseError<I>,
{
let (input, c) = satisfy(is_alpha)(input)?;
Ok((input, Alpha { c }))
}
Pro:
- Allow compile time check
- If the user want this, this will reduce the code of user side
- most user will discard this anyway so no call to
into_inner()
- Increase debug info.
Con:
- User side that don't want it could require call like
into_inner()
to get rid of the wrapper. - make lib more heavy
Maybe as a feature ?
I like the idea, but I am not sure on which level of abstraction this makes sense. I used this technique in another crate (imap-codec) and ended up with multiple string types e.g. IString, NString, Atom, ..., which were really useful, but created a lot boilerplate.
I think it is a powerful technique to increase the reliability of a library. But if we want to do that, I would propose to play around with it and see how well it works out. Maybe we could test how another library based on abnf-core, e.g., the abnf crate, would feel like using the typesafe variant?