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
use crate::column::ColumnValue;
use std::ops::Index;
use std::collections::HashMap;
use crate::*;

#[derive(Debug, Clone)]
pub struct Row {
    vals: HashMap<String, ColumnValue>,
    table: Box<Table>,
    id: String,
}


impl Row {
    pub fn new(tab: &Table, from: HashMap<String, ColumnValue>, id: String) -> Self {
        Self {
            table: Box::new(tab.clone()),
            vals: from,
            id: id,
        }
    }

    /// Set a key to a value on a mutable row. The row's internal value
    /// cache is updated so the row **must** be mutable. Abstracts
    /// over table.set_cell().
    pub fn set<T: ToCell + Clone>(&mut self, k: &str, val: T) {
        let key = k.to_string();

        let old = self.vals.get(k).unwrap().clone();

        self.table.set_cell(self.id.to_owned(), key.clone(), val.clone(), old);
        self.vals.remove(k);
        self.vals.insert(key, val.to_cell());
    }
}

impl Index<&str> for Row {
    type Output = ColumnValue;

    fn index(&self, key: &str) -> &ColumnValue {
        &self.vals.get(key).unwrap()
    }
}