use crate::scene::block::BlockNode;
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum ColumnSpec {
Fixed(f64),
Auto,
Fraction(f64),
}
pub struct TableCell<'a> {
pub content: &'a [BlockNode<'a>],
pub col_span: usize,
pub row_span: usize,
}
impl<'a> TableCell<'a> {
pub fn new(content: &'a [BlockNode<'a>]) -> Self {
Self { content, col_span: 1, row_span: 1 }
}
pub fn with_col_span(mut self, col_span: usize) -> Self {
self.col_span = col_span.max(1);
self
}
pub fn with_row_span(mut self, row_span: usize) -> Self {
self.row_span = row_span.max(1);
self
}
}
pub struct TableRow<'a> {
pub cells: &'a [TableCell<'a>],
}
impl<'a> TableRow<'a> {
pub fn new(cells: &'a [TableCell<'a>]) -> Self {
Self { cells }
}
}
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct CellPadding {
pub h: f64,
pub v: f64,
}
impl CellPadding {
pub const fn new(h: f64, v: f64) -> Self {
Self { h, v }
}
}
impl Default for CellPadding {
fn default() -> Self {
Self { h: 6.0, v: 4.0 }
}
}
pub struct TableBlock<'a> {
pub columns: &'a [ColumnSpec],
pub rows: &'a [TableRow<'a>],
pub cell_padding: CellPadding,
pub header_repeat: bool,
}
impl<'a> TableBlock<'a> {
pub fn new(columns: &'a [ColumnSpec], rows: &'a [TableRow<'a>]) -> Self {
Self { columns, rows, cell_padding: CellPadding::default(), header_repeat: false }
}
pub fn with_cell_padding(mut self, cell_padding: CellPadding) -> Self {
self.cell_padding = cell_padding;
self
}
pub fn with_header_repeat(mut self, header_repeat: bool) -> Self {
self.header_repeat = header_repeat;
self
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn table_block_carries_its_columns_and_rows_verbatim() {
let columns = [ColumnSpec::Fixed(40.0), ColumnSpec::Auto, ColumnSpec::Fraction(1.0)];
let cells: [TableCell<'_>; 0] = [];
let rows = [TableRow::new(&cells)];
let table = TableBlock::new(&columns, &rows);
assert_eq!(table.columns.len(), 3);
assert_eq!(table.rows.len(), 1);
}
}