1
- use std:: { collections :: HashMap , fmt:: Display , str} ;
1
+ use std:: { fmt:: Display , str} ;
2
2
3
3
use serde:: { Deserialize , Serialize , Serializer } ;
4
4
@@ -18,7 +18,7 @@ use crate::{
18
18
#[ derive( Debug , Clone , Default ) ]
19
19
pub struct Values {
20
20
/// List of values
21
- pub ( crate ) values : HashMap < String , Value > ,
21
+ pub ( crate ) values : Vec < Value > ,
22
22
/// List of columns in the order they were added
23
23
pub ( crate ) order : Vec < String > ,
24
24
}
@@ -27,20 +27,28 @@ impl Values {
27
27
/// Create a new instance of Values
28
28
pub fn new ( ) -> Self {
29
29
Values {
30
- values : HashMap :: new ( ) ,
30
+ values : Vec :: new ( ) ,
31
31
order : Vec :: new ( ) ,
32
32
}
33
33
}
34
34
35
35
/// Push a value to the list of values
36
36
pub fn push ( & mut self , column : String , value : impl Into < Value > ) {
37
37
self . order . push ( column. clone ( ) ) ;
38
- self . values . insert ( column , value. into ( ) ) ;
38
+ self . values . push ( value. into ( ) ) ;
39
39
}
40
40
41
41
/// Get a value by index from the list of values
42
42
pub fn get ( & self , column : & String ) -> Option < & Value > {
43
- self . values . get ( column)
43
+ match self . order . iter ( ) . enumerate ( ) . find ( |( _, o) | * o == column) {
44
+ Some ( ( i, _) ) => self . values . get ( i) ,
45
+ None => None ,
46
+ }
47
+ }
48
+
49
+ /// Length / Count of the values stored
50
+ pub fn len ( & self ) -> usize {
51
+ self . values . len ( )
44
52
}
45
53
}
46
54
@@ -51,7 +59,8 @@ impl IntoIterator for Values {
51
59
fn into_iter ( self ) -> Self :: IntoIter {
52
60
self . order
53
61
. into_iter ( )
54
- . map ( move |column| self . values [ & column] . clone ( ) )
62
+ . enumerate ( )
63
+ . map ( move |( index, _) | self . values [ index] . clone ( ) )
55
64
. collect :: < Vec < Value > > ( )
56
65
. into_iter ( )
57
66
}
@@ -184,28 +193,13 @@ where
184
193
}
185
194
}
186
195
187
- impl From < & Option < String > > for Value {
188
- fn from ( value : & Option < String > ) -> Self {
189
- match value {
190
- Some ( value) => Value :: Text ( value. to_string ( ) ) ,
191
- None => Value :: Null ,
192
- }
193
- }
194
- }
195
-
196
- impl From < & Option < i32 > > for Value {
197
- fn from ( value : & Option < i32 > ) -> Self {
198
- match value {
199
- Some ( value) => Value :: Integer ( * value) ,
200
- None => Value :: Null ,
201
- }
202
- }
203
- }
204
-
205
- impl From < & Option < bool > > for Value {
206
- fn from ( value : & Option < bool > ) -> Self {
196
+ impl < T > From < & Option < T > > for Value
197
+ where
198
+ T : Into < Value > + Clone ,
199
+ {
200
+ fn from ( value : & Option < T > ) -> Self {
207
201
match value {
208
- Some ( value) => Value :: Boolean ( if * value { 1 } else { 0 } ) ,
202
+ Some ( value) => value. clone ( ) . into ( ) ,
209
203
None => Value :: Null ,
210
204
}
211
205
}
@@ -375,3 +369,17 @@ impl<'de> Deserialize<'de> for Value {
375
369
deserializer. deserialize_any ( ValueVisitor )
376
370
}
377
371
}
372
+
373
+ #[ cfg( test) ]
374
+ mod tests {
375
+ use super :: Values ;
376
+
377
+ #[ test]
378
+ fn test_values ( ) {
379
+ let mut values = Values :: new ( ) ;
380
+ values. push ( "id" . to_string ( ) , 1 ) ;
381
+ values. push ( "name" . to_string ( ) , "Bob" ) ;
382
+
383
+ assert_eq ! ( values. len( ) , 2 ) ;
384
+ }
385
+ }
0 commit comments