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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
//!
//! Table interface.
//!
/// Define a private namespace for all its items.
mod private
{
use crate::*;
use core::
{
// fmt,
borrow::Borrow,
};
use std::
{
borrow::Cow,
collections::HashMap,
};
use reflect_tools::
{
IteratorTrait,
Fields,
};
// =
/// Trait for types used as keys of rows in table-like structures.
///
pub trait RowKey
{
}
impl< T > RowKey for T
where
T : ?Sized,
{
}
/// Trait for types used as keys of cells in table-like structures.
///
/// The `CellKey` trait aggregates necessary bounds for keys, ensuring they support
/// debugging, equality comparison, and hashing.
///
pub trait CellKey
where
Self : core::cmp::Eq + std::hash::Hash + Borrow< str >,
{
}
impl< T > CellKey for T
where
T : core::cmp::Eq + std::hash::Hash + Borrow< str > + ?Sized,
{
}
/// Trait for types representing table cell content.
///
/// `CellRepr` aggregates necessary bounds for types used as cell representations,
/// ensuring they are copyable and have a static lifetime.
///
pub trait CellRepr
where
Self : Copy + 'static,
{
}
impl< T > CellRepr for T
where
T : Copy + 'static,
{
}
// =
/// Marker trait to tag structures for which table trait deducing should be done from trait Fields, which is reflection.
pub trait TableWithFields {}
// =
/// A trait for iterating over all cells of a row.
pub trait Cells< CellKey >
where
// CellRepr : table::CellRepr,
CellKey : table::CellKey + ?Sized,
{
/// Returns an iterator over all cells of the row.
// fn cells< 'a, 'b >( &'a self ) -> impl IteratorTrait< Item = ( &'b CellKey, OptionalCow< 'b, str > ) >
fn cells< 'a, 'b >( &'a self ) -> impl IteratorTrait< Item = ( &'b CellKey, Option< Cow< 'b, str > > ) >
where
'a : 'b,
CellKey : 'b,
;
}
impl Cells< str > for HashMap< String, String >
{
fn cells< 'a, 'b >( &'a self ) -> impl IteratorTrait< Item = ( &'b str, Option< Cow< 'b, str > > ) >
where
'a : 'b,
{
self.iter().map( | ( k, v ) | ( k.as_str(), Some( Cow::from( v ) ) ) )
}
}
impl< Row, CellKey > Cells< CellKey >
for Row
where
CellKey : table::CellKey + ?Sized,
for< 'ckv >
Row : TableWithFields + Fields
<
&'ckv CellKey,
// OptionalCow< 'ckv, str >,
Option< Cow< 'ckv, str > >,
Key< 'ckv > = &'ckv CellKey,
// Val< 'ckv > = OptionalCow< 'ckv, str >,
Val< 'ckv > = Option< Cow< 'ckv, str > >,
> + 'ckv, // xxx
// CellRepr : table::CellRepr,
{
// fn cells< 'a, 'b >( &'a self ) -> impl IteratorTrait< Item = ( &'b CellKey, OptionalCow< 'b, str > ) >
fn cells< 'a, 'b >( &'a self ) -> impl IteratorTrait< Item = ( &'b CellKey, Option< Cow< 'b, str > > ) >
where
'a : 'b,
CellKey : 'b,
{
self.fields().map
(
move | ( key, cell ) |
{
( key, cell )
}
)
}
}
// =
/// Trait for iterating over rows in a table.
///
/// `TableRows` provides an interface to access all rows in a table,
/// allowing iteration over the data structure.
///
/// # Associated Types
///
/// - `RowKey`: The type used to identify each row.
///
/// - `Row`: The type representing a row, which must implement `Cells`
/// for the specified `CellKey` and `CellRepr`.
///
/// - `CellKey`: The type used to identify cells within a row, requiring
/// implementation of the `Key` trait.
///
/// - `CellRepr`: The type representing the content of a cell, requiring
/// implementation of the `CellRepr` trait.
///
/// # Required Methods
///
/// - `rows(&self) -> impl IteratorTrait<Item = &Self::Row>`:
/// Returns an iterator over all rows in the table.
pub trait TableRows
{
///
/// The type used to identify each row.
type RowKey;
///
/// The type representing a row, which must implement `Cells`
/// for the specified `CellKey` and `CellRepr`.
type Row : Cells< Self::CellKey >;
///
/// The type used to identify cells within a row, requiring
/// implementation of the `Key` trait.
type CellKey : table::CellKey + ?Sized;
///
// /// The type representing the content of a cell, requiring
// /// implementation of the `CellRepr` trait.
// type // CellRepr : table::CellRepr;
/// Returns an iterator over all rows of the table.
fn rows( &self ) -> impl IteratorTrait< Item = &Self::Row >;
// fn rows< 'a >( & 'a self ) -> impl IteratorTrait< Item = & 'a Self::Row >
// where
// Self::Row : 'a;
}
impl< T, RowKey, Row, CellKey > TableRows<>
for AsTable< '_, T, RowKey, Row, CellKey >
where
for< 'k, 'v > T : Fields
<
RowKey,
&'k Row,
// Key< 'k > = RowKey,
Val< 'v > = &'v Row,
> + 'k + 'v,
RowKey : table::RowKey,
Row : Cells< CellKey >,
CellKey : table::CellKey + ?Sized,
// CellRepr : table::CellRepr,
{
type RowKey = RowKey;
type Row = Row;
type CellKey = CellKey;
// type CellRepr = CellRepr;
fn rows( &self ) -> impl IteratorTrait< Item = &Self::Row >
// fn rows< 'a >( &'a self ) -> impl IteratorTrait< Item = &'a Self::Row >
// where
// Self::Row : 'a
{
self.as_ref().fields()
.map( move | ( _k, e ) : ( _, &Row ) |
{
e
})
}
}
// =
// /// A trait for iterating over all rows of a table.
// pub trait TableSize
// {
// /// Returns multi-dimensional size of a table.
// fn mcells( &self ) -> [ usize ; 2 ];
// }
//
// impl< T, RowKey, Row, CellKey > TableSize
// for AsTable< '_, T, RowKey, Row, CellKey >
// where
// Self : TableRows< RowKey = RowKey, Row = Row, CellKey = CellKey >,
// RowKey : table::RowKey,
// Row : Cells< CellKey >,
// CellKey : table::CellKey + ?Sized,
// // CellRepr : table::CellRepr,
// {
// fn mcells( &self ) -> [ usize ; 2 ]
// {
// let rows = self.rows();
// let nrows = rows.len();
// let row = rows.clone().next();
// if let Some( row2 ) = row
// {
// let cit = row2.cells().clone();
// let mcells = cit.len();
// [ mcells, nrows + 1 ]
// }
// else
// {
// [ 0, 0 ] // xxx : test
// }
// }
// }
// =
/// Trait returning headers of a table if any.
pub trait TableHeader
{
/// The type used to identify cells within a row, requiring
/// implementation of the `Key` trait.
type CellKey : table::CellKey + ?Sized;
/// Returns an iterator over all fields of the specified type within the entity.
fn header( &self ) -> Option< impl IteratorTrait< Item = ( &Self::CellKey, &'_ str ) > >;
}
impl< T, RowKey, Row, CellKey > TableHeader
for AsTable< '_, T, RowKey, Row, CellKey >
where
Self : TableRows< RowKey = RowKey, Row = Row, CellKey = CellKey >,
RowKey : table::RowKey,
Row : Cells< CellKey >,
CellKey : table::CellKey + ?Sized,
// CellRepr : table::CellRepr,
{
type CellKey = CellKey;
fn header( &self ) -> Option< impl IteratorTrait< Item = ( &Self::CellKey, &'_ str ) > >
{
let mut rows = self.rows();
let row = rows.next();
if let Some( row ) = row
{
Some
(
row
.cells()
.map( | ( key, _title ) | ( key, key.borrow() ) )
)
}
else
{
None
}
}
}
// =
}
#[ allow( unused_imports ) ]
pub use own::*;
/// Own namespace of the module.
#[ allow( unused_imports ) ]
pub mod own
{
use super::*;
#[ doc( inline ) ]
pub use orphan::*;
#[ doc( inline ) ]
pub use private::
{
RowKey,
CellKey,
CellRepr,
};
}
/// Orphan namespace of the module.
#[ allow( unused_imports ) ]
pub mod orphan
{
use super::*;
#[ doc( inline ) ]
pub use exposed::*;
}
/// Exposed namespace of the module.
#[ allow( unused_imports ) ]
pub mod exposed
{
use super::*;
pub use super::super::table;
#[ doc( inline ) ]
pub use private::
{
TableWithFields,
Cells,
TableRows,
// TableSize,
TableHeader,
};
}
/// Prelude to use essentials: `use my_module::prelude::*`.
#[ allow( unused_imports ) ]
pub mod prelude
{
use super::*;
}