text_table 0.0.4

A library to create formatted plain-text tables from arbitrary data.
use {Cell, ToCell};

#[derive(Clone, PartialEq, Debug)]
pub struct Row {
    cells: Vec<Cell>
}

pub trait ToRow {
    fn to_row(self) -> Row;
}

impl Row {
    pub fn new() -> Row {
        Row {
            cells: Vec::new()
        }
    }

    pub fn with_cells(cells: &[Cell]) -> Row {
        Row {
            cells: cells.to_vec()
        }
    }

    pub fn cells(&self) -> &Vec<Cell> {
        &self.cells
    }
}

impl<'a, T: Clone + ToCell> ToRow for &'a [T] {
    fn to_row(self) -> Row {
        let cells = self.iter().map(|cell| cell.clone().to_cell()).collect::<Vec<_>>();
        Row::with_cells(&cells)
    }
}

macro_rules! impl_to_row {
    () => {};
    ($ident:ident: $ty:ident, $($ident_rest:ident: $ty_rest:ident,)*) => {
        impl<$ty: ToCell, $($ty_rest: ToCell,)*> ToRow for ($ty, $($ty_rest,)*) {
            fn to_row(self) -> Row {
                let ($ident, $($ident_rest,)*) = self;
                Row::with_cells(&[$ident.to_cell(), $($ident_rest.to_cell()),*])
            }
        }
        impl_to_row!($($ident_rest: $ty_rest,)*);
    }
}

impl_to_row!(a: A, b: B, c: C, d: D, e: E, f: F,);