Skip to content

Commit 5d2fb42

Browse files
committed
fixing format_tools
1 parent 9d72e1f commit 5d2fb42

File tree

11 files changed

+382
-237
lines changed

11 files changed

+382
-237
lines changed

module/core/format_tools/src/format.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,11 @@ pub mod table;
262262
pub mod to_string;
263263
pub mod to_string_with_fallback;
264264

265+
/// A strucutre for diagnostic and demonstration purpose.
266+
#[ doc( hidden ) ]
267+
#[ cfg( debug_assertions ) ]
268+
pub mod test_object_without_impl;
269+
265270
#[ doc( inline ) ]
266271
#[ allow( unused_imports ) ]
267272
pub use own::*;
@@ -305,6 +310,14 @@ pub mod orphan
305310
ref_or_debug,
306311
};
307312

313+
#[ doc( hidden ) ]
314+
#[ cfg( debug_assertions ) ]
315+
pub use test_object_without_impl::
316+
{
317+
TestObjectWithoutImpl,
318+
test_objects_gen,
319+
};
320+
308321
}
309322

310323
/// Exposed namespace of the module.
Lines changed: 155 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,155 @@
1+
//! A strucutre for diagnostic and demonstration purpose.
2+
3+
// use super::*;
4+
5+
// use crate::
6+
// {
7+
// Fields,
8+
// IteratorTrait,
9+
// TableWithFields,
10+
// WithRef,
11+
// OptionalCow,
12+
// };
13+
14+
use std::
15+
{
16+
collections::HashMap,
17+
hash::Hasher,
18+
hash::Hash,
19+
cmp::Ordering,
20+
// borrow::Cow,
21+
};
22+
23+
/// Struct representing a test object with various fields.
24+
#[ derive( Clone, Debug, PartialEq, Eq ) ]
25+
pub struct TestObjectWithoutImpl
26+
{
27+
pub id : String,
28+
pub created_at : i64,
29+
pub file_ids : Vec< String >,
30+
pub tools : Option< Vec< HashMap< String, String > > >,
31+
}
32+
33+
// TableWithFields is not implemented for TestObjectWithoutImpl intentionally
34+
35+
// impl TableWithFields for TestObjectWithoutImpl {}
36+
//
37+
// impl Fields< &'_ str, Option< Cow< '_, str > > >
38+
// for TestObjectWithoutImpl
39+
// {
40+
// type Key< 'k > = &'k str;
41+
// type Val< 'v > = Option< Cow< 'v, str > >;
42+
//
43+
// fn fields( &self ) -> impl IteratorTrait< Item = ( &'_ str, Option< Cow< '_, str > > ) >
44+
// {
45+
// use format_tools::ref_or_display_or_debug_multiline::field;
46+
// // use format_tools::ref_or_display_or_debug::field;
47+
// let mut dst : Vec< ( &'_ str, Option< Cow< '_, str > > ) > = Vec::new();
48+
//
49+
// dst.push( field!( &self.id ) );
50+
// dst.push( field!( &self.created_at ) );
51+
// dst.push( field!( &self.file_ids ) );
52+
//
53+
// if let Some( tools ) = &self.tools
54+
// {
55+
// dst.push( field!( tools ) );
56+
// }
57+
// else
58+
// {
59+
// dst.push( ( "tools", Option::None ) );
60+
// }
61+
//
62+
// dst.into_iter()
63+
// }
64+
//
65+
// }
66+
67+
impl Hash for TestObjectWithoutImpl
68+
{
69+
70+
fn hash< H: Hasher >( &self, state: &mut H )
71+
{
72+
self.id.hash( state );
73+
self.created_at.hash( state );
74+
self.file_ids.hash( state );
75+
76+
if let Some( tools ) = &self.tools
77+
{
78+
for tool in tools
79+
{
80+
for ( key, value ) in tool
81+
{
82+
key.hash( state );
83+
value.hash( state );
84+
}
85+
}
86+
}
87+
else
88+
{
89+
state.write_u8( 0 );
90+
}
91+
}
92+
93+
}
94+
95+
impl PartialOrd for TestObjectWithoutImpl
96+
{
97+
98+
fn partial_cmp( &self, other: &Self ) -> Option< Ordering >
99+
{
100+
Some( self.cmp( other ) )
101+
}
102+
103+
}
104+
105+
impl Ord for TestObjectWithoutImpl
106+
{
107+
108+
fn cmp( &self, other: &Self ) -> Ordering
109+
{
110+
self.id
111+
.cmp( &other.id )
112+
.then_with( | | self.created_at.cmp( &other.created_at ) )
113+
.then_with( | | self.file_ids.cmp( &other.file_ids ) )
114+
}
115+
116+
}
117+
118+
/// Generate a dynamic array of test objects.
119+
pub fn test_objects_gen() -> Vec< TestObjectWithoutImpl >
120+
{
121+
122+
vec!
123+
[
124+
TestObjectWithoutImpl
125+
{
126+
id : "1".to_string(),
127+
created_at : 1627845583,
128+
file_ids : vec![ "file1".to_string(), "file2".to_string() ],
129+
tools : None
130+
},
131+
TestObjectWithoutImpl
132+
{
133+
id : "2".to_string(),
134+
created_at : 13,
135+
file_ids : vec![ "file3".to_string(), "file4\nmore details".to_string() ],
136+
tools : Some
137+
(
138+
vec!
139+
[
140+
{
141+
let mut map = HashMap::new();
142+
map.insert( "tool1".to_string(), "value1".to_string() );
143+
map
144+
},
145+
{
146+
let mut map = HashMap::new();
147+
map.insert( "tool2".to_string(), "value2".to_string() );
148+
map
149+
}
150+
]
151+
),
152+
},
153+
]
154+
155+
}

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

Lines changed: 53 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ use std::
2020
#[ test ]
2121
fn iterator_over_optional_cow()
2222
{
23-
use test_object_without_impl::TestObjectWithoutImpl as TestObjectWithoutImpl;
23+
use the_module::TestObjectWithoutImpl as TestObjectWithoutImpl;
2424
use the_module::
2525
{
2626
Fields,
@@ -30,90 +30,59 @@ fn iterator_over_optional_cow()
3030
OptionalCow,
3131
};
3232

33-
// xxx : that should fail
34-
impl TableWithFields for TestObjectWithoutImpl {}
35-
36-
impl Fields< &'_ str, Option< Cow< '_, str > > >
37-
for TestObjectWithoutImpl
38-
{
39-
type Key< 'k > = &'k str;
40-
type Val< 'v > = Option< Cow< 'v, str > >;
41-
42-
fn fields( &self ) -> impl IteratorTrait< Item = ( &'_ str, Option< Cow< '_, str > > ) >
43-
{
44-
use format_tools::ref_or_display_or_debug_multiline::field;
45-
// use format_tools::ref_or_display_or_debug::field;
46-
let mut dst : Vec< ( &'_ str, Option< Cow< '_, str > > ) > = Vec::new();
47-
48-
dst.push( field!( &self.id ) );
49-
dst.push( field!( &self.created_at ) );
50-
dst.push( field!( &self.file_ids ) );
51-
52-
if let Some( tools ) = &self.tools
53-
{
54-
dst.push( field!( tools ) );
55-
}
56-
else
57-
{
58-
dst.push( ( "tools", Option::None ) );
59-
}
60-
61-
dst.into_iter()
62-
}
63-
64-
}
65-
66-
let data : collection_tools::Vec< TestObjectWithoutImpl > = dlist!
67-
{
68-
TestObjectWithoutImpl
69-
{
70-
id : "1".to_string(),
71-
created_at : 1627845583,
72-
file_ids : vec![ "file1".to_string(), "file2".to_string() ],
73-
tools : None
74-
},
75-
TestObjectWithoutImpl
76-
{
77-
id : "2".to_string(),
78-
created_at : 13,
79-
file_ids : vec![ "file3".to_string(), "file4\nmore details".to_string() ],
80-
tools : Some
81-
(
82-
vec!
83-
[
84-
{
85-
let mut map = HashMap::new();
86-
map.insert( "tool1".to_string(), "value1".to_string() );
87-
map
88-
},
89-
{
90-
let mut map = HashMap::new();
91-
map.insert( "tool2".to_string(), "value2".to_string() );
92-
map
93-
}
94-
]
95-
),
96-
},
97-
};
98-
99-
use the_module::TableFormatter;
100-
let _as_table : AsTable< '_, Vec< TestObjectWithoutImpl >, &str, TestObjectWithoutImpl, str> = AsTable::new( &data );
101-
let as_table = AsTable::new( &data );
102-
103-
let rows = TableRows::rows( &as_table );
104-
assert_eq!( rows.len(), 2 );
33+
// // xxx : that should fail
34+
// impl TableWithFields for TestObjectWithoutImpl {}
35+
//
36+
// impl Fields< &'_ str, Option< Cow< '_, str > > >
37+
// for TestObjectWithoutImpl
38+
// {
39+
// type Key< 'k > = &'k str;
40+
// type Val< 'v > = Option< Cow< 'v, str > >;
41+
//
42+
// fn fields( &self ) -> impl IteratorTrait< Item = ( &'_ str, Option< Cow< '_, str > > ) >
43+
// {
44+
// use format_tools::ref_or_display_or_debug_multiline::field;
45+
// // use format_tools::ref_or_display_or_debug::field;
46+
// let mut dst : Vec< ( &'_ str, Option< Cow< '_, str > > ) > = Vec::new();
47+
//
48+
// dst.push( field!( &self.id ) );
49+
// dst.push( field!( &self.created_at ) );
50+
// dst.push( field!( &self.file_ids ) );
51+
//
52+
// if let Some( tools ) = &self.tools
53+
// {
54+
// dst.push( field!( tools ) );
55+
// }
56+
// else
57+
// {
58+
// dst.push( ( "tools", Option::None ) );
59+
// }
60+
//
61+
// dst.into_iter()
62+
// }
63+
//
64+
// }
10565

106-
let mut output = String::new();
107-
let mut context = the_module::print::Context::new( &mut output, Default::default() );
108-
let _got = the_module::TableFormatter::fmt( &as_table, &mut context );
109-
let got = as_table.table_to_string();
110-
assert!( got.contains( "│ id │ created_at │ file_ids │ tools │" ) );
111-
assert!( got.contains( "│ 13 │ [ │ [ │" ) );
112-
assert!( got.contains( "│ 1627845583 │ [ │ │" ) );
66+
let data : collection_tools::Vec< TestObjectWithoutImpl > = the_module::test_objects_gen();
11367

114-
let got = AsTable::new( &data ).table_to_string();
115-
assert!( got.contains( "│ id │ created_at │ file_ids │ tools │" ) );
116-
assert!( got.contains( "│ 13 │ [ │ [ │" ) );
117-
assert!( got.contains( "│ 1627845583 │ [ │ │" ) );
68+
// use the_module::TableFormatter;
69+
// let _as_table : AsTable< '_, Vec< TestObjectWithoutImpl >, &str, TestObjectWithoutImpl, str> = AsTable::new( &data );
70+
// let as_table = AsTable::new( &data );
71+
//
72+
// let rows = TableRows::rows( &as_table );
73+
// assert_eq!( rows.len(), 2 );
74+
//
75+
// let mut output = String::new();
76+
// let mut context = the_module::print::Context::new( &mut output, Default::default() );
77+
// let _got = the_module::TableFormatter::fmt( &as_table, &mut context );
78+
// let got = as_table.table_to_string();
79+
// assert!( got.contains( "│ id │ created_at │ file_ids │ tools │" ) );
80+
// assert!( got.contains( "│ 13 │ [ │ [ │" ) );
81+
// assert!( got.contains( "│ 1627845583 │ [ │ │" ) );
82+
//
83+
// let got = AsTable::new( &data ).table_to_string();
84+
// assert!( got.contains( "│ id │ created_at │ file_ids │ tools │" ) );
85+
// assert!( got.contains( "│ 13 │ [ │ [ │" ) );
86+
// assert!( got.contains( "│ 1627845583 │ [ │ │" ) );
11887

11988
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,7 @@ fn iterator_over_strings()
239239

240240
}
241241

242-
let data : collection_tools::Vec< TestObject3 > = dlist!
242+
let _data : collection_tools::Vec< TestObject3 > = dlist!
243243
{
244244
TestObject3
245245
{

0 commit comments

Comments
 (0)