@@ -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} 
0 commit comments