diff options
author | Pierre-Henri Symoneaux <pierre-henri.symoneaux@nokia.com> | 2018-02-19 22:06:44 +0100 |
---|---|---|
committer | Pierre-Henri Symoneaux <pierre-henri.symoneaux@nokia.com> | 2018-02-19 22:06:44 +0100 |
commit | ef0dea770fb3cde2444f69595cea9efb9101a910 (patch) | |
tree | f5cfcb53181370c471423dab57da2656638f2d17 | |
parent | f38be5c886e2462f074ce849eb9aa6815900a7e3 (diff) |
Added Hash & Eq derive. Implemented Extend on Row and Table
-rw-r--r-- | src/cell.rs | 2 | ||||
-rw-r--r-- | src/format.rs | 10 | ||||
-rw-r--r-- | src/lib.rs | 37 | ||||
-rw-r--r-- | src/row.rs | 33 |
4 files changed, 73 insertions, 9 deletions
diff --git a/src/cell.rs b/src/cell.rs index ef75433..9ab9c59 100644 --- a/src/cell.rs +++ b/src/cell.rs @@ -11,7 +11,7 @@ use super::utils::print_align; /// /// Once created, a cell's content cannot be modified. /// The cell would have to be replaced by another one -#[derive(Clone, Debug, Hash)] +#[derive(Clone, Debug, Hash, PartialEq, Eq)] pub struct Cell { content: Vec<String>, width: usize, diff --git a/src/format.rs b/src/format.rs index 4e7a671..25f07ea 100644 --- a/src/format.rs +++ b/src/format.rs @@ -7,7 +7,7 @@ use encode_unicode::Utf8Char; use super::utils::NEWLINE; /// Alignment for cell's content -#[derive(Clone, Debug, PartialEq, Copy, Hash)] +#[derive(Clone, Debug, PartialEq, Copy, Hash, Eq)] pub enum Alignment { /// Align left LEFT, @@ -18,7 +18,7 @@ pub enum Alignment { } /// Position of a line separator in a table -#[derive(Clone, Debug, PartialEq, Copy, Hash)] +#[derive(Clone, Debug, PartialEq, Copy, Hash, Eq)] pub enum LinePosition { /// Table's border on top Top, @@ -32,7 +32,7 @@ pub enum LinePosition { } /// Position of a column separator in a row -#[derive(Clone, Debug, PartialEq, Copy, Hash)] +#[derive(Clone, Debug, PartialEq, Copy, Hash, Eq)] pub enum ColumnPosition { /// Left table's border Left, @@ -43,7 +43,7 @@ pub enum ColumnPosition { } /// Contains the character used for printing a line separator -#[derive(Clone, Debug, Copy, Hash)] +#[derive(Clone, Debug, Copy, Hash, PartialEq, Eq)] pub struct LineSeparator { /// Line separator line: char, @@ -113,7 +113,7 @@ impl Default for LineSeparator { } /// Contains the table formatting rules -#[derive(Clone, Debug, Copy, Hash)] +#[derive(Clone, Debug, Copy, Hash, PartialEq, Eq)] pub struct TableFormat { /// Optional column separator character csep: Option<char>, @@ -36,7 +36,7 @@ use format::{TableFormat, LinePosition, consts}; use utils::StringWriter; /// An owned printable table -#[derive(Clone, Debug, Hash)] +#[derive(Clone, Debug, Hash, PartialEq, Eq)] pub struct Table { format: Box<TableFormat>, titles: Box<Option<Row>>, @@ -62,7 +62,7 @@ pub struct Table { /// # } /// ``` /// -#[derive(Clone, Debug, Hash)] +#[derive(Clone, Debug, Hash, PartialEq, Eq)] pub struct TableSlice<'a> { format: &'a TableFormat, titles: &'a Option<Row>, @@ -473,6 +473,14 @@ impl<B: ToString, A: IntoIterator<Item = B>> FromIterator<A> for Table { } } +impl FromIterator<Row> for Table { + fn from_iter<T>(iterator: T) -> Table + where T: IntoIterator<Item = Row> + { + Self::init(iterator.into_iter().collect()) + } +} + impl<T, A, B> From<T> for Table where B: ToString, A: IntoIterator<Item = B>, @@ -499,6 +507,20 @@ impl<'a> IntoIterator for &'a mut Table { } } +// impl IntoIterator for Table { +// type Item = Row; +// type IntoIter = std::vec::IntoIter<Self::Item>; +// fn into_iter(self) -> Self::IntoIter { +// self.rows.into_iter() +// } +// } + +impl <A: Into<Row>> Extend<A> for Table { + fn extend<T: IntoIterator<Item=A>>(&mut self, iter: T) { + self.rows.extend(iter.into_iter().map(|r| r.into())); + } +} + /// Iterator over immutable cells in a column pub struct ColumnIter<'a>(Iter<'a, Row>, usize); @@ -990,5 +1012,16 @@ mod tests { .replace("\r\n", "\n"), test_table().to_string().replace("\r\n", "\n")); } + + #[test] + fn extend_table() { + let mut table = Table::new(); + table.add_row(Row::new(vec![Cell::new("ABC"), Cell::new("DEFG"), Cell::new("HIJKLMN")])); + table.extend(vec![vec!["A", "B", "C"]]); + let t2 = table.clone(); + table.extend(t2.rows); + assert_eq!(table.get_row(1).unwrap().get_cell(2).unwrap().get_content(), "C"); + assert_eq!(table.get_row(2).unwrap().get_cell(1).unwrap().get_content(), "DEFG"); + } } } @@ -2,6 +2,7 @@ use std::io::{Write, Error}; use std::iter::FromIterator; use std::slice::{Iter, IterMut}; +// use std::vec::IntoIter; use std::ops::{Index, IndexMut}; use term::Terminal; @@ -11,7 +12,7 @@ use super::cell::Cell; use super::format::{TableFormat, ColumnPosition}; /// Represent a table row made of cells -#[derive(Clone, Debug, Hash)] +#[derive(Clone, Debug, Hash, PartialEq, Eq)] pub struct Row { cells: Vec<Cell>, } @@ -207,6 +208,14 @@ impl<'a> IntoIterator for &'a Row { } } +// impl IntoIterator for Row { +// type Item = Cell; +// type IntoIter = IntoIter<Cell>; +// fn into_iter(self) -> Self::IntoIter { +// self.cells.into_iter() +// } +// } + impl<'a> IntoIterator for &'a mut Row { type Item = &'a mut Cell; type IntoIter = IterMut<'a, Cell>; @@ -215,6 +224,18 @@ impl<'a> IntoIterator for &'a mut Row { } } +impl <S: ToString> Extend<S> for Row { + fn extend<T: IntoIterator<Item=S>>(&mut self, iter: T) { + self.cells.extend(iter.into_iter().map(|s| Cell::new(&s.to_string()))); + } +} + +// impl <S: Into<Cell>> Extend<S> for Row { +// fn extend<T: IntoIterator<Item=S>>(&mut self, iter: T) { +// self.cells.extend(iter.into_iter().map(|s| s.into())); +// } +// } + /// This macro simplifies `Row` creation /// /// The syntax support style spec @@ -305,4 +326,14 @@ mod tests { assert_eq!(row.get_cell(0).unwrap().get_content(), "foo"); assert_eq!(row.get_cell(1).unwrap().get_content(), "foobar"); } + + #[test] + fn extend_row() { + let mut row = Row::from(vec!["foo", "bar", "foobar"]); + row.extend(vec!["A", "B", "C"]); + assert_eq!(row.len(), 6); + assert_eq!(row.get_cell(3).unwrap().get_content(), "A"); + assert_eq!(row.get_cell(4).unwrap().get_content(), "B"); + assert_eq!(row.get_cell(5).unwrap().get_content(), "C"); + } } |