1use super::*;
2
3pub struct ScanState<S: ScanFn> {
4 pub first: S::OutputItem,
5 pub next: S,
6}
7
8pub trait ScanFn: Sized {
9 type InputItem;
10 type InputResult;
11 type OutputItem;
12 type OutputResult;
13 fn map_input(self, input: Self::InputItem) -> ScanState<Self>;
14 fn map_result(self, result: Self::InputResult) -> Self::OutputResult;
15}
16
17pub struct ScanWrap<S: ScanFn>(S);
18
19impl<S: ScanFn> FlatScanFn for ScanWrap<S> {
20 type InputItem = S::InputItem;
21 type InputResult = S::InputResult;
22 type OutputList = OptionList<S::OutputItem, Self>;
23 type EndList = OptionList<S::OutputItem, Id<S::OutputResult>>;
24 fn map_item(self, input: Self::InputItem) -> Self::OutputList {
25 let ScanState { first, next } = self.0.map_input(input);
26 OptionList::Some {
27 first,
28 end: ScanWrap(next),
29 }
30 }
31 fn map_result(self, result: Self::InputResult) -> Self::EndList {
32 OptionList::End(Id::new(self.0.map_result(result)))
33 }
34}
35
36pub trait Scan
37where
38 Self: ListFn,
39 Self::End: ResultFn,
40{
41 fn scan<S: ScanFn<InputItem = Self::Item, InputResult = <Self::End as ResultFn>::Result>>(
42 self,
43 scan: S,
44 ) -> FlatScanState<Self, ScanWrap<S>> {
45 self.flat_scan(ScanWrap(scan))
46 }
47}
48
49impl<L> Scan for L
50where
51 Self: ListFn,
52 Self::End: ResultFn,
53{
54}