Skip to content

Commit 47c7543

Browse files
author
InAnYan
committed
Add test and fix bug in extract
1 parent 2b34a1f commit 47c7543

File tree

3 files changed

+28
-71
lines changed

3 files changed

+28
-71
lines changed

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

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -413,7 +413,13 @@ mod private
413413
let mut ncol = 0;
414414
let mut ncol_vis = 0;
415415

416-
let fields : Vec< ( Cow< 't, str >, [ usize ; 2 ] ) > = row_iter
416+
// This type stores these data:
417+
// index cell data size of cell
418+
// of the column
419+
//
420+
// We have to store index of the column in order to NOT rely on order of the
421+
// `Cells::cells`.
422+
let mut fields : Vec< ( usize, Cow< 't, str >, [ usize ; 2 ] ) > = row_iter
417423
.filter_map
418424
(
419425
| ( key, val ) |
@@ -459,11 +465,14 @@ mod private
459465
});
460466

461467
row.height = row.height.max( sz[ 1 ] );
462-
return Some( ( val, sz ) );
468+
return Some( ( key_to_ikey[ key ], val, sz ) );
463469
}
464470
)
465471
.collect();
466472

473+
fields.sort_by( | ( i1, _, _ ), ( i2, _, _ ) | i1.cmp( i2 ) );
474+
let fields : Vec< ( Cow< 't, str >, [ usize ; 2 ] ) > = fields.into_iter().map( | ( _, k, v ) | ( k, v ) ).collect();
475+
467476
mcells[ 0 ] = mcells[ 0 ].max( ncol );
468477
mcells_vis[ 0 ] = mcells_vis[ 0 ].max( ncol_vis );
469478

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

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -79,9 +79,6 @@ mod private
7979
/// Marker trait to tag structures for which table trait deducing should be done from trait Fields, which is reflection.
8080
pub trait TableWithFields {}
8181

82-
impl TableWithFields for HashMap< String, String > {}
83-
impl TableWithFields for HashMap< &str, String > {}
84-
8582
// =
8683

8784
/// A trait for iterating over all cells of a row.
@@ -99,6 +96,16 @@ mod private
9996
;
10097
}
10198

99+
impl Cells< str > for HashMap< String, String >
100+
{
101+
fn cells< 'a, 'b >( &'a self ) -> impl IteratorTrait< Item = ( &'b str, Option< Cow< 'b, str > > ) >
102+
where
103+
'a : 'b,
104+
{
105+
self.iter().map( | ( k, v ) | ( k.as_str(), Some( Cow::from( v ) ) ) )
106+
}
107+
}
108+
102109
impl< Row, CellKey > Cells< CellKey >
103110
for Row
104111
where
@@ -195,7 +202,7 @@ mod private
195202
> + 'k + 'v,
196203

197204
RowKey : table::RowKey,
198-
Row : TableWithFields + Cells< CellKey >,
205+
Row : Cells< CellKey >,
199206
CellKey : table::CellKey + ?Sized,
200207
// CellRepr : table::CellRepr,
201208
{
@@ -271,7 +278,7 @@ mod private
271278
where
272279
Self : TableRows< RowKey = RowKey, Row = Row, CellKey = CellKey >,
273280
RowKey : table::RowKey,
274-
Row : TableWithFields + Cells< CellKey >,
281+
Row : Cells< CellKey >,
275282
CellKey : table::CellKey + ?Sized,
276283
// CellRepr : table::CellRepr,
277284
{

module/core/format_tools/tests/inc/collection_test.rs

Lines changed: 5 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -411,30 +411,21 @@ fn vec_of_hashmap()
411411
let mut map = HashMap::new();
412412
map.insert( "id".to_string(), "1".to_string() );
413413
map.insert( "created_at".to_string(), "1627845583".to_string() );
414-
map.insert( "file_ids".to_string(), "[ file1, file2 ]".to_string() );
415-
map.insert( "tools".to_string(), "".to_string() );
416414
map
417415
},
418416
{
419417
let mut map = HashMap::new();
420418
map.insert( "id".to_string(), "2".to_string() );
421419
map.insert( "created_at".to_string(), "13".to_string() );
422-
map.insert( "file_ids".to_string(), "[ file3, file4 ]".to_string() );
423-
map.insert( "tools".to_string(), "tool1".to_string() );
424420
map
425421
},
426422
];
427423

428-
use the_module::Fields;
429-
430424
use std::borrow::Cow;
431425

432-
// let _ = <HashMap<std::string::String, std::string::String> as Fields<String, Option<Cow<'_, str>>>>::fields(&map);
433-
434426
use the_module::TableFormatter;
435-
let _as_table : AsTable< '_, Vec< HashMap< String, String > >, &str, HashMap< String, String >, str> = AsTable::new( &data );
436427

437-
/*
428+
let _as_table : AsTable< '_, Vec< HashMap< String, String > >, &str, HashMap< String, String >, str> = AsTable::new( &data );
438429
let as_table = AsTable::new( &data );
439430

440431
let rows = TableRows::rows( &as_table );
@@ -447,59 +438,9 @@ fn vec_of_hashmap()
447438

448439
let got = as_table.table_to_string();
449440

450-
assert!( got.contains( "│ id │ created_at │ file_ids │ tools │" ) );
451-
assert!( got.contains( "│ 13 │ [ │ [ │" ) );
452-
assert!( got.contains( "│ 1627845583 │ [ │ │" ) );
453-
*/
454-
}
455-
456-
#[ test ]
457-
fn vec_of_hashmap_str()
458-
{
459-
let data : Vec< HashMap< &str, String > > = vec!
460-
[
461-
{
462-
let mut map = HashMap::new();
463-
map.insert( "id", "1".to_string() );
464-
map.insert( "created_at", "1627845583".to_string() );
465-
map.insert( "file_ids", "[ file1, file2 ]".to_string() );
466-
map.insert( "tools", "".to_string() );
467-
map
468-
},
469-
{
470-
let mut map = HashMap::new();
471-
map.insert( "id", "2".to_string() );
472-
map.insert( "created_at", "13".to_string() );
473-
map.insert( "file_ids", "[ file3, file4 ]".to_string() );
474-
map.insert( "tools", "tool1".to_string() );
475-
map
476-
},
477-
];
478-
479-
use the_module::Fields;
480-
481-
use std::borrow::Cow;
482-
483-
// let _ = <HashMap<std::string::String, std::string::String> as Fields<String, Option<Cow<'_, str>>>>::fields(&map);
484-
485-
use the_module::TableFormatter;
486-
//let _as_table : AsTable< '_, Vec< HashMap< &str, String > >, &str, HashMap< &str, String >, str> = AsTable::new( &data );
487-
488-
/*
489-
let as_table = AsTable::new( &data );
490-
491-
let rows = TableRows::rows( &as_table );
492-
assert_eq!( rows.len(), 2 );
441+
println!("{}", got);
493442

494-
let mut output = String::new();
495-
let mut context = the_module::print::Context::new( &mut output, Default::default() );
496-
497-
let _got = the_module::TableFormatter::fmt( &as_table, &mut context );
498-
499-
let got = as_table.table_to_string();
500-
501-
assert!( got.contains( "│ id │ created_at │ file_ids │ tools │" ) );
502-
assert!( got.contains( "│ 13 │ [ │ [ │" ) );
503-
assert!( got.contains( "│ 1627845583 │ [ │ │" ) );
504-
*/
443+
assert!( got.contains( "│ id │ created_at │" ) || got.contains( "│ created_at │ id │" ) );
444+
assert!( got.contains( "│ 1 │ 1627845583 │" ) || got.contains( "│ 1627845583 │ 1 │" ) );
445+
assert!( got.contains( "│ 2 │ 13 │" ) || got.contains( "│ 13 │ 2 │" ) );
505446
}

0 commit comments

Comments
 (0)