1 | use std::iter::FromIterator; |
2 | |
3 | use crate::{ |
4 | grid::dimension::CompleteDimensionVecRecords, grid::records::Records, settings::TableOption, |
5 | }; |
6 | |
7 | /// A structure used to set [`Table`] width via a list of columns widths. |
8 | /// |
9 | /// [`Table`]: crate::Table |
10 | #[derive (Debug)] |
11 | pub struct WidthList { |
12 | list: Vec<usize>, |
13 | } |
14 | |
15 | impl WidthList { |
16 | /// Creates a new object. |
17 | pub fn new(list: Vec<usize>) -> Self { |
18 | Self { list } |
19 | } |
20 | } |
21 | |
22 | impl From<Vec<usize>> for WidthList { |
23 | fn from(list: Vec<usize>) -> Self { |
24 | Self::new(list) |
25 | } |
26 | } |
27 | |
28 | impl FromIterator<usize> for WidthList { |
29 | fn from_iter<T: IntoIterator<Item = usize>>(iter: T) -> Self { |
30 | Self::new(list:iter.into_iter().collect()) |
31 | } |
32 | } |
33 | |
34 | impl<R, C> TableOption<R, CompleteDimensionVecRecords<'static>, C> for WidthList |
35 | where |
36 | R: Records, |
37 | { |
38 | fn change( |
39 | self, |
40 | records: &mut R, |
41 | _: &mut C, |
42 | dimension: &mut CompleteDimensionVecRecords<'static>, |
43 | ) { |
44 | if self.list.len() < records.count_columns() { |
45 | return; |
46 | } |
47 | |
48 | let _ = dimension.set_widths(self.list); |
49 | } |
50 | } |
51 | |