Skip to content

Commit ce9d675

Browse files
authored
Merge pull request #1441 from Sakapoi/new_master_merge
READY: master merge
2 parents b13e9e3 + 2cf4f77 commit ce9d675

File tree

3 files changed

+193
-1
lines changed

3 files changed

+193
-1
lines changed

module/core/collection_tools/src/collection.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,3 +30,4 @@ pub mod llist;
3030
pub mod vec;
3131
/// [std::collections::VecDeque] macros
3232
pub mod deque;
33+

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -584,4 +584,4 @@ pub mod prelude
584584
use super::*;
585585
}
586586

587-
//
587+
//
Lines changed: 191 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,191 @@
1+
#[ allow( unused_imports ) ]
2+
use super::*;
3+
4+
use the_module::
5+
{
6+
Fields,
7+
IteratorTrait,
8+
AsTable,
9+
Cells,
10+
TableSize,
11+
TableRows,
12+
TableHeader,
13+
Context,
14+
WithRef,
15+
MaybeAs,
16+
};
17+
18+
use std::
19+
{
20+
collections::HashMap,
21+
borrow::Cow,
22+
};
23+
24+
/// Struct representing a test object with various fields.
25+
#[ derive( Clone, Debug ) ]
26+
pub struct TestObject
27+
{
28+
pub id : String,
29+
pub created_at : i64,
30+
pub file_ids : Vec< String >,
31+
pub tools : Option< Vec< HashMap< String, String > > >,
32+
}
33+
34+
impl Fields< &'static str, MaybeAs< '_, str, WithRef > >
35+
for TestObject
36+
{
37+
type Value< 'v > = MaybeAs< 'v, str, WithRef >;
38+
39+
fn fields( &self ) -> impl IteratorTrait< Item = ( &'static str, MaybeAs< '_, str, WithRef > ) >
40+
{
41+
// use format_tools::ref_or_display_or_debug_multiline::field;
42+
use format_tools::ref_or_display_or_debug::field;
43+
let mut dst : Vec< ( &'static str, MaybeAs< '_, str, WithRef > ) > = Vec::new();
44+
45+
dst.push( field!( &self.id ) );
46+
dst.push( field!( &self.created_at ) );
47+
dst.push( field!( &self.file_ids ) );
48+
49+
if let Some( tools ) = &self.tools
50+
{
51+
dst.push( field!( tools ) );
52+
}
53+
else
54+
{
55+
dst.push( ( "tools", MaybeAs::none() ) );
56+
}
57+
58+
dst.into_iter()
59+
}
60+
}
61+
62+
//
63+
64+
fn test_objects_gen() -> Vec< TestObject >
65+
{
66+
67+
vec!
68+
[
69+
TestObject
70+
{
71+
id : "1".to_string(),
72+
created_at : 1627845583,
73+
file_ids : vec![ "file1".to_string(), "file2".to_string() ],
74+
tools : None
75+
},
76+
TestObject
77+
{
78+
id : "2".to_string(),
79+
created_at : 13,
80+
file_ids : vec![ "file3".to_string(), "file4\nmore details".to_string() ],
81+
tools : Some
82+
(
83+
vec!
84+
[
85+
{
86+
let mut map = HashMap::new();
87+
map.insert( "tool1".to_string(), "value1".to_string() );
88+
map
89+
},
90+
{
91+
let mut map = HashMap::new();
92+
map.insert( "tool2".to_string(), "value2".to_string() );
93+
map
94+
}
95+
]
96+
),
97+
},
98+
]
99+
100+
}
101+
102+
//
103+
104+
#[ test ]
105+
fn table_to_string()
106+
// where
107+
// for< 'a > AsTable< 'a, Vec< TestObject >, usize, TestObject, &'static str, String, &'static str > : TableFormatter< 'a >,
108+
{
109+
use the_module::TableToString;
110+
let test_objects = test_objects_gen();
111+
112+
let cells = Cells::< &'static str, WithRef >::cells( &test_objects[ 0 ] );
113+
assert_eq!( cells.len(), 4 );
114+
let cells = Cells::< &'static str, WithRef >::cells( &test_objects[ 1 ] );
115+
assert_eq!( cells.len(), 4 );
116+
drop( cells );
117+
118+
let as_table : AsTable< '_, Vec< TestObject >, usize, TestObject, &str, WithRef > = AsTable::new( &test_objects );
119+
let size = TableSize::mcells( &as_table );
120+
assert_eq!( size, [ 2, 4 ] );
121+
let rows = TableRows::rows( &as_table );
122+
assert_eq!( rows.len(), 2 );
123+
dbg!( rows.collect::< Vec< _ > >() );
124+
let header = TableHeader::header( &as_table );
125+
assert!( header.is_some() );
126+
let header = header.unwrap();
127+
assert_eq!( header.len(), 4 );
128+
assert_eq!( header.clone().collect::< Vec< _ > >(), vec!
129+
[
130+
( "id", Cow::Owned( "id".to_string() ) ),
131+
( "created_at", Cow::Owned( "created_at".to_string() ) ),
132+
( "file_ids", Cow::Owned( "file_ids".to_string() ) ),
133+
( "tools", Cow::Owned( "tools".to_string() ) )
134+
]);
135+
dbg!( header.collect::< Vec< _ > >() );
136+
137+
let mut output = String::new();
138+
let mut context = Context::new( &mut output, Default::default() );
139+
let got = the_module::TableFormatter::fmt( &as_table, &mut context );
140+
assert!( got.is_ok() );
141+
println!( "{}", &output );
142+
143+
// with explicit arguments
144+
145+
let as_table : AsTable< '_, Vec< TestObject >, usize, TestObject, &str, WithRef > = AsTable::new( &test_objects );
146+
let table_string = as_table.table_to_string();
147+
assert!( table_string.contains( "id" ) );
148+
assert!( table_string.contains( "created_at" ) );
149+
assert!( table_string.contains( "file_ids" ) );
150+
assert!( table_string.contains( "tools" ) );
151+
152+
// without explicit arguments
153+
154+
println!( "" );
155+
let as_table = AsTable::new( &test_objects );
156+
let table_string = as_table.table_to_string();
157+
assert!( table_string.contains( "id" ) );
158+
assert!( table_string.contains( "created_at" ) );
159+
assert!( table_string.contains( "file_ids" ) );
160+
assert!( table_string.contains( "tools" ) );
161+
println!( "{table_string}" );
162+
163+
}
164+
165+
#[ test ]
166+
fn custom_formatter()
167+
{
168+
// use the_module::TableToString;
169+
let test_objects = test_objects_gen();
170+
171+
let mut output = String::new();
172+
let mut formatter = the_module::Styles::default();
173+
formatter.cell_separator = " | ".into();
174+
formatter.row_prefix = "> ".into();
175+
formatter.row_postfix = " <".into();
176+
177+
let as_table = AsTable::new( &test_objects );
178+
let mut context = Context::new( &mut output, formatter );
179+
let got = the_module::TableFormatter::fmt( &as_table, &mut context );
180+
assert!( got.is_ok() );
181+
// let table_string = got.unwrap();
182+
183+
assert!( output.contains( "id" ) );
184+
assert!( output.contains( "created_at" ) );
185+
assert!( output.contains( "file_ids" ) );
186+
assert!( output.contains( "tools" ) );
187+
println!( "{output}" );
188+
189+
}
190+
191+
// xxx

0 commit comments

Comments
 (0)