use super::Matcher;
pub struct Number();
pub struct Digit();
impl Number {
pub fn new() -> Self {
Number()
}
}
impl Matcher for Number {
fn match_with(&self, target: &str) -> Option<usize> {
let mut chars = target.chars();
let mut index = 0;
while let Some(c) = chars.next()
&& c.is_ascii_digit()
{
index += c.len_utf8();
}
if index != 0 { Some(index) } else { None }
}
fn capture<'a>(&self, target: &'a str) -> Option<(usize, Vec<&'a str>)> {
let len = self.match_with(target)?;
Some((len, vec![&target[..len]]))
}
}
impl Digit {
pub fn new() -> Self {
Digit()
}
}
impl Matcher for Digit {
fn match_with(&self, target: &str) -> Option<usize> {
if target.starts_with(|c: char| c.is_ascii_digit()) {
Some(1)
} else {
None
}
}
fn capture<'a>(&self, target: &'a str) -> Option<(usize, Vec<&'a str>)> {
let len = self.match_with(target)?;
Some((len, vec![&target[..len]]))
}
}