intuicio_parser/
inject.rs1use crate::{ParseResult, Parser, ParserExt, ParserHandle, ParserRegistry};
2
3pub mod shorthand {
4 use super::*;
5
6 pub fn inject(id: impl ToString) -> ParserHandle {
7 InjectParser::new(id).into_handle()
8 }
9}
10
11#[derive(Clone)]
12pub struct InjectParser(String);
13
14impl InjectParser {
15 pub fn new(id: impl ToString) -> Self {
16 Self(id.to_string())
17 }
18}
19
20impl Parser for InjectParser {
21 fn parse<'a>(&self, registry: &ParserRegistry, input: &'a str) -> ParseResult<'a> {
22 registry.parse(&self.0, input)
23 }
24}
25
26#[cfg(test)]
27mod tests {
28 use crate::{
29 inject::InjectParser,
30 shorthand::{inject, lit, seq, ws},
31 ParserNoValue, ParserOutput, ParserRegistry,
32 };
33
34 fn is_async<T: Send + Sync>() {}
35
36 #[test]
37 fn test_inject() {
38 is_async::<InjectParser>();
39
40 let registry = ParserRegistry::default()
41 .with_parser("foo", lit("foo"))
42 .with_parser("bar", lit("bar"))
43 .with_parser("ws", ws())
44 .with_parser("main", seq([inject("foo"), inject("ws"), inject("bar")]));
45 let (rest, value) = registry.parse("main", "foo bar").unwrap();
46 assert_eq!(rest, "");
47 let value = value.consume::<Vec<ParserOutput>>().ok().unwrap();
48 assert_eq!(value.len(), 3);
49 for value in value {
50 assert!(value.read::<String>().is_some() || value.read::<ParserNoValue>().is_some());
51 }
52 }
53}