format_tools/format/
test_object_without_impl.rs

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
14use 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 ) ]
25pub 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
67impl 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
95impl PartialOrd for TestObjectWithoutImpl
96{
97
98  fn partial_cmp( &self, other: &Self ) -> Option< Ordering >
99  {
100    Some( self.cmp( other ) )
101  }
102
103}
104
105impl 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.
119pub 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}