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
use std::{borrow::Cow, collections::HashMap};

use serde::Serialize;

use crate::{
    cell::{Cell, Cells, ColIndex, Number},
    context::Context,
};

#[derive(Default, Serialize)]
pub(crate) struct Rows {
    pub(crate) next_row_index: RowIndex,
    pub(crate) rows: HashMap<RowIndex, Row>,
}

#[derive(Default, Eq, PartialEq, Hash, Copy, Clone, Serialize)]
pub struct RowIndex(pub usize);

#[derive(Serialize)]
pub struct Row {
    pub(crate) row_index: RowIndex,
    pub(crate) cells: Cells,
}

impl Rows {
    pub(crate) fn add_row(&mut self) -> &mut Row {
        self.get_row(self.next_row_index)
    }

    pub(crate) fn get_row(&mut self, row_index: RowIndex) -> &mut Row {
        if row_index.0 >= self.next_row_index.0 {
            self.next_row_index = RowIndex(row_index.0 + 1);
        }

        self.rows.entry(row_index).or_insert_with(|| Row {
            row_index,
            cells: Cells::default(),
        })
    }
}

impl Row {
    pub fn add_str_cell<V: Into<Cow<'static, str>>>(
        &mut self,
        context: &mut Context,
        cell_value: V,
    ) -> &mut Cell {
        self.cells.add_str_cell(context, cell_value.into())
    }

    pub fn set_str_cell<V: Into<Cow<'static, str>>>(
        &mut self,
        context: &mut Context,
        col_index: ColIndex,
        cell_value: V,
    ) -> &mut Cell {
        self.cells
            .set_str_cell(context, col_index, cell_value.into())
    }

    pub fn add_num_cell(&mut self, context: &mut Context, cell_value: Number) -> &mut Cell {
        self.cells.add_num_cell(context, cell_value)
    }

    pub fn set_num_cell(
        &mut self,
        context: &mut Context,
        col_index: ColIndex,
        cell_value: Number,
    ) -> &mut Cell {
        self.cells.set_num_cell(context, col_index, cell_value)
    }
}