tabled/grid/dimension/
static_dimension.rs

1use crate::grid::dimension::{Dimension, Estimate};
2
3/// A constant dimension.
4#[derive(Debug, Clone)]
5pub struct StaticDimension {
6    width: DimensionValue,
7    height: DimensionValue,
8}
9
10impl StaticDimension {
11    /// Creates a constant dimension.
12    pub fn new(width: DimensionValue, height: DimensionValue) -> Self {
13        Self { width, height }
14    }
15}
16
17impl From<StaticDimension> for (DimensionValue, DimensionValue) {
18    fn from(value: StaticDimension) -> Self {
19        (value.width, value.height)
20    }
21}
22
23impl Dimension for StaticDimension {
24    fn get_width(&self, column: usize) -> usize {
25        self.width.get(column)
26    }
27
28    fn get_height(&self, row: usize) -> usize {
29        self.height.get(row)
30    }
31}
32
33impl<R, C> Estimate<R, C> for StaticDimension {
34    fn estimate(&mut self, _: R, _: &C) {}
35}
36
37/// A dimension value.
38#[derive(Debug, Clone)]
39pub enum DimensionValue {
40    /// Const width value.
41    Exact(usize),
42    /// A list of width values for columns.
43    List(Vec<usize>),
44    /// A list of width values for columns and a value for the rest.
45    Partial(Vec<usize>, usize),
46}
47
48impl DimensionValue {
49    /// Get a width by column.
50    pub fn get(&self, col: usize) -> usize {
51        match self {
52            DimensionValue::Exact(val) => *val,
53            DimensionValue::List(cols) => cols[col],
54            DimensionValue::Partial(cols, val) => {
55                if cols.len() > col {
56                    cols[col]
57                } else {
58                    *val
59                }
60            }
61        }
62    }
63}