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};
#[derive(Default)]
pub struct Row<'d> {
headers: Vec<&'d Header>,
cells: HashMap<&'d Header, Box<dyn Any>>,
}
impl<'d> Row<'d> {
pub fn len(&self) -> usize {
self.cells.len()
}
pub fn column_index<T>(&'d self, index: usize) -> &'d T
where T: 'static + ColumnType {
self[index].downcast_ref::<T>().unwrap()
}
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]]
}
}
#[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)
}
}