blackjack/row.rs
1//!
2//! Representation of a row in a `DataFrame` and related structs
3
4use std::ops::Index;
5
6use crate::prelude::*;
7
8/// Representation of a DataFrame row, in which each element
9/// can have a different type.
10pub struct Row<'a> {
11 /// Represents the elements in the `Row`
12 pub data: Vec<Element<'a>>,
13}
14
15impl<'a> Row<'a> {
16 /// Create an empty `Row`
17 pub fn new() -> Self {
18 Row { data: vec![] }
19 }
20
21 /// Push an `Element` into the `Row`
22 pub fn add(&mut self, data: Element<'a>) {
23 self.data.push(data)
24 }
25}
26
27/// Represent a single data element, the enum of the data itself, and the name
28/// for the column it belongs in.
29pub struct Element<'a> {
30 /// Enum containing a reference to the data within the dataframe.
31 pub data: Datum<'a>,
32
33 /// The name of the column, of which this Element belongs
34 pub name: String,
35}
36
37impl<'a> Element<'a> {
38 /// Create a new element, which represents an element of a `Row`
39 pub fn new(name: String, data: Datum<'a>) -> Self {
40 Element { name, data }
41 }
42}
43
44impl<'a, 'b> Index<&'b str> for Row<'a> {
45 type Output = Datum<'a>;
46 fn index(&self, name: &str) -> &Self::Output {
47 for element in &self.data {
48 if element.name == name {
49 return &element.data;
50 }
51 }
52 panic!("Element named: {} now found", name);
53 }
54}