Skip to content

Commit 0faaddb

Browse files
author
InAnYan
committed
Continue to work on
1 parent d3bf267 commit 0faaddb

File tree

3 files changed

+87
-46
lines changed

3 files changed

+87
-46
lines changed

module/core/format_tools/src/format/output_format.rs

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@
3232
mod private
3333
{
3434

35+
use std::borrow::Cow;
36+
3537
use crate::*;
3638
use print::
3739
{
@@ -78,6 +80,57 @@ mod private
7880
}
7981
}
8082

83+
/*
84+
pub fn table_data_write< 'buf, 'data, 'context >
85+
(
86+
table : &Vec< Vec< Cow< 'data, str > > >,
87+
has_header : bool,
88+
filter_col : &'context ( dyn FilterCol + 'context ),
89+
filter_row : &'context ( dyn FilterRow + 'context ),
90+
output_format : impl TableOutputFormat,
91+
c : &mut Context< 'buf >,
92+
) -> fmt::Result
93+
{
94+
struct TableWrapper< 'data >
95+
{
96+
has_header : bool,
97+
data : &'data Vec< Vec< Cow< 'data, str > > >,
98+
}
99+
100+
struct CellsWrapper< 'data >
101+
{
102+
header : Option< &'data Vec< Cow< 'data, str
103+
data : &'data Vec< Cow< 'data, str > >,
104+
}
105+
106+
impl Cells< str > for CellsWrapper
107+
{
108+
fn cells< 'a, 'b >( &'a self ) -> impl IteratorTrait< Item = ( &'b CellKey, Option< Cow< 'b, str > > ) >
109+
where
110+
'a : 'b,
111+
CellKey : 'b,
112+
{
113+
self.data.iter().map( | c | )
114+
}
115+
}
116+
117+
impl TableRows for TableWrapper
118+
{
119+
fn rows( &self ) -> impl IteratorTrait< Item = &Self::Row >
120+
{
121+
self.data.iter().skip( if self.has_header { 1 } else { 0 } )
122+
}
123+
}
124+
125+
let wrapped_table = TableWrapper { has_header, data : table };
126+
127+
InputExtract::extract( &wrapped_table, filter_col, filter_row, | x |
128+
{
129+
output_format.extract_write( x, c )
130+
})
131+
}
132+
*/
133+
81134
}
82135

83136
mod table;

module/core/format_tools/src/format/print.rs

Lines changed: 33 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -364,6 +364,7 @@ mod private
364364
""
365365
}
366366
}
367+
367368
/// Extract input data from and collect it in a format consumable by output formatter.
368369
pub fn extract< 't, 'context, Table, RowKey, Row, CellKey>
369370
(
@@ -379,9 +380,27 @@ mod private
379380
Table : TableRows< RowKey = RowKey, Row = Row, CellKey = CellKey >,
380381
Table : TableHeader< CellKey = CellKey >,
381382
RowKey : table::RowKey,
382-
Row : Cells< CellKey> + 'data,
383+
Row : Cells< CellKey > + 'data,
383384
CellKey : table::CellKey + ?Sized + 'data,
384385
// CellRepr : table::CellRepr,
386+
{
387+
Self::extract_from_raw_table
388+
(
389+
filter_col,
390+
filter_row,
391+
callback,
392+
)
393+
}
394+
395+
pub fn extract_from_raw_table< 'context >
396+
(
397+
column_names : Vec< Cow< 'data, str > >,
398+
has_header : bool,
399+
rows : Vec< Vec< Cow< 'data, str > > >,
400+
filter_col : &'context ( dyn FilterCol + 'context ),
401+
filter_row : &'context ( dyn FilterRow + 'context ),
402+
callback : impl for< 'a2 > FnOnce( &'a2 InputExtract< 'a2 > ) -> fmt::Result,
403+
) -> fmt::Result
385404
{
386405
use md_math::MdOffset;
387406

@@ -391,19 +410,17 @@ mod private
391410
let mut mchars = [ 0 ; 2 ];
392411

393412
// key width, index
394-
let mut key_to_ikey : HashMap< &'t CellKey, usize > = HashMap::new();
413+
let mut key_to_ikey : HashMap< Cow< 'data, str >, usize > = HashMap::new();
395414

396415
let mut col_descriptors : Vec< ColDescriptor< '_ > > = Vec::with_capacity( mcells[ 0 ] );
397416
let mut row_descriptors : Vec< RowDescriptor > = Vec::with_capacity( mcells[ 1 ] );
398-
let mut has_header = false;
399417

400-
let mut data : Vec< Vec< ( Cow< 't, str >, [ usize ; 2 ] ) > > = Vec::new();
401-
let rows = table.rows();
418+
let mut data : Vec< Vec< ( Cow< 'data, str >, [ usize ; 2 ] ) > > = Vec::new();
402419
let mut irow : usize = 0;
403420
let filter_col_need_args = filter_col.need_args();
404421
// let filter_row_need_args = filter_row.need_args();
405422

406-
let mut row_add = | row_iter : &'_ mut dyn _IteratorTrait< Item = ( &'t CellKey, Cow< 't, str > ) >, typ : LineType |
423+
let mut row_add = | row_data : Vec< Cow< 'data, str > >, typ : LineType |
407424
{
408425

409426
irow = row_descriptors.len();
@@ -413,18 +430,21 @@ mod private
413430
let mut ncol = 0;
414431
let mut ncol_vis = 0;
415432

416-
let fields : Vec< ( Cow< 't, str >, [ usize ; 2 ] ) > = row_iter
433+
let fields : Vec< ( Cow< 'data, str >, [ usize ; 2 ] ) > = row_data
434+
.into_iter()
435+
.enumerate()
417436
.filter_map
418437
(
419-
| ( key, val ) |
438+
| ( ikey, val ) |
420439
{
440+
let key = &column_names[ ikey ];
421441
let l = col_descriptors.len();
422442

423443
ncol += 1;
424444

425445
if filter_col_need_args
426446
{
427-
if !filter_col.filter_col( key.borrow() )
447+
if !filter_col.filter_col( key.as_ref() )
428448
{
429449
return None;
430450
}
@@ -442,7 +462,7 @@ mod private
442462
let sz = string::size( &val );
443463

444464
key_to_ikey
445-
.entry( key )
465+
.entry( key.clone() )
446466
.and_modify( | icol |
447467
{
448468
let col = &mut col_descriptors[ *icol ];
@@ -481,18 +501,9 @@ mod private
481501

482502
// process header first
483503

484-
if let Some( header ) = table.header()
504+
if has_header
485505
{
486-
rows.len().checked_add( 1 ).expect( "Table has too many rows" );
487-
// assert!( header.len() <= usize::MAX, "Header of a table has too many cells" );
488-
has_header = true;
489-
490-
let mut row2 = header.map( | ( key, title ) |
491-
{
492-
( key, Cow::Borrowed( title ) )
493-
});
494-
495-
row_add( &mut row2, LineType::Header );
506+
row_add( column_names.clone(), LineType::Header );
496507
}
497508

498509
// Collect rows
@@ -501,30 +512,7 @@ mod private
501512
{
502513
// assert!( row.cells().len() <= usize::MAX, "Row of a table has too many cells" );
503514

504-
let mut row2 = row
505-
.cells()
506-
.map
507-
(
508-
| ( key, val ) |
509-
{
510-
511-
let val = match val
512-
{
513-
Some( val ) =>
514-
{
515-
val
516-
}
517-
None =>
518-
{
519-
Cow::Borrowed( "" )
520-
}
521-
};
522-
523-
return ( key, val );
524-
}
525-
);
526-
527-
row_add( &mut row2, LineType::Regular );
515+
row_add( row, LineType::Regular );
528516
}
529517

530518
// calculate size in chars

module/core/format_tools/tests/tests.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//! Primary tests.
22
3-
#![ feature( trace_macros ) ]
3+
// #![ feature( trace_macros ) ]
44
#![ allow( unused_imports ) ]
55

66
use format_tools as the_module;

0 commit comments

Comments
 (0)