将鼻涕虫与 Nom 匹配
Match a slug with Nom
一段时间以来,我一直在努力寻找一个合适的解决方案,让 Nom 将鼻涕虫识别为 alpha1
。
所以我可以解析这样的东西
fn parse<'a>(text: &'a str) -> IResult<&'a str, &'a str> {
delimited(char(':'), slug, char(':'))(text)
}
assert!(
parse(":hello-world-i-only-accept-alpha-numeric-char-and-dashes:"),
"hello-world-i-only-accept-alpha-numeric-char-and-dashes"
);
我试过类似的东西,但它似乎不起作用。
fn slug<T, E: ParseError<T>>(input: T) -> IResult<T, T, E>
where
T: InputTakeAtPosition,
<T as InputTakeAtPosition>::Item: AsChar + Clone,
{
input.split_at_position1(
|item| {
let c = item.clone().as_char();
!(item.is_alpha() || c == '-')
},
ErrorKind::Char,
)
}
PS: 你知道怎么告诉 Nom 一个 slug 中的“-”不能在开头也不能在结尾吗?
有 nom::multi::separated_list
for exactly this. And since you want the result to be string itself rather than a vector of segments, combining it with nom::combinator::recognize
可以解决问题:
use std::error::Error;
use nom::{
IResult,
character::complete::{alphanumeric1, char},
combinator::recognize,
multi::separated_list,
sequence::delimited,
};
fn slug_parse<'a>(text: &'a str) -> IResult<&'a str, &'a str> {
let slug = separated_list(char('-'), alphanumeric1);
delimited(char(':'), recognize(slug), char(':'))(text)
}
fn main() -> Result<(), Box<dyn Error>> {
let (_, res) = slug_parse(":hello-world-i-only-accept-alpha-numeric-char-and-dashes:")?;
assert_eq!(
res,
"hello-world-i-only-accept-alpha-numeric-char-and-dashes"
);
Ok(())
}
一段时间以来,我一直在努力寻找一个合适的解决方案,让 Nom 将鼻涕虫识别为 alpha1
。
所以我可以解析这样的东西
fn parse<'a>(text: &'a str) -> IResult<&'a str, &'a str> {
delimited(char(':'), slug, char(':'))(text)
}
assert!(
parse(":hello-world-i-only-accept-alpha-numeric-char-and-dashes:"),
"hello-world-i-only-accept-alpha-numeric-char-and-dashes"
);
我试过类似的东西,但它似乎不起作用。
fn slug<T, E: ParseError<T>>(input: T) -> IResult<T, T, E>
where
T: InputTakeAtPosition,
<T as InputTakeAtPosition>::Item: AsChar + Clone,
{
input.split_at_position1(
|item| {
let c = item.clone().as_char();
!(item.is_alpha() || c == '-')
},
ErrorKind::Char,
)
}
PS: 你知道怎么告诉 Nom 一个 slug 中的“-”不能在开头也不能在结尾吗?
有 nom::multi::separated_list
for exactly this. And since you want the result to be string itself rather than a vector of segments, combining it with nom::combinator::recognize
可以解决问题:
use std::error::Error;
use nom::{
IResult,
character::complete::{alphanumeric1, char},
combinator::recognize,
multi::separated_list,
sequence::delimited,
};
fn slug_parse<'a>(text: &'a str) -> IResult<&'a str, &'a str> {
let slug = separated_list(char('-'), alphanumeric1);
delimited(char(':'), recognize(slug), char(':'))(text)
}
fn main() -> Result<(), Box<dyn Error>> {
let (_, res) = slug_parse(":hello-world-i-only-accept-alpha-numeric-char-and-dashes:")?;
assert_eq!(
res,
"hello-world-i-only-accept-alpha-numeric-char-and-dashes"
);
Ok(())
}