Skip to content

Commit dc7b822

Browse files
committed
fixing format_tools
1 parent d1f2ed7 commit dc7b822

File tree

4 files changed

+229
-29
lines changed

4 files changed

+229
-29
lines changed

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

Lines changed: 185 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -49,20 +49,70 @@ fn basic()
4949
//
5050

5151
#[ test ]
52-
fn dlist_basic()
52+
fn iterator_over_optional_cow()
5353
{
54-
use test_object::TestObject;
54+
// use test_object::TestObject2 as TestObject2;
55+
use the_module::
56+
{
57+
Fields,
58+
IteratorTrait,
59+
TableWithFields,
60+
WithRef,
61+
OptionalCow,
62+
};
63+
64+
/// Struct representing a test object with various fields.
65+
#[ derive( Clone, Debug, PartialEq, Eq ) ]
66+
pub struct TestObject2
67+
{
68+
pub id : String,
69+
pub created_at : i64,
70+
pub file_ids : Vec< String >,
71+
pub tools : Option< Vec< HashMap< String, String > > >,
72+
}
73+
74+
impl TableWithFields for TestObject2 {}
5575

56-
let data : collection_tools::Vec< TestObject > = dlist!
76+
impl Fields< &'_ str, OptionalCow< '_, str, WithRef > >
77+
for TestObject2
5778
{
58-
TestObject
79+
type Key< 'k > = &'k str;
80+
type Val< 'v > = OptionalCow< 'v, str, WithRef >;
81+
82+
fn fields( &self ) -> impl IteratorTrait< Item = ( &'_ str, OptionalCow< '_, str, WithRef > ) >
83+
{
84+
use format_tools::ref_or_display_or_debug_multiline::field;
85+
// use format_tools::ref_or_display_or_debug::field;
86+
let mut dst : Vec< ( &'_ str, OptionalCow< '_, str, WithRef > ) > = Vec::new();
87+
88+
dst.push( field!( &self.id ) );
89+
dst.push( field!( &self.created_at ) );
90+
dst.push( field!( &self.file_ids ) );
91+
92+
if let Some( tools ) = &self.tools
93+
{
94+
dst.push( field!( tools ) );
95+
}
96+
else
97+
{
98+
dst.push( ( "tools", OptionalCow::none() ) );
99+
}
100+
101+
dst.into_iter()
102+
}
103+
104+
}
105+
106+
let data : collection_tools::Vec< TestObject2 > = dlist!
107+
{
108+
TestObject2
59109
{
60110
id : "1".to_string(),
61111
created_at : 1627845583,
62112
file_ids : vec![ "file1".to_string(), "file2".to_string() ],
63113
tools : None
64114
},
65-
TestObject
115+
TestObject2
66116
{
67117
id : "2".to_string(),
68118
created_at : 13,
@@ -87,7 +137,7 @@ fn dlist_basic()
87137
};
88138

89139
use the_module::TableFormatter;
90-
let _as_table : AsTable< '_, Vec< TestObject >, &str, TestObject, str, WithRef > = AsTable::new( &data );
140+
let _as_table : AsTable< '_, Vec< TestObject2 >, &str, TestObject2, str, WithRef > = AsTable::new( &data );
91141
let as_table = AsTable::new( &data );
92142

93143
let rows = TableRows::rows( &as_table );
@@ -101,9 +151,134 @@ fn dlist_basic()
101151
assert!( got.contains( "│ 13 │ [ │ [ │" ) );
102152
assert!( got.contains( "│ 1627845583 │ [ │ │" ) );
103153

104-
// let got = AsTable::new( &data ).table_to_string();
105-
// assert!( got.contains( "│ id │ created_at │ file_ids │ tools │" ) );
106-
// assert!( got.contains( "│ 13 │ [ │ [ │" ) );
107-
// assert!( got.contains( "│ 1627845583 │ [ │ │" ) );
154+
let got = AsTable::new( &data ).table_to_string();
155+
assert!( got.contains( "│ id │ created_at │ file_ids │ tools │" ) );
156+
assert!( got.contains( "│ 13 │ [ │ [ │" ) );
157+
assert!( got.contains( "│ 1627845583 │ [ │ │" ) );
158+
159+
}
160+
161+
//
162+
163+
#[ test ]
164+
fn iterator_over_strings()
165+
{
166+
167+
// fn to_owned< 'a, T1, T2 : Copy >( src : ( T1, OptionalCow< 'a, str, T2 > ) ) -> ( T1, String )
168+
// {
169+
// ( src.0, src.1.into_owned() )
170+
// }
171+
172+
fn into< 'a, T1, T2 : Copy >( src : ( T1, OptionalCow< 'a, str, T2 > ) ) -> ( T1, Option< Cow< 'a, str > > )
173+
{
174+
( src.0, src.1.into() )
175+
}
176+
177+
// use test_object::TestObject as TestObject3;
178+
use the_module::
179+
{
180+
Fields,
181+
IteratorTrait,
182+
TableWithFields,
183+
WithRef,
184+
OptionalCow,
185+
};
186+
187+
use std::borrow::Cow;
188+
189+
/// Struct representing a test object with various fields.
190+
#[ derive( Clone, Debug, PartialEq, Eq ) ]
191+
pub struct TestObject3
192+
{
193+
pub id : String,
194+
pub created_at : i64,
195+
pub file_ids : Vec< String >,
196+
pub tools : Option< Vec< HashMap< String, String > > >,
197+
}
198+
199+
impl TableWithFields for TestObject3 {}
200+
201+
impl Fields< &'_ str, String >
202+
for TestObject3
203+
{
204+
type Key< 'k > = &'k str;
205+
type Val< 'v > = Option< Cow< 'v, str > >;
206+
207+
fn fields( &self ) -> impl IteratorTrait< Item = ( &'_ str, Option< Cow< '_, str > > ) >
208+
{
209+
use format_tools::ref_or_display_or_debug_multiline::field;
210+
// use format_tools::ref_or_display_or_debug::field;
211+
let mut dst : Vec< ( &'_ str, Option< Cow< '_, str > > ) > = Vec::new();
212+
213+
dst.push( into( field!( &self.id ) ) );
214+
// dst.push( field!( &self.created_at ) );
215+
// dst.push( field!( &self.file_ids ) );
216+
//
217+
// if let Some( tools ) = &self.tools
218+
// {
219+
// dst.push( field!( tools ) );
220+
// }
221+
// else
222+
// {
223+
// dst.push( ( "tools", OptionalCow::none() ) );
224+
// }
225+
226+
dst.into_iter()
227+
}
228+
229+
}
230+
231+
// let data : collection_tools::Vec< TestObject3 > = dlist!
232+
// {
233+
// TestObject3
234+
// {
235+
// id : "1".to_string(),
236+
// created_at : 1627845583,
237+
// file_ids : vec![ "file1".to_string(), "file2".to_string() ],
238+
// tools : None
239+
// },
240+
// TestObject3
241+
// {
242+
// id : "2".to_string(),
243+
// created_at : 13,
244+
// file_ids : vec![ "file3".to_string(), "file4\nmore details".to_string() ],
245+
// tools : Some
246+
// (
247+
// vec!
248+
// [
249+
// {
250+
// let mut map = HashMap::new();
251+
// map.insert( "tool1".to_string(), "value1".to_string() );
252+
// map
253+
// },
254+
// {
255+
// let mut map = HashMap::new();
256+
// map.insert( "tool2".to_string(), "value2".to_string() );
257+
// map
258+
// }
259+
// ]
260+
// ),
261+
// },
262+
// };
263+
//
264+
// use the_module::TableFormatter;
265+
// let _as_table : AsTable< '_, Vec< TestObject3 >, &str, TestObject3, str, WithRef > = AsTable::new( &data );
266+
// let as_table = AsTable::new( &data );
267+
//
268+
// let rows = TableRows::rows( &as_table );
269+
// assert_eq!( rows.len(), 2 );
270+
//
271+
// let mut output = String::new();
272+
// let mut context = the_module::print::Context::new( &mut output, Default::default() );
273+
// let _got = the_module::TableFormatter::fmt( &as_table, &mut context );
274+
// let got = as_table.table_to_string();
275+
// assert!( got.contains( "│ id │ created_at │ file_ids │ tools │" ) );
276+
// assert!( got.contains( "│ 13 │ [ │ [ │" ) );
277+
// assert!( got.contains( "│ 1627845583 │ [ │ │" ) );
278+
//
279+
// let got = AsTable::new( &data ).table_to_string();
280+
// assert!( got.contains( "│ id │ created_at │ file_ids │ tools │" ) );
281+
// assert!( got.contains( "│ 13 │ [ │ [ │" ) );
282+
// assert!( got.contains( "│ 1627845583 │ [ │ │" ) );
108283

109284
}

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

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ use std::
2020
};
2121

2222
/// Struct representing a test object with various fields.
23-
#[ derive( Clone, Debug ) ]
23+
#[ derive( Clone, Debug, PartialEq, Eq ) ]
2424
pub struct TestObject
2525
{
2626
pub id : String,
@@ -89,22 +89,22 @@ impl Hash for TestObject
8989

9090
}
9191

92-
impl PartialEq for TestObject
93-
{
94-
95-
fn eq( &self, other: &Self ) -> bool
96-
{
97-
self.id == other.id &&
98-
self.created_at == other.created_at &&
99-
self.file_ids == other.file_ids &&
100-
self.tools == other.tools
101-
}
102-
103-
}
104-
105-
impl Eq for TestObject
106-
{
107-
}
92+
// impl PartialEq for TestObject
93+
// {
94+
//
95+
// fn eq( &self, other: &Self ) -> bool
96+
// {
97+
// self.id == other.id &&
98+
// self.created_at == other.created_at &&
99+
// self.file_ids == other.file_ids &&
100+
// self.tools == other.tools
101+
// }
102+
//
103+
// }
104+
//
105+
// impl Eq for TestObject
106+
// {
107+
// }
108108

109109
impl PartialOrd for TestObject
110110
{

module/core/reflect_tools/src/reflect/wrapper.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ mod private
77
{
88
}
99

10-
mod maybe_as;
10+
mod optional_cow;
1111

1212
#[ doc( inline ) ]
1313
#[ allow( unused_imports ) ]
@@ -40,7 +40,7 @@ pub mod exposed
4040
#[ allow( unused_imports ) ]
4141
pub use super::
4242
{
43-
maybe_as::OptionalCow,
43+
optional_cow::OptionalCow,
4444
};
4545
}
4646

module/core/reflect_tools/src/reflect/wrapper/maybe_as.rs renamed to module/core/reflect_tools/src/reflect/wrapper/optional_cow.rs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,19 @@ where
2020
Marker : Clone + Copy + 'static,
2121
{
2222

23+
// Creates owned data from borrowed data, usually by cloning.
24+
#[ inline( always ) ]
25+
pub fn into_owned( &self ) -> < T as std::borrow::ToOwned >::Owned
26+
where
27+
< T as std::borrow::ToOwned >::Owned : Default,
28+
{
29+
match self.0.as_ref()
30+
{
31+
Some( c ) => c.clone().into_owned(),
32+
None => < T as std::borrow::ToOwned >::Owned::default(),
33+
}
34+
}
35+
2336
/// Check is it borrowed.
2437
#[ inline( always ) ]
2538
pub fn is_borrowed( &self ) -> bool
@@ -212,3 +225,15 @@ where
212225
T : Eq,
213226
{
214227
}
228+
229+
impl< 'a, T, Marker > From< OptionalCow< 'a, T, Marker > > for Option< Cow< 'a, T > >
230+
where
231+
T : std::borrow::ToOwned + ?Sized,
232+
Marker : Clone + Copy + 'static,
233+
{
234+
#[ inline( always ) ]
235+
fn from( src : OptionalCow< 'a, T, Marker > ) -> Self
236+
{
237+
src.0
238+
}
239+
}

0 commit comments

Comments
 (0)