layered_nlp/ll_line/x/
seq.rs1use super::{LLLine, ToIdx, XDirection, XMatch};
2
3pub trait Seq {
4 type Out;
5
6 fn into_seq(self) -> Self::Out;
7}
8
9impl<A, B> Seq for (A, B) {
10 type Out = Seq2<A, B>;
11
12 fn into_seq(self) -> Self::Out {
13 Seq2(self.0, self.1)
14 }
15}
16
17impl<A, B, C> Seq for (A, B, C) {
18 type Out = Seq3<A, B, C>;
19
20 fn into_seq(self) -> Self::Out {
21 Seq3(self.0, self.1, self.2)
22 }
23}
24
25pub struct Seq2<A, B>(pub A, pub B);
26
27impl<'l, A: XMatch<'l>, B: XMatch<'l>> XMatch<'l> for Seq2<A, B> {
28 type Out = (A::Out, B::Out);
29
30 fn go<M>(&self, direction: &M, ll_line: &'l LLLine) -> Vec<(Self::Out, ToIdx)>
31 where
32 M: XDirection<'l>,
33 {
34 self.0
35 .go(direction, ll_line)
36 .into_iter()
37 .flat_map(|(a, to_idx)| {
38 direction
39 .after(to_idx.0, ll_line)
40 .map(|direction| self.1.go(&direction, ll_line))
41 .unwrap_or_else(Vec::new)
42 .into_iter()
43 .map(move |(b, to_idx)| ((a, b), to_idx))
44 })
45 .collect()
46 }
47}
48
49pub struct Seq3<A, B, C>(pub A, pub B, pub C);
50
51impl<'l, A: XMatch<'l>, B: XMatch<'l>, C: XMatch<'l>> XMatch<'l> for Seq3<A, B, C> {
52 type Out = (A::Out, B::Out, C::Out);
53
54 fn go<M>(&self, direction: &M, ll_line: &'l LLLine) -> Vec<(Self::Out, ToIdx)>
55 where
56 M: XDirection<'l>,
57 {
58 self.0
59 .go(direction, ll_line)
60 .into_iter()
61 .flat_map(|(a, to_idx)| {
62 direction
63 .after(to_idx.0, ll_line)
64 .map(|direction| self.1.go(&direction, ll_line))
65 .unwrap_or_else(Vec::new)
66 .into_iter()
67 .flat_map(move |(b, to_idx)| {
68 direction
69 .after(to_idx.0, ll_line)
70 .map(|direction| self.2.go(&direction, ll_line))
71 .unwrap_or_else(Vec::new)
72 .into_iter()
73 .map(move |(c, to_idx)| ((a, b, c), to_idx))
74 })
75 })
76 .collect()
77 }
78}