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 | where |
39 | T: AsRef<str>, |
40 | { |
41 | type Iter = Vec<Vec<T>>; |
42 | |
43 | fn iter_rows(self) -> <Self::Iter as IntoRecords>::IterRows { |
44 | self.data.iter_rows() |
45 | } |
46 | |
47 | fn count_columns(&self) -> usize { |
48 | self.shape.1 |
49 | } |
50 | |
51 | fn hint_count_rows(&self) -> Option<usize> { |
52 | Some(self.shape.0) |
53 | } |
54 | } |
55 | |
56 | impl<'a, T> Records for &'a VecRecords<T> |
57 | where |
58 | T: AsRef<str>, |
59 | { |
60 | type Iter = &'a [Vec<T>]; |
61 | |
62 | fn iter_rows(self) -> <Self::Iter as IntoRecords>::IterRows { |
63 | (&self.data).iter_rows() |
64 | } |
65 | |
66 | fn count_columns(&self) -> usize { |
67 | self.shape.1 |
68 | } |
69 | |
70 | fn hint_count_rows(&self) -> Option<usize> { |
71 | Some(self.shape.0) |
72 | } |
73 | } |
74 | |
75 | impl<T> ExactRecords for VecRecords<T> { |
76 | fn count_rows(&self) -> usize { |
77 | self.shape.0 |
78 | } |
79 | } |
80 | |
81 | impl<T> PeekableRecords for VecRecords<T> |
82 | where |
83 | T: Cell, |
84 | { |
85 | fn get_text(&self, (row: usize, col: usize): Position) -> &str { |
86 | self[row][col].text() |
87 | } |
88 | |
89 | fn count_lines(&self, (row: usize, col: usize): Position) -> usize { |
90 | self[row][col].count_lines() |
91 | } |
92 | |
93 | fn get_line(&self, (row: usize, col: usize): Position, line: usize) -> &str { |
94 | self[row][col].line(line) |
95 | } |
96 | |
97 | fn get_line_width(&self, (row: usize, col: usize): Position, line: usize) -> usize { |
98 | self[row][col].line_width(line) |
99 | } |
100 | |
101 | fn get_width(&self, (row: usize, col: usize): Position) -> usize { |
102 | self[row][col].width() |
103 | } |
104 | } |
105 | |
106 | impl<T> Deref for VecRecords<T> { |
107 | type Target = Vec<Vec<T>>; |
108 | |
109 | fn deref(&self) -> &Self::Target { |
110 | &self.data |
111 | } |
112 | } |
113 | |
114 | impl<T> DerefMut for VecRecords<T> { |
115 | fn deref_mut(&mut self) -> &mut Self::Target { |
116 | &mut self.data |
117 | } |
118 | } |
119 | |
120 | impl<T> From<VecRecords<T>> for Vec<Vec<T>> { |
121 | fn from(records: VecRecords<T>) -> Self { |
122 | records.data |
123 | } |
124 | } |
125 | |