summaryrefslogtreecommitdiffstats
path: root/vendor/tabled/src/settings/width/width_list.rs
blob: 7547b97f3fc15bf5f2a5a3a9e86c67d2c4c08103 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
use std::iter::FromIterator;

use crate::{
    grid::dimension::CompleteDimensionVecRecords, grid::records::Records, settings::TableOption,
};

/// A structure used to set [`Table`] width via a list of columns widths.
///
/// [`Table`]: crate::Table
#[derive(Debug)]
pub struct WidthList {
    list: Vec<usize>,
}

impl WidthList {
    /// Creates a new object.
    pub fn new(list: Vec<usize>) -> Self {
        Self { list }
    }
}

impl From<Vec<usize>> for WidthList {
    fn from(list: Vec<usize>) -> Self {
        Self::new(list)
    }
}

impl FromIterator<usize> for WidthList {
    fn from_iter<T: IntoIterator<Item = usize>>(iter: T) -> Self {
        Self::new(iter.into_iter().collect())
    }
}

impl<R, C> TableOption<R, CompleteDimensionVecRecords<'static>, C> for WidthList
where
    R: Records,
{
    fn change(
        self,
        records: &mut R,
        _: &mut C,
        dimension: &mut CompleteDimensionVecRecords<'static>,
    ) {
        if self.list.len() < records.count_columns() {
            return;
        }

        let _ = dimension.set_widths(self.list);
    }
}