diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-06-07 05:48:48 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-06-07 05:48:48 +0000 |
commit | ef24de24a82fe681581cc130f342363c47c0969a (patch) | |
tree | 0d494f7e1a38b95c92426f58fe6eaa877303a86c /vendor/tabled/tests/matrix | |
parent | Releasing progress-linux version 1.74.1+dfsg1-1~progress7.99u1. (diff) | |
download | rustc-ef24de24a82fe681581cc130f342363c47c0969a.tar.xz rustc-ef24de24a82fe681581cc130f342363c47c0969a.zip |
Merging upstream version 1.75.0+dfsg1.
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'vendor/tabled/tests/matrix')
-rw-r--r-- | vendor/tabled/tests/matrix/matrix.rs | 161 | ||||
-rw-r--r-- | vendor/tabled/tests/matrix/matrix_list.rs | 58 | ||||
-rw-r--r-- | vendor/tabled/tests/matrix/mod.rs | 6 |
3 files changed, 225 insertions, 0 deletions
diff --git a/vendor/tabled/tests/matrix/matrix.rs b/vendor/tabled/tests/matrix/matrix.rs new file mode 100644 index 000000000..48ba55f9b --- /dev/null +++ b/vendor/tabled/tests/matrix/matrix.rs @@ -0,0 +1,161 @@ +use std::{ + fmt::{self, Display}, + iter::FromIterator, + string::ToString, +}; + +use tabled::{ + grid::config::ColoredConfig, + grid::dimension::CompleteDimensionVecRecords, + grid::records::vec_records::{CellInfo, VecRecords}, + settings::{object::Segment, Alignment, Modify, TableOption}, + Table, Tabled, +}; + +use super::matrix_list::MatrixList; + +/// A helper table factory. +/// +/// It uses center alignment by default, because it's more complex and may spot more issues. +#[derive(Debug, Clone)] +pub struct Matrix { + data: Vec<Vec<String>>, + size: (usize, usize), +} + +impl Matrix { + pub fn empty() -> Self { + Self { + data: vec![], + size: (0, 0), + } + } + + pub fn with_no_frame(rows: usize, columns: usize) -> Self { + Self { + data: create_matrix(rows, columns), + size: (rows, columns), + } + } + + pub fn new(rows: usize, columns: usize) -> Self { + Self::with_no_frame(rows, columns) + .with_header() + .with_index() + } + + pub fn vec(rows: usize, columns: usize) -> Vec<Vec<String>> { + Self::new(rows, columns).to_vec() + } + + pub fn table(rows: usize, columns: usize) -> Table { + Self::new(rows, columns).to_table() + } + + pub fn list<const ROWS: usize, const COLUMNS: usize>() -> Vec<MatrixList<COLUMNS, true>> { + create_list::<ROWS, COLUMNS>() + } + + pub fn iter<I, T>(iter: I) -> Table + where + I: IntoIterator<Item = T>, + T: Tabled, + { + let mut table = tabled::Table::new(iter); + table.with(Modify::new(Segment::all()).with(Alignment::center())); + table + } + + pub fn with_index(mut self) -> Self { + set_index(&mut self.data); + self + } + + pub fn with_header(mut self) -> Self { + set_header(&mut self.data, self.size.1); + self + } + + pub fn insert<V: ToString>(mut self, pos: tabled::grid::config::Position, value: V) -> Self { + self.data[pos.0][pos.1] = value.to_string(); + self + } + + pub fn to_table(&self) -> Table { + let mut table = tabled::Table::from_iter(self.data.clone()); + table.with(Modify::new(Segment::all()).with(Alignment::center())); + table + } + + pub fn to_vec(&self) -> Vec<Vec<String>> { + self.data.clone() + } + + pub fn with<O>(self, opt: O) -> Table + where + O: TableOption< + VecRecords<CellInfo<String>>, + CompleteDimensionVecRecords<'static>, + ColoredConfig, + >, + { + let mut table = self.to_table(); + table.with(opt); + table + } +} + +impl Display for Matrix { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + self.clone().to_table().fmt(f) + } +} + +fn create_matrix(rows: usize, columns: usize) -> Vec<Vec<String>> { + let mut arr = Vec::with_capacity(rows); + for row in 0..rows { + let mut data = Vec::with_capacity(columns); + for column in 0..columns { + let text = format!("{row}-{column}"); + data.push(text); + } + + arr.push(data); + } + + arr +} + +fn set_header(data: &mut Vec<Vec<String>>, columns: usize) { + data.insert( + 0, + (0..columns) + .map(|n| format!("column {n}")) + .collect::<Vec<_>>(), + ); +} + +fn set_index(data: &mut [Vec<String>]) { + if data.is_empty() { + return; + } + + data[0].insert(0, "N".to_owned()); + + for (n, row) in data.iter_mut().skip(1).enumerate() { + row.insert(0, n.to_string()); + } +} + +fn create_list<const ROWS: usize, const COLUMNS: usize>() -> Vec<MatrixList<COLUMNS, true>> { + let mut arr = Vec::with_capacity(ROWS); + for row in 0..ROWS { + let data = (0..COLUMNS) + .map(|column| format!("{row}-{column}")) + .collect::<Vec<_>>(); + let list = MatrixList::with_index(row, data); + arr.push(list); + } + + arr +} diff --git a/vendor/tabled/tests/matrix/matrix_list.rs b/vendor/tabled/tests/matrix/matrix_list.rs new file mode 100644 index 000000000..9f58400ca --- /dev/null +++ b/vendor/tabled/tests/matrix/matrix_list.rs @@ -0,0 +1,58 @@ +use std::{ + borrow::Cow, + iter::once, + ops::{Index, IndexMut}, +}; + +use tabled::Tabled; + +#[derive(Debug)] +pub struct MatrixList<const N: usize, const INDEX: bool> { + data: Vec<String>, +} + +impl<const N: usize> MatrixList<N, false> { + #[allow(dead_code)] + pub fn new(data: Vec<String>) -> Self { + Self { data } + } +} + +impl<const N: usize> MatrixList<N, true> { + pub fn with_index(index: usize, mut data: Vec<String>) -> Self { + assert_eq!(data.len(), N); + data.insert(0, index.to_string()); + Self { data } + } +} + +impl<const N: usize, const INDEX: bool> Index<usize> for MatrixList<N, INDEX> { + type Output = String; + + fn index(&self, index: usize) -> &Self::Output { + &self.data[index] + } +} + +impl<const N: usize, const INDEX: bool> IndexMut<usize> for MatrixList<N, INDEX> { + fn index_mut(&mut self, index: usize) -> &mut Self::Output { + &mut self.data[index] + } +} + +impl<const N: usize, const INDEX: bool> Tabled for MatrixList<N, INDEX> { + const LENGTH: usize = N + 1; + + fn fields(&self) -> Vec<Cow<'_, str>> { + self.data.iter().cloned().map(Cow::Owned).collect() + } + + fn headers() -> Vec<Cow<'static, str>> { + let header = (0..N).map(|n| format!("column {n}")); + + match INDEX { + true => once("N".to_owned()).chain(header).map(Cow::Owned).collect(), + false => header.map(Cow::Owned).collect(), + } + } +} diff --git a/vendor/tabled/tests/matrix/mod.rs b/vendor/tabled/tests/matrix/mod.rs new file mode 100644 index 000000000..d2d42cfc5 --- /dev/null +++ b/vendor/tabled/tests/matrix/mod.rs @@ -0,0 +1,6 @@ +#[allow(clippy::module_inception)] +mod matrix; +mod matrix_list; + +pub use matrix::Matrix; +pub use matrix_list::MatrixList; |