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