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
use core::marker::PhantomData;

use crate::{
    error::UniError, protocol::ProtocolDescription, protocols::sequence, walk::WalkOnce, Fallible,
    Visitor, WalkStatus, Walker,
};

pub struct IterWalker<'value, 'ctx, I: Iterator, WalkerErr, VisitorErr>(
    I,
    fn(
        I::Item,
        &mut dyn Visitor<'value, 'ctx, WalkerErr, Error = VisitorErr>,
    ) -> Result<WalkStatus, UniError<WalkerErr, VisitorErr>>,
    PhantomData<&'value &'ctx ()>,
);

impl<'value, 'ctx, I, WalkerErr, VisitorErr: 'value>
    IterWalker<'value, 'ctx, I, WalkerErr, VisitorErr>
where
    I: Iterator,
{
    pub fn new<T>(into_iter: T) -> Self
    where
        T: IntoIterator<IntoIter = I>,
        <I as Iterator>::Item: WalkOnce<'value, 'ctx, Error = WalkerErr>,
    {
        Self(
            into_iter.into_iter(),
            |item, visitor| item.into_walker().walk(visitor),
            PhantomData,
        )
    }
}

impl<'value, 'ctx, I: Iterator, WalkerErr, VisitorErr> Fallible
    for IterWalker<'value, 'ctx, I, WalkerErr, VisitorErr>
{
    type Error = WalkerErr;
}

impl<'value, 'ctx, I, WalkerErr, VisitorErr> Walker<'value, 'ctx, VisitorErr>
    for IterWalker<'value, 'ctx, I, WalkerErr, VisitorErr>
where
    I: Iterator,
{
    fn walk(
        &mut self,
        visitor: &mut dyn Visitor<'value, 'ctx, Self::Error, Error = VisitorErr>,
    ) -> Result<WalkStatus, UniError<Self::Error, VisitorErr>> {
        while let Some(item) = self.0.next() {
            (self.1)(item, visitor)?;
        }

        Ok(WalkStatus::Done)
    }
}