Skip to content

Instantly share code, notes, and snippets.

@faiface
Last active April 20, 2022 23:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save faiface/1cefa2e1e1732089cea35759425b5861 to your computer and use it in GitHub Desktop.
Save faiface/1cefa2e1e1732089cea35759425b5861 to your computer and use it in GitHub Desktop.
// I have to create a custom type because the undesired behavior
// only occurs if I use a custom type for the parsing result. If
// I tried with something like bool, I get my custom error returned
// back just as I want it. Very strange.
pub enum MyUnit { Only }
// Now this parser returns "found end of input" if the input is not
// equal to "only". But a custom error is generated and wanted to be
// returned instead.
pub fn example() -> impl Parser<char, MyUnit, Error = Simple<char>> {
text::ident().padded()
.try_map(|s: String, span| {
match s.as_str() {
"only" => Ok(MyUnit::Only),
_ => Err(Simple::custom(span, format!("invalid bool value {:?}", s))),
}
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment