Skip to content

Commit 3c2204e

Browse files
author
InAnYan
committed
Make test
1 parent e9c5618 commit 3c2204e

File tree

3 files changed

+83
-39
lines changed

3 files changed

+83
-39
lines changed

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

Lines changed: 16 additions & 39 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,56 +80,30 @@ mod private
7880
}
7981
}
8082

81-
/*
82-
pub fn table_data_write< 'buf, 'data, 'context >
83+
/// Print table, which is constructed with vectors and `Cow`s, with the
84+
/// specified output formatter.
85+
///
86+
/// This function is useful when you do not want to use `AsTable`, or implement `Fields`, and
87+
/// other traits, but you just have string slices in vectors.
88+
///
89+
/// `rows` should not contain header of the table, it will be automatically added if `has_header`
90+
/// is true.
91+
pub fn vector_table_write< 'buf, 'data, 'context >
8392
(
84-
table : &Vec< Vec< Cow< 'data, str > > >,
93+
column_names : Vec< Cow< 'data, str > >,
8594
has_header : bool,
95+
rows : Vec< Vec< Cow< 'data, str > > >,
8696
filter_col : &'context ( dyn FilterCol + 'context ),
8797
filter_row : &'context ( dyn FilterRow + 'context ),
88-
output_format : impl TableOutputFormat,
98+
output_format : &impl TableOutputFormat,
8999
c : &mut Context< 'buf >,
90100
) -> fmt::Result
91101
{
92-
struct TableWrapper< 'data >
93-
{
94-
has_header : bool,
95-
data : &'data Vec< Vec< Cow< 'data, str > > >,
96-
}
97-
98-
struct CellsWrapper< 'data >
99-
{
100-
header : Option< &'data Vec< Cow< 'data, str
101-
data : &'data Vec< Cow< 'data, str > >,
102-
}
103-
104-
impl Cells< str > for CellsWrapper
105-
{
106-
fn cells< 'a, 'b >( &'a self ) -> impl IteratorTrait< Item = ( &'b CellKey, Option< Cow< 'b, str > > ) >
107-
where
108-
'a : 'b,
109-
CellKey : 'b,
110-
{
111-
self.data.iter().map( | c | )
112-
}
113-
}
114-
115-
impl TableRows for TableWrapper
116-
{
117-
fn rows( &self ) -> impl IteratorTrait< Item = &Self::Row >
118-
{
119-
self.data.iter().skip( if self.has_header { 1 } else { 0 } )
120-
}
121-
}
122-
123-
let wrapped_table = TableWrapper { has_header, data : table };
124-
125-
InputExtract::extract( &wrapped_table, filter_col, filter_row, | x |
102+
InputExtract::extract_from_raw_table( column_names, has_header, rows, filter_col, filter_row, | x |
126103
{
127104
output_format.extract_write( x, c )
128105
})
129106
}
130-
*/
131107

132108
}
133109

@@ -157,6 +133,7 @@ pub mod own
157133
#[ doc( inline ) ]
158134
pub use private::
159135
{
136+
vector_table_write,
160137
};
161138

162139
}

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -414,6 +414,11 @@ mod private
414414
)
415415
}
416416

417+
/// Extract input data from a table that is constructed with vectors and `Cow`s and collect
418+
/// it in a format consumable by output formatter.
419+
///
420+
/// `rows` should not contain header of the table, it will be automatically added if `has_header`
421+
/// is true.
417422
pub fn extract_from_raw_table< 'context >
418423
(
419424
column_names : Vec< Cow< 'data, str > >,

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

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -298,3 +298,65 @@ fn iterator_over_strings()
298298
// assert!( got.contains( "│ 1627845583 │ [ │ │" ) );
299299

300300
}
301+
302+
#[ test ]
303+
fn test_vector_table()
304+
{
305+
let column_names : Vec< Cow< 'static, str > > = vec![
306+
"id".into(),
307+
"created_at".into(),
308+
"file_ids".into(),
309+
"tools".into(),
310+
];
311+
312+
let rows : Vec< Vec< Cow< 'static, str > > > = vec!
313+
[
314+
vec!
315+
[
316+
"1".into(),
317+
"1627845583".into(),
318+
"[ file1, file2 ]".into(),
319+
"".into(),
320+
],
321+
322+
vec!
323+
[
324+
"2".into(),
325+
"13".into(),
326+
"[ file3, file4 ]".into(),
327+
"[ tool1 ]".into(),
328+
],
329+
];
330+
331+
use the_module::
332+
{
333+
output_format,
334+
filter,
335+
print,
336+
};
337+
338+
let mut output = String::new();
339+
let mut context = print::Context::new( &mut output, Default::default() );
340+
341+
let res = output_format::vector_table_write
342+
(
343+
column_names,
344+
true,
345+
rows,
346+
&filter::All,
347+
&filter::All,
348+
&output_format::Table::default(),
349+
&mut context,
350+
);
351+
352+
assert!( res.is_ok() );
353+
354+
println!( "{}", output );
355+
356+
let exp = r#"│ id │ created_at │ file_ids │ tools │
357+
──────────────────────────────────────────────────
358+
│ 1 │ 1627845583 │ [ file1, file2 ] │ │
359+
│ 2 │ 13 │ [ file3, file4 ] │ [ tool1 ] │"#;
360+
361+
a_id!( output.as_str(), exp );
362+
}

0 commit comments

Comments
 (0)