summaryrefslogtreecommitdiffstats
path: root/vendor/tabled/src/grid/records/into_records/limit_row_records.rs
diff options
context:
space:
mode:
Diffstat (limited to 'vendor/tabled/src/grid/records/into_records/limit_row_records.rs')
-rw-r--r--vendor/tabled/src/grid/records/into_records/limit_row_records.rs59
1 files changed, 59 insertions, 0 deletions
diff --git a/vendor/tabled/src/grid/records/into_records/limit_row_records.rs b/vendor/tabled/src/grid/records/into_records/limit_row_records.rs
new file mode 100644
index 000000000..a461c6682
--- /dev/null
+++ b/vendor/tabled/src/grid/records/into_records/limit_row_records.rs
@@ -0,0 +1,59 @@
+//! The module contains [`LimitRows`] records iterator.
+
+use crate::grid::records::IntoRecords;
+
+/// [`LimitRows`] is an records iterator which limits amount of rows.
+#[derive(Debug)]
+pub struct LimitRows<I> {
+ records: I,
+ limit: usize,
+}
+
+impl LimitRows<()> {
+ /// Creates new [`LimitRows`] iterator.
+ pub fn new<I: IntoRecords>(records: I, limit: usize) -> LimitRows<I> {
+ LimitRows { records, limit }
+ }
+}
+
+impl<I> IntoRecords for LimitRows<I>
+where
+ I: IntoRecords,
+{
+ type Cell = I::Cell;
+ type IterColumns = I::IterColumns;
+ type IterRows = LimitRowsIter<<I::IterRows as IntoIterator>::IntoIter>;
+
+ fn iter_rows(self) -> Self::IterRows {
+ LimitRowsIter {
+ iter: self.records.iter_rows().into_iter(),
+ limit: self.limit,
+ }
+ }
+}
+
+/// A rows iterator for [`LimitRows`]
+#[derive(Debug)]
+pub struct LimitRowsIter<I> {
+ iter: I,
+ limit: usize,
+}
+
+impl<I> Iterator for LimitRowsIter<I>
+where
+ I: Iterator,
+ I::Item: IntoIterator,
+ <I::Item as IntoIterator>::Item: AsRef<str>,
+{
+ type Item = I::Item;
+
+ fn next(&mut self) -> Option<Self::Item> {
+ if self.limit == 0 {
+ return None;
+ }
+
+ self.limit -= 1;
+
+ self.iter.next()
+ }
+}