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
use super::*;

pub struct ScanState<S: ScanFn> {
    pub first: S::OutputItem,
    pub next: S,
}

pub trait ScanFn: Sized {
    type InputItem;
    type InputResult;
    type OutputItem;
    type OutputResult;
    fn map_input(self, input: Self::InputItem) -> ScanState<Self>;
    fn map_result(self, result: Self::InputResult) -> Self::OutputResult;
}

pub struct ScanWrap<S: ScanFn>(S);

impl<S: ScanFn> FlatScanFn for ScanWrap<S> {
    type InputItem = S::InputItem;
    type InputResult = S::InputResult;
    type OutputList = OptionList<S::OutputItem, Self>;
    type EndList = OptionList<S::OutputItem, Id<S::OutputResult>>;
    fn map_item(self, input: Self::InputItem) -> Self::OutputList {
        let ScanState { first, next } = self.0.map_input(input);
        OptionList::Some {
            first,
            end: ScanWrap(next),
        }
    }
    fn map_result(self, result: Self::InputResult) -> Self::EndList {
        OptionList::End(Id::new(self.0.map_result(result)))
    }
}

pub trait Scan
where
    Self: ListFn,
    Self::End: ResultFn,
{
    fn scan<S: ScanFn<InputItem = Self::Item, InputResult = <Self::End as ResultFn>::Result>>(
        self,
        scan: S,
    ) -> FlatScanState<Self, ScanWrap<S>> {
        self.flat_scan(ScanWrap(scan))
    }
}

impl<L> Scan for L
where
    Self: ListFn,
    Self::End: ResultFn,
{
}