added example for one_of_with_error

This commit is contained in:
Trevor Settles 2024-03-12 15:41:48 -06:00
parent b146030fff
commit 47b537c278
No known key found for this signature in database
GPG key ID: F46B83058222DBAA

View file

@ -1799,6 +1799,36 @@ macro_rules! one_of {
};
}
/// # Examples
/// ```
/// # use roc_parse::state::{State};
/// # use crate::roc_parse::parser::{Parser, Progress, Progress::{MadeProgress, NoProgress}, word, word1};
/// # use roc_region::all::{Loc, Position};
/// # use roc_parse::ident::lowercase_ident;
/// # use roc_parse::one_of_with_error;
/// # use bumpalo::Bump;
/// # #[derive(Debug, PartialEq)]
/// # enum Problem {
/// # NotFound(Position),
/// # Other(Position),
/// # }
/// # let arena = Bump::new();
/// # fn foo<'a>(arena: &'a Bump) {
/// let parser = one_of_with_error!(
/// Problem::Other;
/// word("hello", Problem::NotFound)
/// );
/// let (progress, output, state) = parser.parse(&arena, State::new("hello, world".as_bytes()), 0).unwrap();
/// assert_eq!(progress, Progress::MadeProgress);
/// assert_eq!(output, ());
/// assert_eq!(state.pos().offset, 5);
///
/// let (progress, err) = parser.parse(&arena, State::new("bye, world".as_bytes()), 0).unwrap_err();
/// assert_eq!(progress, Progress::MadeProgress);
/// assert_eq!(err, Problem::Other(Position::new(0)));
/// # }
/// # foo(&arena);
/// ```
#[macro_export]
macro_rules! one_of_with_error {
($toerror:expr; $p1:expr) => {