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
use std::{borrow::Cow, collections::HashMap}; use serde::Serialize; use crate::{context::Context, shared_strings::SharedStringIndex}; #[derive(Default, Serialize)] pub(crate) struct Cells { pub(crate) next_col_index: ColIndex, pub(crate) cells: HashMap<ColIndex, Cell>, } #[derive(Default, Copy, Clone, Eq, PartialEq, Hash, Serialize)] pub struct ColIndex(pub usize); #[derive(Serialize)] pub struct Cell { pub(crate) col_index: ColIndex, pub(crate) cell_type: CellType, } #[derive(Serialize)] #[serde(tag = "t", content = "c")] pub(crate) enum CellType { String(SharedStringIndex), Number(Number), } #[derive(Serialize)] pub enum Number { I8(i8), U8(u8), I16(i16), U16(u16), I32(i32), U32(u32), I64(i64), U64(u64), Isize(isize), Usize(usize), I128(i128), U128(u128), F32(f32), F64(f64), } impl Cells { pub(crate) fn add_str_cell( &mut self, context: &mut Context, cell_value: Cow<'static, str>, ) -> &mut Cell { self.set_str_cell(context, self.next_col_index, cell_value) } pub(crate) fn set_str_cell( &mut self, context: &mut Context, col_index: ColIndex, cell_value: Cow<'static, str>, ) -> &mut Cell { let shared_string_index = context.add_shared_string(cell_value); self.set_cell( context, Cell { col_index, cell_type: CellType::String(shared_string_index), }, ) } pub(crate) fn add_num_cell(&mut self, context: &mut Context, cell_value: Number) -> &mut Cell { self.set_num_cell(context, self.next_col_index, cell_value) } pub(crate) fn set_num_cell( &mut self, context: &mut Context, col_index: ColIndex, cell_value: Number, ) -> &mut Cell { self.set_cell( context, Cell { col_index, cell_type: CellType::Number(cell_value), }, ) } pub(crate) fn set_cell(&mut self, context: &mut Context, cell: Cell) -> &mut Cell { let col_index = cell.col_index; if col_index.0 >= self.next_col_index.0 { self.next_col_index = ColIndex(col_index.0 + 1); } context.add_col_index(col_index); self.cells.insert(col_index, cell); self.cells.get_mut(&col_index).unwrap() } }