| 1 | //! Module contains [`VecRecords`]. |
| 2 | |
| 3 | mod cell; |
| 4 | mod cell_info; |
| 5 | |
| 6 | use crate::{ |
| 7 | config::Position, |
| 8 | records::{ExactRecords, IntoRecords, Records}, |
| 9 | }; |
| 10 | use std::ops::{Deref, DerefMut}; |
| 11 | |
| 12 | use super::PeekableRecords; |
| 13 | |
| 14 | pub use cell::Cell; |
| 15 | pub use cell_info::{CellInfo, StrWithWidth}; |
| 16 | |
| 17 | /// A [Records] implementation based on allocated buffers. |
| 18 | #[derive (Debug, Clone, Default, PartialEq, Eq, PartialOrd, Ord)] |
| 19 | pub struct VecRecords<T> { |
| 20 | data: Vec<Vec<T>>, |
| 21 | shape: (usize, usize), |
| 22 | } |
| 23 | |
| 24 | impl<T> VecRecords<T> { |
| 25 | /// Creates new [`VecRecords`] structure. |
| 26 | /// |
| 27 | /// It assumes that data vector has all rows has the same length(). |
| 28 | pub fn new(data: Vec<Vec<T>>) -> Self { |
| 29 | let count_columns: usize = data.get(0).map_or(default:0, |row: &Vec| row.len()); |
| 30 | let count_rows: usize = data.len(); |
| 31 | let shape: (usize, usize) = (count_rows, count_columns); |
| 32 | |
| 33 | Self { data, shape } |
| 34 | } |
| 35 | } |
| 36 | |
| 37 | impl<T> Records for VecRecords<T> { |
| 38 | type Iter = Vec<Vec<T>>; |
| 39 | |
| 40 | fn iter_rows(self) -> <Self::Iter as IntoRecords>::IterRows { |
| 41 | self.data.iter_rows() |
| 42 | } |
| 43 | |
| 44 | fn count_columns(&self) -> usize { |
| 45 | self.shape.1 |
| 46 | } |
| 47 | |
| 48 | fn hint_count_rows(&self) -> Option<usize> { |
| 49 | Some(self.shape.0) |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | impl<'a, T> Records for &'a VecRecords<T> { |
| 54 | type Iter = &'a [Vec<T>]; |
| 55 | |
| 56 | fn iter_rows(self) -> <Self::Iter as IntoRecords>::IterRows { |
| 57 | (&self.data).iter_rows() |
| 58 | } |
| 59 | |
| 60 | fn count_columns(&self) -> usize { |
| 61 | self.shape.1 |
| 62 | } |
| 63 | |
| 64 | fn hint_count_rows(&self) -> Option<usize> { |
| 65 | Some(self.shape.0) |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | impl<T> ExactRecords for VecRecords<T> { |
| 70 | fn count_rows(&self) -> usize { |
| 71 | self.shape.0 |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | impl<T> PeekableRecords for VecRecords<T> |
| 76 | where |
| 77 | T: Cell, |
| 78 | { |
| 79 | fn get_text(&self, (row: usize, col: usize): Position) -> &str { |
| 80 | self[row][col].text() |
| 81 | } |
| 82 | |
| 83 | fn count_lines(&self, (row: usize, col: usize): Position) -> usize { |
| 84 | self[row][col].count_lines() |
| 85 | } |
| 86 | |
| 87 | fn get_line(&self, (row: usize, col: usize): Position, line: usize) -> &str { |
| 88 | self[row][col].line(line) |
| 89 | } |
| 90 | |
| 91 | fn get_line_width(&self, (row: usize, col: usize): Position, line: usize) -> usize { |
| 92 | self[row][col].line_width(line) |
| 93 | } |
| 94 | |
| 95 | fn get_width(&self, (row: usize, col: usize): Position) -> usize { |
| 96 | self[row][col].width() |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | impl<T> Deref for VecRecords<T> { |
| 101 | type Target = Vec<Vec<T>>; |
| 102 | |
| 103 | fn deref(&self) -> &Self::Target { |
| 104 | &self.data |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | impl<T> DerefMut for VecRecords<T> { |
| 109 | fn deref_mut(&mut self) -> &mut Self::Target { |
| 110 | &mut self.data |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | impl<T> From<VecRecords<T>> for Vec<Vec<T>> { |
| 115 | fn from(records: VecRecords<T>) -> Self { |
| 116 | records.data |
| 117 | } |
| 118 | } |
| 119 | |