Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions module/core/format_tools/src/format/as_table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ mod private
)
where
RowKey : table::RowKey,
Row : Cells< CellKey>,
Row : Cells< CellKey >,
CellKey : table::CellKey + ?Sized,
// CellRepr : table::CellRepr
;
Expand All @@ -41,7 +41,7 @@ mod private
AsTable< 'table, Table, RowKey, Row, CellKey>
where
RowKey : table::RowKey,
Row : Cells< CellKey>,
Row : Cells< CellKey >,
CellKey : table::CellKey + ?Sized,
// CellRepr : table::CellRepr,
{
Expand All @@ -56,7 +56,7 @@ mod private
for AsTable< 'table, Table, RowKey, Row, CellKey>
where
RowKey : table::RowKey,
Row : Cells< CellKey>,
Row : Cells< CellKey >,
CellKey : table::CellKey + ?Sized,
// CellRepr : table::CellRepr,
{
Expand All @@ -70,7 +70,7 @@ mod private
for AsTable< 'table, Table, RowKey, Row, CellKey>
where
RowKey : table::RowKey,
Row : Cells< CellKey>,
Row : Cells< CellKey >,
CellKey : table::CellKey + ?Sized,
// CellRepr : table::CellRepr,
{
Expand All @@ -86,7 +86,7 @@ mod private
for AsTable< 'table, Table, RowKey, Row, CellKey>
where
RowKey : table::RowKey,
Row : Cells< CellKey>,
Row : Cells< CellKey >,
CellKey : table::CellKey + ?Sized,
// CellRepr : table::CellRepr,
{
Expand All @@ -101,7 +101,7 @@ mod private
where
Table : fmt::Debug,
RowKey : table::RowKey,
Row : Cells< CellKey>,
Row : Cells< CellKey >,
CellKey : table::CellKey + ?Sized,
// CellRepr : table::CellRepr,
{
Expand Down Expand Up @@ -146,7 +146,7 @@ mod private
for AsTable< 'table, Table, RowKey, Row, CellKey>
where
RowKey : table::RowKey,
Row : Cells< CellKey>,
Row : Cells< CellKey >,
CellKey : table::CellKey + ?Sized,
// CellRepr : table::CellRepr,
Self : Copy,
Expand Down
27 changes: 20 additions & 7 deletions module/core/format_tools/src/format/print.rs
Original file line number Diff line number Diff line change
Expand Up @@ -373,13 +373,26 @@ mod private
CellKey : table::CellKey + ?Sized + 'data,
// CellRepr : table::CellRepr,
{
let rows = table.rows().map( | r | r.cells().map( | ( _, c ) | {
match c
let mut key_to_ikey : HashMap< Cow< 'data, str >, usize > = HashMap::new();
let mut keys_count = 0;

let rows = table.rows().map( | r |
{
let mut unsorted : Vec< ( usize, Cow< 'data, str > ) > = r.cells().map( | ( key, c ) |
{
Some( c ) => c,
None => Cow::from( "" ),
}
}).collect()).collect();
if !key_to_ikey.contains_key( key.borrow() )
{
key_to_ikey.insert( key.borrow().into(), keys_count );
keys_count += 1;
}

( key_to_ikey[ key.borrow() ], c.unwrap_or( Cow::from( "" ) ) )
} ).collect();

unsorted.sort_by( | ( i1, _ ), ( i2, _ ) | i1.cmp(i2) );

unsorted.into_iter().map( | ( _, c ) | c).collect()
} ).collect();

let has_header = table.header().is_some();

Expand Down Expand Up @@ -535,7 +548,7 @@ mod private

mchars[ 0 ] = col_descriptors.iter().fold( 0, | acc, col | acc + col.width );
mchars[ 1 ] = row_descriptors.iter().fold( 0, | acc, row | acc + if row.vis { row.height } else { 0 } );

let mut x = InputExtract::< '_ >
{
mcells,
Expand Down
22 changes: 18 additions & 4 deletions module/core/format_tools/src/format/table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,11 @@ mod private
// fmt,
borrow::Borrow,
};
use std::borrow::Cow;
use std::
{
borrow::Cow,
collections::HashMap,
};
use reflect_tools::
{
IteratorTrait,
Expand Down Expand Up @@ -72,7 +76,7 @@ mod private

// =

/// Marker trait to tag structures for whcih table trait deducing should be done from trait Fields, which is reflection.
/// Marker trait to tag structures for which table trait deducing should be done from trait Fields, which is reflection.
pub trait TableWithFields {}

// =
Expand All @@ -92,6 +96,16 @@ mod private
;
}

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
Expand Down Expand Up @@ -188,7 +202,7 @@ mod private
> + 'k + 'v,

RowKey : table::RowKey,
Row : TableWithFields + Cells< CellKey >,
Row : Cells< CellKey >,
CellKey : table::CellKey + ?Sized,
// CellRepr : table::CellRepr,
{
Expand Down Expand Up @@ -264,7 +278,7 @@ mod private
where
Self : TableRows< RowKey = RowKey, Row = Row, CellKey = CellKey >,
RowKey : table::RowKey,
Row : TableWithFields + Cells< CellKey >,
Row : Cells< CellKey >,
CellKey : table::CellKey + ?Sized,
// CellRepr : table::CellRepr,
{
Expand Down
43 changes: 43 additions & 0 deletions module/core/format_tools/tests/inc/collection_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -401,3 +401,46 @@ fn llist_basic()
}

// qqq : xxx : implement for other containers

#[ test ]
fn vec_of_hashmap()
{
let data : Vec< HashMap< String, String > > = vec!
[
{
let mut map = HashMap::new();
map.insert( "id".to_string(), "1".to_string() );
map.insert( "created_at".to_string(), "1627845583".to_string() );
map
},
{
let mut map = HashMap::new();
map.insert( "id".to_string(), "2".to_string() );
map.insert( "created_at".to_string(), "13".to_string() );
map
},
];

use std::borrow::Cow;

use the_module::TableFormatter;

let _as_table : AsTable< '_, Vec< HashMap< String, String > >, &str, HashMap< String, String >, str> = AsTable::new( &data );
let as_table = AsTable::new( &data );

let rows = TableRows::rows( &as_table );
assert_eq!( rows.len(), 2 );

let mut output = String::new();
let mut context = the_module::print::Context::new( &mut output, Default::default() );

let _got = the_module::TableFormatter::fmt( &as_table, &mut context );

let got = as_table.table_to_string();

println!("{}", got);

assert!( got.contains( "│ id │ created_at │" ) || got.contains( "│ created_at │ id │" ) );
assert!( got.contains( "│ 1 │ 1627845583 │" ) || got.contains( "│ 1627845583 │ 1 │" ) );
assert!( got.contains( "│ 2 │ 13 │" ) || got.contains( "│ 13 │ 2 │" ) );
}
Loading