rrpack_prime/visual/table/
state.rs

1use derive_more::{From, Into};
2use rill_protocol::flow::core::Flow;
3use rill_protocol::io::provider::StreamType;
4use rrpack_basis::manifest::description::{Layer, PackFlow};
5use serde::{Deserialize, Serialize};
6use std::collections::BTreeMap;
7use std::convert::{TryFrom, TryInto};
8use std::fmt;
9
10#[derive(Debug, Clone, Serialize, Deserialize)]
11pub struct TableSpec {
12    #[serde(with = "vectorize")]
13    pub columns: BTreeMap<Col, ColRecord>,
14}
15
16/// Id of a column in a table.
17#[derive(
18    Debug, Clone, Copy, Serialize, Deserialize, From, Into, PartialEq, Eq, PartialOrd, Ord, Hash,
19)]
20pub struct Col(pub u64);
21
22impl fmt::Display for Col {
23    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
24        self.0.fmt(f)
25    }
26}
27
28impl TryFrom<usize> for Col {
29    type Error = <u64 as TryFrom<usize>>::Error;
30
31    fn try_from(value: usize) -> Result<Self, Self::Error> {
32        value.try_into().map(Self)
33    }
34}
35
36/// Id of a row in a table.
37#[derive(
38    Debug, Clone, Copy, Serialize, Deserialize, From, Into, PartialEq, Eq, PartialOrd, Ord, Hash,
39)]
40pub struct Row(pub u64);
41
42impl fmt::Display for Row {
43    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
44        self.0.fmt(f)
45    }
46}
47
48impl TryFrom<usize> for Row {
49    type Error = <u64 as TryFrom<usize>>::Error;
50
51    fn try_from(value: usize) -> Result<Self, Self::Error> {
52        value.try_into().map(Self)
53    }
54}
55
56#[derive(Debug, Clone, Serialize, Deserialize)]
57pub struct TableState {
58    pub spec: TableSpec,
59    // MUTABLE:
60    #[serde(with = "vectorize")]
61    pub rows: BTreeMap<Row, RowRecord>,
62}
63
64impl From<TableSpec> for TableState {
65    fn from(spec: TableSpec) -> Self {
66        Self {
67            spec,
68            rows: BTreeMap::new(),
69        }
70    }
71}
72
73impl PackFlow for TableState {
74    fn layer() -> Layer {
75        Layer::Visual
76    }
77}
78
79impl Flow for TableState {
80    type Action = ();
81    type Event = TableEvent;
82
83    fn stream_type() -> StreamType {
84        StreamType::from(module_path!())
85    }
86
87    fn apply(&mut self, event: Self::Event) {
88        match event {
89            TableEvent::AddRow { row } => {
90                let record = RowRecord {
91                    cols: BTreeMap::new(),
92                };
93                self.rows.insert(row, record);
94            }
95            TableEvent::DelRow { row } => {
96                self.rows.remove(&row);
97            }
98            TableEvent::SetCell { row, col, value } => {
99                if let Some(record) = self.rows.get_mut(&row) {
100                    if self.spec.columns.contains_key(&col) {
101                        record.cols.insert(col, value);
102                    }
103                }
104            }
105        }
106    }
107}
108
109#[derive(Debug, Clone, Serialize, Deserialize)]
110pub enum TableEvent {
111    AddRow { row: Row },
112    DelRow { row: Row },
113    SetCell { row: Row, col: Col, value: String },
114}
115
116#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
117pub struct ColRecord {
118    pub title: String,
119}
120
121#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
122pub struct RowRecord {
123    #[serde(with = "vectorize")]
124    pub cols: BTreeMap<Col, String>,
125}