use crate::{Length, Sides};
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct GridStyle {
columns: Vec<Option<Length>>,
cell_padding: Sides,
}
impl GridStyle {
pub fn new() -> Self {
Self::default()
}
pub fn columns(mut self, columns: impl IntoIterator<Item = Option<Length>>) -> Self {
self.columns = columns.into_iter().collect();
self
}
pub fn reset_columns(mut self) -> Self {
self.columns.clear();
self
}
pub fn cell_padding(mut self, sides: impl Into<Sides>) -> Self {
self.cell_padding = sides.into();
self
}
pub fn reset_cell_padding(mut self) -> Self {
self.cell_padding = Sides::default();
self
}
pub fn get_column(&self, index: usize) -> Option<Length> {
self.columns.get(index).copied().flatten()
}
pub const fn get_cell_padding(&self) -> Sides {
self.cell_padding
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn a_column_past_the_stated_list_is_auto() {
let style = GridStyle::new().columns([Some(Length::Cells(4)), None]);
assert_eq!(style.get_column(0), Some(Length::Cells(4)));
assert_eq!(style.get_column(1), None, "stated as absent");
assert_eq!(style.get_column(9), None, "past the end");
}
#[test]
fn builders_can_restore_grid_defaults_explicitly() {
let stated = GridStyle::new()
.columns([Some(Length::Cells(4))])
.cell_padding((0, 1));
assert_eq!(
stated.clone().reset_columns().reset_cell_padding(),
GridStyle::new()
);
assert_ne!(stated, GridStyle::new(), "the removals did the work");
}
#[test]
fn cell_padding_accepts_the_same_shorthands_a_block_takes() {
assert_eq!(
GridStyle::new().cell_padding((0, 1)).get_cell_padding(),
Sides {
top: 0,
right: 1,
bottom: 0,
left: 1,
}
);
}
}