dataflow/
lib.rs

1#![allow(unused_variables)]
2#![allow(unused_imports)]
3#![allow(dead_code)]
4
5use intmap::IntMap;
6use std::{collections::HashMap, fmt};
7
8pub mod collection;
9pub mod collumn;
10pub mod row;
11
12#[derive(Clone, Debug)]
13pub struct Channel {
14    pub name: String,
15    pub tag: u8,
16    pub visible: bool,
17}
18
19#[derive(Clone, Debug)]
20pub struct DataFrame<M, D>
21where
22    M: fmt::Display,
23{
24    // metric contain something like a timestamp, or month names
25    pub metric: M,
26    // data key is tag for stream meta (a.k.a column tag)
27    // D is
28    pub data: IntMap<D>,
29}
30
31pub struct DataCellChangeRecord<T>
32where
33    T: fmt::Display,
34{
35    pub row_index: usize,
36    pub column_index: usize,
37    pub old_value: T,
38    pub new_value: T,
39}
40
41impl<T> DataCellChangeRecord<T>
42where
43    T: fmt::Display,
44{
45    fn new(row_index: usize, column_index: usize, old_value: T, new_value: T) -> Self {
46        Self {
47            row_index,
48            column_index,
49            old_value,
50            new_value,
51        }
52    }
53}
54
55impl<T> fmt::Display for DataCellChangeRecord<T>
56where
57    T: fmt::Display,
58{
59    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
60        write!(
61            f,
62            "DataCellChangeRecord {{ rowIndex: {}, colIndex; {}, {}, {} }}",
63            self.row_index, self.column_index, self.old_value, self.new_value
64        )
65    }
66}
67
68pub struct DataCollectionChangeRecord {
69    pub index: usize,
70    pub added_count: usize,
71    pub removed_count: usize,
72}
73
74impl DataCollectionChangeRecord {
75    fn new(index: usize, added_count: usize, removed_count: usize) -> Self {
76        Self {
77            index,
78            added_count,
79            removed_count,
80        }
81    }
82}
83
84impl fmt::Display for DataCollectionChangeRecord {
85    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
86        write!(
87            f,
88            "DataCollectionChangeRecord {{ index: {}, added: {}, removed: {} }}",
89            self.index, self.added_count, self.removed_count
90        )
91    }
92}
93
94// should impl getters/setters
95pub trait TableEntity {}
96
97#[derive(Clone)]
98pub struct DataStream<M, D>
99where
100    M: fmt::Display,
101    D: fmt::Display + Copy,
102{
103    pub meta: Vec<Channel>,
104    // data is renamed to frames
105    pub frames: Vec<DataFrame<M, D>>,
106    // column_index_by_name: HashMap<String, usize>,
107    // columns: Option<DataColumnCollection<M, D>>,
108    // rows: Option<DataRowCollection<M, D>>,
109    // cellChangeController: StreamController<DataCellChangeRecord>,
110    // columnsChangeController: StreamController<DataCollectionChangeRecord>,
111    // rowsChangeController: StreamController<DataCollectionChangeRecord>,
112}
113
114impl<M, D> Default for DataStream<M, D>
115where
116    M: fmt::Display,
117    D: fmt::Display + Copy,
118{
119    fn default() -> Self {
120        Self {
121            meta: Vec::new(),
122            frames: Vec::new(),
123        }
124    }
125}
126
127impl<M, D> DataStream<M, D>
128where
129    M: fmt::Display,
130    D: fmt::Display + Copy,
131{
132    /// Creates a [DataTable] with optional data [data].
133    ///
134    /// * 'meta' - contains row names
135    ///
136    /// The first row in [data] contains the column names.
137    /// The data type of each column is determined by the first non-null value
138    /// in that column.
139    ///
140    /// All values in each column are expected to be of the same type,
141    /// and all rows are expected to have the same length.
142    // data is optional
143    pub fn new(meta: Vec<Channel>, frames: Vec<DataFrame<M, D>>) -> Self {
144        // let data_table = DataTable {
145        //     // column_index_by_name: Default::default(),
146        //     // columns: None,
147        //     // rows: None,
148        // };
149
150        // let column_index_by_name = HashMap::<String, usize>::new();
151        // let rows: DataRowCollection<String> = DataRowCollection::new(&data_table);
152        // let columns: DataColumnCollection = DataColumnCollection::new(&data_table);
153
154        // let col_count = metadata.len();
155        // let row_count = data.len();
156
157        // // first deal with columns
158        // for col_idx in 0..col_count {
159        //     let name = metadata[col_idx];
160        //     columns.add(DataColumn { index: 0, name });
161        // }
162
163        // rows.add_all(data);
164        Self { meta, frames }
165    }
166
167    fn on_cell_changed(row_index: i64, column_index: i64, old_value: String, new_value: String) {
168        // if (cellChangeController != null) {
169        //   let record =
170        //       DataCellChangeRecord(rowIndex, columnIndex, oldValue, newValue);
171        //   cellChangeController.add(record);
172        // }
173    }
174
175    // fn onRowsOrColumnsInserted(source: DataCollectionBase, index: i64, count: i64) {
176    //     // let record = DataCollectionChangeRecord(index, count, 0);
177    //     // if (source == columns) {
178    //     //   insertColumns(index, count);
179    //     //   updateColumnIndexes(index);
180    //     //   columnsChangeController?.add(record);
181    //     // } else {
182    //     //   rowsChangeController?.add(record);
183    //     // }
184    // }
185
186    // fn onRowsOrColumnsRemoved(source: DataCollectionBase, index: i64, count: i64) {
187    //     // let record = DataCollectionChangeRecord(index, 0, count);
188    //     // if (source == columns) {
189    //     //   removeColumns(index, count);
190    //     //   updateColumnIndexes(index);
191    //     //   columnsChangeController?.add(record);
192    //     // } else {
193    //     //   rowsChangeController?.add(record);
194    //     // }
195    // }
196
197    fn insert_columns(start: i64, count: i64) {
198        // for (let row in rows) {
199        //   row.cells.insertAll(start, List(count));
200        // }
201    }
202
203    fn remove_columns(start: i64, count: i64) {
204        // for (let row in rows) {
205        //   row.cells.remove_range(start, start + count);
206        // }
207    }
208
209    fn update_column_indexes(start: i64) {
210        // let end = columns.length;
211        // while (start < end) {
212        //   columnIndexByName[_columns[start].name] = start++;
213        // }
214    }
215
216    // /// The columns in this [DataTable].
217    // DataColumnCollection get columns => columns;
218
219    // /// The rows (without the header row) in this [DataTable].
220    // DataRowCollection get rows => rows;
221
222    // /// Fired when a cell is changed.
223    // Stream<DataCellChangeRecord> get onCellChange {
224    // ??= - Assign the value if the variable is null
225    //   cellChangeController ??= StreamController.broadcast(
226    //       sync: true,
227    //       onCancel: () {
228    //         cellChangeController = null;
229    //       });
230    //   return cellChangeController.stream;
231    // }
232
233    // /// Fired when [columns] are changed.
234    // Stream<DataCollectionChangeRecord> get onColumnsChange {
235    // ??= - Assign the value if the variable is null
236    //   columnsChangeController ??= StreamController.broadcast(
237    //       sync: true,
238    //       onCancel: () {
239    //         columnsChangeController = null;
240    //       });
241    //   return columnsChangeController.stream;
242    // }
243
244    // /// Fired when [rows] are changed.
245    // Stream<DataCollectionChangeRecord> get onRowsChange {
246    // ??= - Assign the value if the variable is null
247    //   rowsChangeController ??= StreamController.broadcast(
248    //       sync: true,
249    //       onCancel: () {
250    //         rowsChangeController = null;
251    //       });
252    //   return rowsChangeController.stream;
253    // }
254
255    // /// Gets the index of the column specified by [name].
256    // i64 getColumnIndexByName(String name) {
257    //   if (columnIndexByName.containsKey(name)) {
258    //     return columnIndexByName[name];
259    //   }
260    //   return -1;
261    // }
262
263    // /// Gets the values of the column specified by [columnIndex].
264    // Vec<T> getColumnValues<T>(i64 columnIndex) {
265    //   let list = <T>[];
266    //   for (let row in rows) {
267    //     list.add(row[columnIndex]);
268    //   }
269    //   return list;
270    // }
271}
272
273#[cfg(test)]
274mod tests {
275    #[test]
276    fn it_works() {
277        assert_eq!(2 + 2, 4);
278    }
279}