text_table 0.0.4

A library to create formatted plain-text tables from arbitrary data.
use Alignment;

#[derive(Clone, PartialEq, Debug)]
pub struct Cell {
    string: String,
    alignment: Alignment
}

pub trait ToCell {
    fn to_cell(self) -> Cell;
}

impl Cell {
    pub fn new<T: ToCell>(cell: T) -> Cell {
        cell.to_cell()
    }

    pub fn with_string(string: String) -> Cell {
        Cell {
            string: string,
            alignment: Alignment::Left
        }
    }

    pub fn alignment(&self) -> &Alignment {
        &self.alignment
    }

    pub fn string(&self) -> &String {
        &self.string
    }

    pub fn left(mut self) -> Cell {
        self.alignment = Alignment::Left;
        self
    }

    pub fn center(mut self) -> Cell {
        self.alignment = Alignment::Center;
        self
    }

    pub fn right(mut self) -> Cell {
        self.alignment = Alignment::Right;
        self
    }
}

impl ToCell for Cell {
    fn to_cell(self) -> Cell {
        self
    }
}

impl<'a> ToCell for () {
    fn to_cell(self) -> Cell {
        Cell::new("")
    }
}

impl<'a> ToCell for &'a str {
    fn to_cell(self) -> Cell {
        Cell::new(self.to_string())
    }
}

impl ToCell for String {
    fn to_cell(self) -> Cell {
        Cell::with_string(self)
    }
}

macro_rules! impl_to_cell {
    ($($ty:ty)+) => {
        $(
            impl ToCell for $ty {
                fn to_cell(self) -> Cell {
                    Cell::new(self.to_string())
                }
            }
        )+
    }
}

impl_to_cell!(u8 u16 u32 u64 usize
              i8 i16 i32 i64 isize
              f32 f64);