@@ -112,112 +112,142 @@ impl<W: CrdtWriter> CrdtWrite<W> for Any {
112
112
}
113
113
}
114
114
115
- // TODO: impl for Any::Undefined
116
- impl From < String > for Any {
117
- fn from ( s : String ) -> Self {
118
- Any :: String ( s)
115
+ impl Any {
116
+ fn read_key_value < R : CrdtReader > ( reader : & mut R ) -> JwstCodecResult < ( String , Any ) > {
117
+ let key = reader. read_var_string ( ) ?;
118
+ let value = Self :: read ( reader) ?;
119
+
120
+ Ok ( ( key, value) )
119
121
}
120
- }
121
122
122
- impl From < & str > for Any {
123
- fn from ( s : & str ) -> Self {
124
- Any :: from ( s. to_string ( ) )
123
+ fn write_key_value < W : CrdtWriter > ( writer : & mut W , key : & str , value : & Any ) -> JwstCodecResult {
124
+ writer. write_var_string ( key) ?;
125
+ value. write ( writer) ?;
126
+
127
+ Ok ( ( ) )
125
128
}
126
- }
127
129
128
- impl < T : Into < Any > > From < Option < T > > for Any {
129
- fn from ( value : Option < T > ) -> Self {
130
- if let Some ( val ) = value {
131
- val . into ( )
132
- } else {
133
- Any :: Null
134
- }
130
+ pub ( crate ) fn read_multiple < R : CrdtReader > ( reader : & mut R ) -> JwstCodecResult < Vec < Any > > {
131
+ let len = reader . read_var_u64 ( ) ? ;
132
+ let any = ( 0 ..len )
133
+ . map ( |_| Any :: read ( reader ) )
134
+ . collect :: < Result < Vec < _ > , _ > > ( ) ? ;
135
+
136
+ Ok ( any )
135
137
}
136
- }
137
138
138
- impl From < u64 > for Any {
139
- fn from ( value : u64 ) -> Self {
140
- Any :: Integer ( value)
139
+ pub ( crate ) fn write_multiple < W : CrdtWriter > ( writer : & mut W , any : & [ Any ] ) -> JwstCodecResult {
140
+ writer. write_var_u64 ( any. len ( ) as u64 ) ?;
141
+ for value in any {
142
+ value. write ( writer) ?;
143
+ }
144
+
145
+ Ok ( ( ) )
141
146
}
142
147
}
143
148
144
- impl From < OrderedFloat < f32 > > for Any {
145
- fn from ( value : OrderedFloat < f32 > ) -> Self {
146
- Any :: Float32 ( value)
147
- }
149
+ macro_rules! impl_primitive_from {
150
+ ( unsigned, $( $ty: ty) ,* ) => {
151
+ $(
152
+ impl From <$ty> for Any {
153
+ fn from( value: $ty) -> Self {
154
+ Self :: Integer ( value. into( ) )
155
+ }
156
+ }
157
+ ) *
158
+ } ;
159
+ ( signed, $( $ty: ty) ,* ) => {
160
+ $(
161
+ impl From <$ty> for Any {
162
+ fn from( value: $ty) -> Self {
163
+ Self :: BigInt64 ( value. into( ) )
164
+ }
165
+ }
166
+ ) *
167
+ } ;
168
+ ( string, $( $ty: ty) ,* ) => {
169
+ $(
170
+ impl From <$ty> for Any {
171
+ fn from( value: $ty) -> Self {
172
+ Self :: String ( value. into( ) )
173
+ }
174
+ }
175
+ ) *
176
+ } ;
148
177
}
149
178
150
- impl From < OrderedFloat < f64 > > for Any {
151
- fn from ( value : OrderedFloat < f64 > ) -> Self {
152
- Any :: Float64 ( value)
179
+ impl_primitive_from ! ( unsigned, u8 , u16 , u32 , u64 ) ;
180
+ impl_primitive_from ! ( signed, i8 , i16 , i32 , i64 ) ;
181
+ impl_primitive_from ! ( string, String , & str ) ;
182
+
183
+ impl From < f32 > for Any {
184
+ fn from ( value : f32 ) -> Self {
185
+ Self :: Float32 ( value. into ( ) )
153
186
}
154
187
}
155
188
156
- impl From < i64 > for Any {
157
- fn from ( value : i64 ) -> Self {
158
- Any :: BigInt64 ( value)
189
+ impl From < f64 > for Any {
190
+ fn from ( value : f64 ) -> Self {
191
+ Self :: Float64 ( value. into ( ) )
159
192
}
160
193
}
161
194
162
195
impl From < bool > for Any {
163
196
fn from ( value : bool ) -> Self {
164
197
if value {
165
- Any :: True
198
+ Self :: True
166
199
} else {
167
- Any :: False
200
+ Self :: False
168
201
}
169
202
}
170
203
}
171
204
172
- impl From < HashMap < String , Any > > for Any {
173
- fn from ( value : HashMap < String , Any > ) -> Self {
174
- Any :: Object ( value )
205
+ impl FromIterator < Any > for Any {
206
+ fn from_iter < I : IntoIterator < Item = Any > > ( iter : I ) -> Self {
207
+ Self :: Array ( iter . into_iter ( ) . collect ( ) )
175
208
}
176
209
}
177
210
178
- impl From < Vec < Any > > for Any {
179
- fn from ( value : Vec < Any > ) -> Self {
180
- Any :: Array ( value )
211
+ impl < ' a > FromIterator < & ' a Any > for Any {
212
+ fn from_iter < I : IntoIterator < Item = & ' a Any > > ( iter : I ) -> Self {
213
+ Self :: Array ( iter . into_iter ( ) . cloned ( ) . collect ( ) )
181
214
}
182
215
}
183
216
184
- impl From < Vec < u8 > > for Any {
185
- fn from ( value : Vec < u8 > ) -> Self {
186
- Any :: Binary ( value)
217
+ impl FromIterator < ( String , Any ) > for Any {
218
+ fn from_iter < I : IntoIterator < Item = ( String , Any ) > > ( iter : I ) -> Self {
219
+ let mut map = HashMap :: new ( ) ;
220
+ map. extend ( iter) ;
221
+ Self :: Object ( map)
187
222
}
188
223
}
189
224
190
- impl Any {
191
- fn read_key_value < R : CrdtReader > ( reader : & mut R ) -> JwstCodecResult < ( String , Any ) > {
192
- let key = reader. read_var_string ( ) ?;
193
- let value = Self :: read ( reader) ?;
194
-
195
- Ok ( ( key, value) )
225
+ impl From < HashMap < String , Any > > for Any {
226
+ fn from ( value : HashMap < String , Any > ) -> Self {
227
+ Self :: Object ( value)
196
228
}
229
+ }
197
230
198
- fn write_key_value < W : CrdtWriter > ( writer : & mut W , key : & str , value : & Any ) -> JwstCodecResult {
199
- writer. write_var_string ( key) ?;
200
- value. write ( writer) ?;
201
-
202
- Ok ( ( ) )
231
+ impl From < Vec < u8 > > for Any {
232
+ fn from ( value : Vec < u8 > ) -> Self {
233
+ Self :: Binary ( value)
203
234
}
235
+ }
204
236
205
- pub ( crate ) fn read_multiple < R : CrdtReader > ( reader : & mut R ) -> JwstCodecResult < Vec < Any > > {
206
- let len = reader. read_var_u64 ( ) ?;
207
- let any = ( 0 ..len)
208
- . map ( |_| Any :: read ( reader) )
209
- . collect :: < Result < Vec < _ > , _ > > ( ) ?;
210
-
211
- Ok ( any)
237
+ impl From < & [ u8 ] > for Any {
238
+ fn from ( value : & [ u8 ] ) -> Self {
239
+ Self :: Binary ( value. into ( ) )
212
240
}
241
+ }
213
242
214
- pub ( crate ) fn write_multiple < W : CrdtWriter > ( writer : & mut W , any : & [ Any ] ) -> JwstCodecResult {
215
- writer. write_var_u64 ( any. len ( ) as u64 ) ?;
216
- for value in any {
217
- value. write ( writer) ?;
243
+ // TODO: impl for Any::Undefined
244
+ impl < T : Into < Any > > From < Option < T > > for Any {
245
+ fn from ( value : Option < T > ) -> Self {
246
+ if let Some ( val) = value {
247
+ val. into ( )
248
+ } else {
249
+ Any :: Null
218
250
}
219
-
220
- Ok ( ( ) )
221
251
}
222
252
}
223
253
@@ -310,4 +340,65 @@ mod tests {
310
340
}
311
341
}
312
342
}
343
+
344
+ #[ test]
345
+ fn test_convert_to_any ( ) {
346
+ let any: Vec < Any > = vec ! [
347
+ 42u8 . into( ) ,
348
+ 42u16 . into( ) ,
349
+ 42u32 . into( ) ,
350
+ 42u64 . into( ) ,
351
+ 114.514f32 . into( ) ,
352
+ 1919.810f64 . into( ) ,
353
+ ( -42i8 ) . into( ) ,
354
+ ( -42i16 ) . into( ) ,
355
+ ( -42i32 ) . into( ) ,
356
+ ( -42i64 ) . into( ) ,
357
+ false . into( ) ,
358
+ true . into( ) ,
359
+ "JWST" . to_string( ) . into( ) ,
360
+ "OctoBase" . into( ) ,
361
+ vec![ 1u8 , 9 , 1 , 9 ] . into( ) ,
362
+ ( & [ 8u8 , 1 , 0 ] [ ..] ) . into( ) ,
363
+ [ Any :: True , 42u8 . into( ) ] . iter( ) . collect( ) ,
364
+ ] ;
365
+ assert_eq ! (
366
+ any,
367
+ vec![
368
+ Any :: Integer ( 42 ) ,
369
+ Any :: Integer ( 42 ) ,
370
+ Any :: Integer ( 42 ) ,
371
+ Any :: Integer ( 42 ) ,
372
+ Any :: Float32 ( 114.514 . into( ) ) ,
373
+ Any :: Float64 ( 1919.810 . into( ) ) ,
374
+ Any :: BigInt64 ( -42 ) ,
375
+ Any :: BigInt64 ( -42 ) ,
376
+ Any :: BigInt64 ( -42 ) ,
377
+ Any :: BigInt64 ( -42 ) ,
378
+ Any :: False ,
379
+ Any :: True ,
380
+ Any :: String ( "JWST" . to_string( ) ) ,
381
+ Any :: String ( "OctoBase" . to_string( ) ) ,
382
+ Any :: Binary ( vec![ 1 , 9 , 1 , 9 ] ) ,
383
+ Any :: Binary ( vec![ 8 , 1 , 0 ] ) ,
384
+ Any :: Array ( vec![ Any :: True , Any :: Integer ( 42 ) ] )
385
+ ]
386
+ ) ;
387
+
388
+ assert_eq ! (
389
+ vec![ ( "key" . to_string( ) , 10u64 . into( ) ) ]
390
+ . into_iter( )
391
+ . collect:: <Any >( ) ,
392
+ Any :: Object ( HashMap :: from_iter( vec![ (
393
+ "key" . to_string( ) ,
394
+ Any :: Integer ( 10 )
395
+ ) ] ) )
396
+ ) ;
397
+
398
+ let any: Any = 10u64 . into ( ) ;
399
+ assert_eq ! (
400
+ [ any] . iter( ) . collect:: <Any >( ) ,
401
+ Any :: Array ( vec![ Any :: Integer ( 10 ) ] )
402
+ ) ;
403
+ }
313
404
}
0 commit comments