papergrid/records/vec_records/
mod.rs

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