summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorPierre-Henri Symoneaux <phsym@users.noreply.github.com>2018-03-02 10:39:49 +0100
committerGitHub <noreply@github.com>2018-03-02 10:39:49 +0100
commit5dacda1f2854fd8c1cfd2b09a76d704a6839036f (patch)
tree0166a9da5531e45d858fd99f2ee2bc53fde8739f
parentfd626c1d144d666a4f1e5c630fe9fdd1992d763e (diff)
parentef0dea770fb3cde2444f69595cea9efb9101a910 (diff)
Merge pull request #74 from phsym/73_derive_hash
#[derive(Hash, Eq)] and impl Extend
-rw-r--r--.travis.yml1
-rw-r--r--src/cell.rs2
-rw-r--r--src/format.rs10
-rw-r--r--src/lib.rs37
-rw-r--r--src/row.rs33
5 files changed, 74 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..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)]
+#[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 5944f83..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)]
+#[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)]
+#[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)]
+#[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)]
+#[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)]
+#[derive(Clone, Debug, Copy, Hash, PartialEq, Eq)]
pub struct TableFormat {
/// Optional column separator character
csep: Option<char>,
diff --git a/src/lib.rs b/src/lib.rs
index ae90adb..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)]
+#[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)]
+#[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");
+ }
}
}
diff --git a/src/row.rs b/src/row.rs
index 86b7d68..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)]
+#[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");
+ }
}