intuicio_parser/
extension.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
use crate::{ParseResult, Parser, ParserExt, ParserHandle, ParserRegistry};
use std::sync::Arc;

pub mod shorthand {
    use super::*;

    pub fn ext<T: Send + Sync + 'static>(
        f: impl Fn(Arc<T>) -> ParserHandle + Send + Sync + 'static,
    ) -> ParserHandle {
        ExtensionParser::new(f).into_handle()
    }
}

#[derive(Clone)]
pub struct ExtensionParser<T: Send + Sync + 'static> {
    parser_generator: Arc<dyn Fn(Arc<T>) -> ParserHandle + Send + Sync>,
}

impl<T: Send + Sync + 'static> ExtensionParser<T> {
    pub fn new(f: impl Fn(Arc<T>) -> ParserHandle + Send + Sync + 'static) -> Self {
        Self {
            parser_generator: Arc::new(f),
        }
    }
}

impl<T: Send + Sync + 'static> Parser for ExtensionParser<T> {
    fn parse<'a>(&self, registry: &ParserRegistry, input: &'a str) -> ParseResult<'a> {
        if let Some(extension) = registry.extension::<T>() {
            (self.parser_generator)(extension).parse(registry, input)
        } else {
            Err("Could not get ExtensionParser extension!".into())
        }
    }
}

#[cfg(test)]
mod tests {
    use std::sync::RwLock;

    use crate::{
        extension::ExtensionParser,
        shorthand::{ext, lit},
        ParserRegistry,
    };

    fn is_async<T: Send + Sync>() {}

    #[derive(Default)]
    struct Extension {
        pub counter: RwLock<usize>,
    }

    #[test]
    fn test_extension() {
        is_async::<ExtensionParser<()>>();

        let registry = ParserRegistry::default().with_extension(Extension::default());
        let parser = ext::<Extension>(|extension| {
            *extension.counter.write().unwrap() += 1;
            lit("foo")
        });
        let (rest, _) = parser.parse(&registry, "foo").unwrap();
        assert_eq!(rest, "");
        assert_eq!(
            *registry
                .extension::<Extension>()
                .unwrap()
                .counter
                .read()
                .unwrap(),
            1
        );
    }
}