-
Notifications
You must be signed in to change notification settings - Fork 87
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #487 from zhiburt/patch/add-layout-iterator
Add tabled::iter::LayoutIterator
- Loading branch information
Showing
5 changed files
with
140 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
#[cfg(feature = "std")] | ||
use crate::{Table, Tabled}; | ||
|
||
/// [`LayoutIterator`] is a convient abstraction to iterate over rows/columns. | ||
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Default)] | ||
pub struct LayoutIterator { | ||
from: usize, | ||
to: usize, | ||
batch: usize, | ||
i: usize, | ||
} | ||
|
||
impl LayoutIterator { | ||
/// Creates a custom [`LayoutIterator`] instance. | ||
pub fn new(from: usize, to: usize, batch: usize) -> Self { | ||
Self { | ||
from, | ||
to, | ||
batch, | ||
i: 0, | ||
} | ||
} | ||
|
||
/// Creates a record iterator for KV table created by [`Table::kv`]. | ||
/// So it basically skips all rows until next record starts. | ||
#[cfg(feature = "std")] | ||
pub fn kv_batches<T>(t: &Table) -> Self | ||
where | ||
T: Tabled, | ||
{ | ||
Self::new(0, t.count_rows(), T::LENGTH) | ||
} | ||
} | ||
|
||
impl Iterator for LayoutIterator { | ||
type Item = usize; | ||
|
||
fn next(&mut self) -> Option<Self::Item> { | ||
if self.batch == 0 { | ||
return None; | ||
} | ||
|
||
if self.from >= self.to { | ||
return None; | ||
} | ||
|
||
let value = self.i * self.batch; | ||
self.from += self.batch; | ||
self.i += 1; | ||
|
||
Some(value) | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use crate::iter::LayoutIterator; | ||
|
||
#[test] | ||
fn test_layout_iterator() { | ||
assert_eq!( | ||
LayoutIterator::new(0, 5, 1).collect::<Vec<_>>(), | ||
vec![0, 1, 2, 3, 4] | ||
); | ||
assert_eq!( | ||
LayoutIterator::new(0, 5, 2).collect::<Vec<_>>(), | ||
vec![0, 2, 4] | ||
); | ||
assert_eq!( | ||
LayoutIterator::new(0, 6, 2).collect::<Vec<_>>(), | ||
vec![0, 2, 4] | ||
); | ||
assert_eq!(LayoutIterator::new(0, 0, 2).collect::<Vec<_>>(), vec![]); | ||
assert_eq!(LayoutIterator::new(0, 5, 0).collect::<Vec<_>>(), vec![]); | ||
assert_eq!(LayoutIterator::new(0, 0, 0).collect::<Vec<_>>(), vec![]); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
//! A module for iterator structures. | ||
mod layout_iterator; | ||
|
||
pub use layout_iterator::LayoutIterator; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
#![cfg(all(feature = "std", feature = "derive"))] | ||
|
||
use tabled::{ | ||
iter::LayoutIterator, | ||
settings::{style::BorderSpanCorrection, Span, Style}, | ||
Table, Tabled, | ||
}; | ||
|
||
use testing_table::test_table; | ||
|
||
test_table!( | ||
push_record, | ||
{ | ||
#[derive(Tabled)] | ||
struct Company<'a> { | ||
name: &'a str, | ||
street: &'a str, | ||
city: &'a str, | ||
zip_code: &'a str, | ||
} | ||
|
||
|
||
let companies = vec![ | ||
Company { name: "INTEL CORP", city: "SANTA CLARA", street: "2200 MISSION COLLEGE BLVD, RNB-4-151", zip_code: "95054" }, | ||
Company { name: "Apple Inc.", city: "CUPERTINO", street: "ONE APPLE PARK WAY", zip_code: "95014" }, | ||
]; | ||
|
||
let mut table = Table::kv(&companies); | ||
|
||
for row in LayoutIterator::kv_batches::<Company>(&table) { | ||
table.modify((row, 1), Span::column(-1)); | ||
} | ||
|
||
table.with(Style::modern()); | ||
table.with(BorderSpanCorrection); | ||
|
||
table | ||
}, | ||
"┌─────────────────────────────────────────────────┐" | ||
"│ INTEL CORP │" | ||
"├──────────┬──────────────────────────────────────┤" | ||
"│ street │ 2200 MISSION COLLEGE BLVD, RNB-4-151 │" | ||
"├──────────┼──────────────────────────────────────┤" | ||
"│ city │ SANTA CLARA │" | ||
"├──────────┼──────────────────────────────────────┤" | ||
"│ zip_code │ 95054 │" | ||
"├──────────┴──────────────────────────────────────┤" | ||
"│ Apple Inc. │" | ||
"├──────────┬──────────────────────────────────────┤" | ||
"│ street │ ONE APPLE PARK WAY │" | ||
"├──────────┼──────────────────────────────────────┤" | ||
"│ city │ CUPERTINO │" | ||
"├──────────┼──────────────────────────────────────┤" | ||
"│ zip_code │ 95014 │" | ||
"└──────────┴──────────────────────────────────────┘" | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters