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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
use crate::sql_type::{Collection, FromSql};
use crate::{Error, Result};
use std::iter::FusedIterator;
use std::marker::PhantomData;

#[derive(Clone, Debug)]
enum State {
    Begin,
    Current(i32),
    End,
}

impl State {
    fn next(&self, coll: &Collection) -> Result<State> {
        let index_result = match self {
            State::Begin => coll.first_index(),
            State::Current(index) => coll.next_index(*index),
            State::End => return Ok(State::End),
        };
        match index_result {
            Ok(index) => Ok(State::Current(index)),
            Err(Error::NoDataFound) => Ok(State::End),
            Err(err) => Err(err),
        }
    }

    fn next_back(&self, coll: &Collection) -> Result<State> {
        let index_result = match self {
            State::Begin => return Ok(State::Begin),
            State::Current(index) => coll.prev_index(*index),
            State::End => coll.last_index(),
        };
        match index_result {
            Ok(index) => Ok(State::Current(index)),
            Err(Error::NoDataFound) => Ok(State::Begin),
            Err(err) => Err(err),
        }
    }
}

/// An iterator over the elements of a Collection.
///
/// This struct is created by [`Collection::iter()`]. See its documentation for more.
#[derive(Clone, Debug)]
pub struct Iter<'a, T: FromSql> {
    coll: &'a Collection,
    state: State,
    phantom: PhantomData<T>,
}

impl<T: FromSql> Iter<'_, T> {
    fn try_next(&mut self) -> Result<Option<(i32, T)>> {
        let next_state = self.state.next(self.coll)?;
        let result = if let State::Current(index) = next_state {
            Some((index, self.coll.get(index)?))
        } else {
            None
        };
        self.state = next_state;
        Ok(result)
    }

    fn try_next_back(&mut self) -> Result<Option<(i32, T)>> {
        let next_state = self.state.next_back(self.coll)?;
        let result = if let State::Current(index) = next_state {
            Some((index, self.coll.get(index)?))
        } else {
            None
        };
        self.state = next_state;
        Ok(result)
    }
}

impl<T: FromSql> Iter<'_, T> {
    pub(crate) fn new(coll: &Collection) -> Iter<T> {
        Iter {
            coll,
            state: State::Begin,
            phantom: PhantomData,
        }
    }
}

impl<T> Iterator for Iter<'_, T>
where
    T: FromSql,
{
    type Item = Result<(i32, T)>;
    fn next(&mut self) -> Option<Result<(i32, T)>> {
        self.try_next().transpose()
    }
}

impl<T> DoubleEndedIterator for Iter<'_, T>
where
    T: FromSql,
{
    fn next_back(&mut self) -> Option<Result<(i32, T)>> {
        self.try_next_back().transpose()
    }
}

impl<T: FromSql> FusedIterator for Iter<'_, T> {}

/// An iterator over the values of a Collection.
///
/// This struct is created by [`Collection::values()`]. See its documentation for more.
#[derive(Clone, Debug)]
pub struct Values<'a, T: FromSql> {
    coll: &'a Collection,
    state: State,
    phantom: PhantomData<T>,
}

impl<T: FromSql> Values<'_, T> {
    fn try_next(&mut self) -> Result<Option<T>> {
        let next_state = self.state.next(self.coll)?;
        let result = if let State::Current(index) = next_state {
            Some(self.coll.get(index)?)
        } else {
            None
        };
        self.state = next_state;
        Ok(result)
    }

    fn try_next_back(&mut self) -> Result<Option<T>> {
        let next_state = self.state.next_back(self.coll)?;
        let result = if let State::Current(index) = next_state {
            Some(self.coll.get(index)?)
        } else {
            None
        };
        self.state = next_state;
        Ok(result)
    }
}

impl<T: FromSql> Values<'_, T> {
    pub(crate) fn new(coll: &Collection) -> Values<T> {
        Values {
            coll,
            state: State::Begin,
            phantom: PhantomData,
        }
    }
}

impl<T> Iterator for Values<'_, T>
where
    T: FromSql,
{
    type Item = Result<T>;
    fn next(&mut self) -> Option<Result<T>> {
        self.try_next().transpose()
    }
}

impl<T> DoubleEndedIterator for Values<'_, T>
where
    T: FromSql,
{
    fn next_back(&mut self) -> Option<Result<T>> {
        self.try_next_back().transpose()
    }
}

impl<T: FromSql> FusedIterator for Values<'_, T> {}

/// An iterator over the indices of a Collection.
///
/// This struct is created by [`Collection::indices()`]. See its documentation for more.
#[derive(Clone, Debug)]
pub struct Indices<'a> {
    coll: &'a Collection,
    state: State,
}

impl Indices<'_> {
    pub(crate) fn new(coll: &Collection) -> Indices {
        Indices {
            coll,
            state: State::Begin,
        }
    }

    fn try_next(&mut self) -> Result<Option<i32>> {
        self.state = self.state.next(self.coll)?;
        Ok(if let State::Current(index) = self.state {
            Some(index)
        } else {
            None
        })
    }

    fn try_next_back(&mut self) -> Result<Option<i32>> {
        self.state = self.state.next_back(self.coll)?;
        Ok(if let State::Current(index) = self.state {
            Some(index)
        } else {
            None
        })
    }
}

impl Iterator for Indices<'_> {
    type Item = Result<i32>;
    fn next(&mut self) -> Option<Result<i32>> {
        self.try_next().transpose()
    }
}

impl DoubleEndedIterator for Indices<'_> {
    fn next_back(&mut self) -> Option<Result<i32>> {
        self.try_next_back().transpose()
    }
}

impl FusedIterator for Indices<'_> {}