add helper method for parsing exactly N of something
This commit is contained in:
parent
5eb76e73ca
commit
743f1e46fc
|
@ -1,4 +1,5 @@
|
|||
use num::Num;
|
||||
use std::fmt::Debug;
|
||||
use yap::{IntoTokens, Tokens};
|
||||
pub fn space(tokens: &mut impl Tokens<Item = char>) -> bool {
|
||||
tokens.token(' ')
|
||||
|
@ -36,6 +37,19 @@ pub fn parse_number_with_radix<T: Num>(
|
|||
}
|
||||
}
|
||||
|
||||
pub fn parse_n<T: Debug, P: Tokens<Item = char>, const N: usize>(
|
||||
tokens: &mut P,
|
||||
parser: &dyn Fn(&mut P) -> Option<T>,
|
||||
separator: &dyn Fn(&mut P) -> bool,
|
||||
) -> Option<[T; N]> {
|
||||
let elements: Vec<T> = tokens.sep_by(parser, separator).collect();
|
||||
if elements.len() != 10 {
|
||||
None
|
||||
} else {
|
||||
Some(elements.try_into().unwrap())
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod test {
|
||||
use yap::IntoTokens;
|
||||
|
|
Loading…
Reference in a new issue