1//! The module contains [`LimitRows`] records iterator.
2
3use crate::grid::records::IntoRecords;
4
5/// [`LimitRows`] is an records iterator which limits amount of rows.
6#[derive(Debug)]
7pub struct LimitRows<I> {
8 records: I,
9 limit: usize,
10}
11
12impl LimitRows<()> {
13 /// Creates new [`LimitRows`] iterator.
14 pub fn new<I: IntoRecords>(records: I, limit: usize) -> LimitRows<I> {
15 LimitRows { records, limit }
16 }
17}
18
19impl<I> IntoRecords for LimitRows<I>
20where
21 I: IntoRecords,
22{
23 type Cell = I::Cell;
24 type IterColumns = I::IterColumns;
25 type IterRows = LimitRowsIter<<I::IterRows as IntoIterator>::IntoIter>;
26
27 fn iter_rows(self) -> Self::IterRows {
28 LimitRowsIter {
29 iter: self.records.iter_rows().into_iter(),
30 limit: self.limit,
31 }
32 }
33}
34
35/// A rows iterator for [`LimitRows`]
36#[derive(Debug)]
37pub struct LimitRowsIter<I> {
38 iter: I,
39 limit: usize,
40}
41
42impl<I> Iterator for LimitRowsIter<I>
43where
44 I: Iterator,
45 I::Item: IntoIterator,
46 <I::Item as IntoIterator>::Item: AsRef<str>,
47{
48 type Item = I::Item;
49
50 fn next(&mut self) -> Option<Self::Item> {
51 if self.limit == 0 {
52 return None;
53 }
54
55 self.limit -= 1;
56
57 self.iter.next()
58 }
59}
60