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
76
77
78
79
80
81
82
use object_query::Query;
use serde::Deserialize;
#[derive(Debug)]
pub enum MatchError {
Nlsd(nlsd::Error),
MismatchedStaticToken,
EmptyQuery,
UnknownQueryVar,
UnknownDataVar,
UnfilledVar,
UnexpectedEof,
InvalidCtx,
}
pub struct Matcher<'a> {
src: &'a str,
}
impl<'a> Matcher<'a> {
pub fn new(src: &'a str) -> Self {
Self { src }
}
pub fn next_static(&mut self) -> Result<&'a str, MatchError> {
if let Ok((_, tok, rest)) = nl_parser::parse_token(self.src) {
self.src = rest;
Ok(tok)
} else {
Err(MatchError::UnexpectedEof)
}
}
pub fn next_query(&mut self) -> Result<Vec<Query<'a>>, MatchError> {
let mut nloq_de = nloq::Deserializer::from_str(self.src);
let query = nloq_de.query();
if query.is_empty() {
Err(MatchError::EmptyQuery)
} else {
self.src = nloq_de.rest();
Ok(query)
}
}
pub fn next_data<T>(&mut self) -> Result<T, MatchError>
where
T: Deserialize<'a>,
{
let mut nlsd_de = nlsd::Deserializer::from_str(self.src);
let out = T::deserialize(&mut nlsd_de)?;
self.src = nlsd_de.rest();
Ok(out)
}
}
pub trait Match<'a, C>: Sized {
fn match_str(ctx: &mut C, string: &'a str) -> Result<Self, MatchError>;
}
impl From<nlsd::Error> for MatchError {
fn from(err: nlsd::Error) -> Self {
Self::Nlsd(err)
}
}