From f38be5c886e2462f074ce849eb9aa6815900a7e3 Mon Sep 17 00:00:00 2001 From: Pierre-Henri Symoneaux Date: Mon, 19 Feb 2018 13:22:06 +0100 Subject: Derive hash on public types --- .travis.yml | 1 + src/cell.rs | 2 +- src/format.rs | 10 +++++----- src/lib.rs | 4 ++-- src/row.rs | 2 +- 5 files changed, 10 insertions(+), 9 deletions(-) diff --git a/.travis.yml b/.travis.yml index 098b3ca..3266656 100644 --- a/.travis.yml +++ b/.travis.yml @@ -10,6 +10,7 @@ rust: - 1.20.0 - 1.21.0 - 1.22.1 +- 1.23.0 - stable - beta - nightly diff --git a/src/cell.rs b/src/cell.rs index ec9deb7..ef75433 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)] +#[derive(Clone, Debug, Hash)] pub struct Cell { content: Vec, width: usize, diff --git a/src/format.rs b/src/format.rs index 5944f83..4e7a671 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)] +#[derive(Clone, Debug, PartialEq, Copy, Hash)] 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)] +#[derive(Clone, Debug, PartialEq, Copy, Hash)] 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)] +#[derive(Clone, Debug, PartialEq, Copy, Hash)] 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)] +#[derive(Clone, Debug, Copy, Hash)] pub struct LineSeparator { /// Line separator line: char, @@ -113,7 +113,7 @@ impl Default for LineSeparator { } /// Contains the table formatting rules -#[derive(Clone, Debug, Copy)] +#[derive(Clone, Debug, Copy, Hash)] pub struct TableFormat { /// Optional column separator character csep: Option, diff --git a/src/lib.rs b/src/lib.rs index ae90adb..d6c22ce 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -36,7 +36,7 @@ use format::{TableFormat, LinePosition, consts}; use utils::StringWriter; /// An owned printable table -#[derive(Clone, Debug)] +#[derive(Clone, Debug, Hash)] pub struct Table { format: Box, titles: Box>, @@ -62,7 +62,7 @@ pub struct Table { /// # } /// ``` /// -#[derive(Clone, Debug)] +#[derive(Clone, Debug, Hash)] pub struct TableSlice<'a> { format: &'a TableFormat, titles: &'a Option, diff --git a/src/row.rs b/src/row.rs index 86b7d68..28bed2a 100644 --- a/src/row.rs +++ b/src/row.rs @@ -11,7 +11,7 @@ use super::cell::Cell; use super::format::{TableFormat, ColumnPosition}; /// Represent a table row made of cells -#[derive(Clone, Debug)] +#[derive(Clone, Debug, Hash)] pub struct Row { cells: Vec, } -- cgit v1.2.3 From ef0dea770fb3cde2444f69595cea9efb9101a910 Mon Sep 17 00:00:00 2001 From: Pierre-Henri Symoneaux Date: Mon, 19 Feb 2018 22:06:44 +0100 Subject: Added Hash & Eq derive. Implemented Extend on Row and Table --- src/cell.rs | 2 +- src/format.rs | 10 +++++----- src/lib.rs | 37 +++++++++++++++++++++++++++++++++++-- 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, 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, diff --git a/src/lib.rs b/src/lib.rs index d6c22ce..bd08d43 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -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, titles: Box>, @@ -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, @@ -473,6 +473,14 @@ impl> FromIterator for Table { } } +impl FromIterator for Table { + fn from_iter(iterator: T) -> Table + where T: IntoIterator + { + Self::init(iterator.into_iter().collect()) + } +} + impl From for Table where B: ToString, A: IntoIterator, @@ -499,6 +507,20 @@ impl<'a> IntoIterator for &'a mut Table { } } +// impl IntoIterator for Table { +// type Item = Row; +// type IntoIter = std::vec::IntoIter; +// fn into_iter(self) -> Self::IntoIter { +// self.rows.into_iter() +// } +// } + +impl > Extend for Table { + fn extend>(&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"); + } } } diff --git a/src/row.rs b/src/row.rs index 28bed2a..f7e49f0 100644 --- a/src/row.rs +++ b/src/row.rs @@ -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, } @@ -207,6 +208,14 @@ impl<'a> IntoIterator for &'a Row { } } +// impl IntoIterator for Row { +// type Item = Cell; +// type IntoIter = IntoIter; +// 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 Extend for Row { + fn extend>(&mut self, iter: T) { + self.cells.extend(iter.into_iter().map(|s| Cell::new(&s.to_string()))); + } +} + +// impl > Extend for Row { +// fn extend>(&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"); + } } -- cgit v1.2.3