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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
use crate::manifest::description::{Layer, PackFlow};
use derive_more::{From, Into};
use rill_protocol::flow::core::Flow;
use rill_protocol::io::provider::StreamType;
use serde::{Deserialize, Serialize};
use std::collections::BTreeMap;
use std::convert::{TryFrom, TryInto};
use std::fmt;
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TableSpec {
#[serde(with = "vectorize")]
pub columns: BTreeMap<Col, ColRecord>,
}
#[derive(
Debug, Clone, Copy, Serialize, Deserialize, From, Into, PartialEq, Eq, PartialOrd, Ord, Hash,
)]
pub struct Col(pub u64);
impl fmt::Display for Col {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
self.0.fmt(f)
}
}
impl TryFrom<usize> for Col {
type Error = <u64 as TryFrom<usize>>::Error;
fn try_from(value: usize) -> Result<Self, Self::Error> {
value.try_into().map(Self)
}
}
#[derive(
Debug, Clone, Copy, Serialize, Deserialize, From, Into, PartialEq, Eq, PartialOrd, Ord, Hash,
)]
pub struct Row(pub u64);
impl fmt::Display for Row {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
self.0.fmt(f)
}
}
impl TryFrom<usize> for Row {
type Error = <u64 as TryFrom<usize>>::Error;
fn try_from(value: usize) -> Result<Self, Self::Error> {
value.try_into().map(Self)
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TableState {
pub spec: TableSpec,
#[serde(with = "vectorize")]
pub rows: BTreeMap<Row, RowRecord>,
}
impl From<TableSpec> for TableState {
fn from(spec: TableSpec) -> Self {
Self {
spec,
rows: BTreeMap::new(),
}
}
}
impl PackFlow for TableState {
fn layer() -> Layer {
Layer::Visual
}
}
impl Flow for TableState {
type Action = ();
type Event = TableEvent;
fn stream_type() -> StreamType {
StreamType::from(module_path!())
}
fn apply(&mut self, event: Self::Event) {
match event {
TableEvent::AddRow { row } => {
let record = RowRecord {
cols: BTreeMap::new(),
};
self.rows.insert(row, record);
}
TableEvent::DelRow { row } => {
self.rows.remove(&row);
}
TableEvent::SetCell { row, col, value } => {
if let Some(record) = self.rows.get_mut(&row) {
if self.spec.columns.contains_key(&col) {
record.cols.insert(col, value);
}
}
}
}
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum TableEvent {
AddRow { row: Row },
DelRow { row: Row },
SetCell { row: Row, col: Col, value: String },
}
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub struct ColRecord {
pub title: String,
}
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub struct RowRecord {
#[serde(with = "vectorize")]
pub cols: BTreeMap<Col, String>,
}