diff --git a/module/core/format_tools/src/format/as_table.rs b/module/core/format_tools/src/format/as_table.rs index 7409b42952..d269556525 100644 --- a/module/core/format_tools/src/format/as_table.rs +++ b/module/core/format_tools/src/format/as_table.rs @@ -32,7 +32,7 @@ mod private ) where RowKey : table::RowKey, - Row : Cells< CellKey>, + Row : Cells< CellKey >, CellKey : table::CellKey + ?Sized, // CellRepr : table::CellRepr ; @@ -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, { @@ -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, { @@ -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, { @@ -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, { @@ -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, { @@ -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, diff --git a/module/core/format_tools/src/format/print.rs b/module/core/format_tools/src/format/print.rs index b4c1b8d6f3..e102eb5372 100644 --- a/module/core/format_tools/src/format/print.rs +++ b/module/core/format_tools/src/format/print.rs @@ -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(); @@ -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, diff --git a/module/core/format_tools/src/format/table.rs b/module/core/format_tools/src/format/table.rs index 27e44ca0e5..1fab2ab744 100644 --- a/module/core/format_tools/src/format/table.rs +++ b/module/core/format_tools/src/format/table.rs @@ -12,7 +12,11 @@ mod private // fmt, borrow::Borrow, }; - use std::borrow::Cow; + use std:: + { + borrow::Cow, + collections::HashMap, + }; use reflect_tools:: { IteratorTrait, @@ -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 {} // = @@ -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 @@ -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, { @@ -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, { diff --git a/module/core/format_tools/tests/inc/collection_test.rs b/module/core/format_tools/tests/inc/collection_test.rs index 6b17ce0975..0d066004e2 100644 --- a/module/core/format_tools/tests/inc/collection_test.rs +++ b/module/core/format_tools/tests/inc/collection_test.rs @@ -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 │" ) ); +} \ No newline at end of file