| 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, C, CompleteDimensionVecRecords<'_>> for WidthList |
| 35 | where |
| 36 | R: Records, |
| 37 | { |
| 38 | fn change(self, records: &mut R, _: &mut C, dimension: &mut CompleteDimensionVecRecords<'_>) { |
| 39 | if self.list.len() < records.count_columns() { |
| 40 | return; |
| 41 | } |
| 42 | |
| 43 | dimension.set_widths(self.list); |
| 44 | } |
| 45 | } |
| 46 | |