1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
use std::any::{Any, TypeId};
use std::collections::HashMap;
use std::ops::Index;

use crate::{DataFrame, Header, ColumnType};

/// An opaque container for arbitrary types plucked from different [`Column`]s
///
/// `Row`s only exist derivatively from a [`DataFrame`] that holds them. They hold boxed
/// references to the values owned by [`Column`]s.
///
/// Because `Row`s exist ephemerally, the number of cells and their types can't be known at compile
/// time. The values are stored as [`Any`]s and must be downcast into the appropriate types to
/// access the interior values. `Row` allows you to access values by specifying the column name or
/// offset and supplying a type parameter which is forwarded to [`downcast_ref`] for you.
///
/// ```
/// use tubular::DataFrame;
///
/// let mut df = DataFrame::default();
/// df.push("Fruits", &["apple", "pear", "mango"]);
/// df.push("Quantity", &[16, 25, 3]);
/// df.push("Organic", &[true, true, false]);
///
/// for row in df.rows() {
///     let organic = *row.column_name::<bool>("Organic");
///     let label = if organic { "Organic" } else { "" };
///     println!("{} Fruit {} - {} available",
///         label,
///         row.column_name::<String>("Fruits"),
///         row.column_name::<i32>("Quantity")
///     );
/// }
/// ```
///
/// [`Column`]: enum.Column.html
#[derive(Default)]
pub struct Row<'d> {
    headers: Vec<&'d Header>,
    cells: HashMap<&'d Header, Box<dyn Any>>,
}

impl<'d> Row<'d> {
    /// Number of cells in this `Row`
    pub fn len(&self) -> usize {
        self.cells.len()
    }

    /// Returns the cell value by number and downcasts to `T` simultaneously.
    ///
    /// # Panics
    ///
    /// This method panics if `T` does not match the type of the value at the provided index.
    pub fn column_index<T>(&'d self, index: usize) -> &'d T
        where T: 'static + ColumnType {
        self[index].downcast_ref::<T>().unwrap()
    }

    /// Returns the cell value by column name and downcasts to `T` simultaneously.
    ///
    /// # Panics
    ///
    /// This method panics if `T` does not match the type of the value at the provided name.
    pub fn column_name<T>(&'d self, index: &'static str) -> &'d T
        where T: 'static + ColumnType {
        self[index].downcast_ref::<T>().unwrap()
    }
}

impl<'d> std::fmt::Debug for Row<'d> {
    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
        let mut cell_displays = vec![];
        for i in 0..self.len() {
            let any_cell = &self[i];
            let type_id = any_cell.type_id();
            cell_displays.push(if type_id == TypeId::of::<String>() {
                any_cell.downcast_ref::<String>().unwrap().clone()
            } else if type_id == TypeId::of::<bool>() {
                format!("{}", any_cell.downcast_ref::<bool>().unwrap())
            } else if type_id == TypeId::of::<u32>() {
                format!("{}", any_cell.downcast_ref::<u32>().unwrap())
            } else {
                "?".into()
            })
        }
        write!(f, "Row[{}]", cell_displays.join(", "))
    }
}

impl<'d> Index<&'static str> for Row<'d> {
    type Output = Box<dyn Any>;

    fn index(&self, index: &'static str) -> &Self::Output {
        &self.cells[&index.to_string()]
    }
}

impl<'d> Index<String> for Row<'d> {
    type Output = Box<dyn Any>;

    fn index(&self, index: String) -> &Self::Output {
        &self.cells[&index]
    }
}

impl<'d> Index<usize> for Row<'d> {
    type Output = Box<dyn Any>;

    fn index(&self, index: usize) -> &Self::Output {
        &self.cells[&self.headers[index]]
    }
}

/// Iterator over [`Row`](struct.Row.html) made by calling [`rows()`](struct.DataFrame.html#method.rows)
#[derive(Debug, PartialEq)]
pub struct Rows<'d> {
    index: usize,
    df: &'d DataFrame,
}

impl<'d> From<&'d DataFrame> for Rows<'d> {
    fn from(df: &'d DataFrame) -> Rows<'d> {
        Rows {
            index: 0,
            df,
        }
    }
}

impl<'d> Iterator for Rows<'d> {
    type Item = Row<'d>;

    fn next(&mut self) -> Option<Row<'d>> {
        if self.index >= self.df.row_len() {
            return None;
        }

        let index = self.index;
        self.index += 1;

        let mut row = Row::default();
        for (col_index, column) in self.df.into_iter().enumerate() {
            let header: &'d Header = &self.df.headers()[col_index];
            row.cells.insert(header, column.to_any(index));
            row.headers.push(header);
        }

        Some(row)
    }
}