1
1
#![ allow( clippy:: blacklisted_name) ]
2
2
3
- use bson:: { bson, doc, spec:: BinarySubtype , Binary , Bson , Deserializer , Serializer } ;
3
+ use crate :: {
4
+ bson,
5
+ compat:: u2f,
6
+ doc,
7
+ from_bson,
8
+ spec:: BinarySubtype ,
9
+ tests:: LOCK ,
10
+ to_bson,
11
+ Binary ,
12
+ Bson ,
13
+ Deserializer ,
14
+ Serializer ,
15
+ } ;
4
16
use serde:: { Deserialize , Serialize } ;
5
17
use serde_json:: json;
6
18
@@ -75,13 +87,13 @@ fn test_ser_timestamp() {
75
87
} ,
76
88
} ;
77
89
78
- let x = bson :: to_bson ( & foo) . unwrap ( ) ;
90
+ let x = to_bson ( & foo) . unwrap ( ) ;
79
91
assert_eq ! (
80
92
x. as_document( ) . unwrap( ) ,
81
93
& doc! { "ts" : Bson :: Timestamp ( Timestamp { time: 0x0000_000C , increment: 0x0000_000A } ) }
82
94
) ;
83
95
84
- let xfoo: Foo = bson :: from_bson ( x) . unwrap ( ) ;
96
+ let xfoo: Foo = from_bson ( x) . unwrap ( ) ;
85
97
assert_eq ! ( xfoo, foo) ;
86
98
}
87
99
@@ -95,7 +107,7 @@ fn test_de_timestamp() {
95
107
ts : Timestamp ,
96
108
}
97
109
98
- let foo: Foo = bson :: from_bson ( Bson :: Document ( doc ! {
110
+ let foo: Foo = from_bson ( Bson :: Document ( doc ! {
99
111
"ts" : Bson :: Timestamp ( Timestamp { time: 0x0000_000C , increment: 0x0000_000A } ) ,
100
112
} ) )
101
113
. unwrap ( ) ;
@@ -128,13 +140,13 @@ fn test_ser_regex() {
128
140
regex : regex. clone ( ) ,
129
141
} ;
130
142
131
- let x = bson :: to_bson ( & foo) . unwrap ( ) ;
143
+ let x = to_bson ( & foo) . unwrap ( ) ;
132
144
assert_eq ! (
133
145
x. as_document( ) . unwrap( ) ,
134
146
& doc! { "regex" : Bson :: RegularExpression ( regex) }
135
147
) ;
136
148
137
- let xfoo: Foo = bson :: from_bson ( x) . unwrap ( ) ;
149
+ let xfoo: Foo = from_bson ( x) . unwrap ( ) ;
138
150
assert_eq ! ( xfoo, foo) ;
139
151
}
140
152
@@ -153,7 +165,7 @@ fn test_de_regex() {
153
165
options : "01" . into ( ) ,
154
166
} ;
155
167
156
- let foo: Foo = bson :: from_bson ( Bson :: Document ( doc ! {
168
+ let foo: Foo = from_bson ( Bson :: Document ( doc ! {
157
169
"regex" : Bson :: RegularExpression ( regex. clone( ) ) ,
158
170
} ) )
159
171
. unwrap ( ) ;
@@ -180,13 +192,13 @@ fn test_ser_code_with_scope() {
180
192
code_with_scope : code_with_scope. clone ( ) ,
181
193
} ;
182
194
183
- let x = bson :: to_bson ( & foo) . unwrap ( ) ;
195
+ let x = to_bson ( & foo) . unwrap ( ) ;
184
196
assert_eq ! (
185
197
x. as_document( ) . unwrap( ) ,
186
198
& doc! { "code_with_scope" : Bson :: JavaScriptCodeWithScope ( code_with_scope) }
187
199
) ;
188
200
189
- let xfoo: Foo = bson :: from_bson ( x) . unwrap ( ) ;
201
+ let xfoo: Foo = from_bson ( x) . unwrap ( ) ;
190
202
assert_eq ! ( xfoo, foo) ;
191
203
}
192
204
@@ -205,7 +217,7 @@ fn test_de_code_with_scope() {
205
217
scope : doc ! { "x" : 12 } ,
206
218
} ;
207
219
208
- let foo: Foo = bson :: from_bson ( Bson :: Document ( doc ! {
220
+ let foo: Foo = from_bson ( Bson :: Document ( doc ! {
209
221
"code_with_scope" : Bson :: JavaScriptCodeWithScope ( code_with_scope. clone( ) ) ,
210
222
} ) )
211
223
. unwrap ( ) ;
@@ -234,13 +246,13 @@ fn test_ser_datetime() {
234
246
date : From :: from ( now) ,
235
247
} ;
236
248
237
- let x = bson :: to_bson ( & foo) . unwrap ( ) ;
249
+ let x = to_bson ( & foo) . unwrap ( ) ;
238
250
assert_eq ! (
239
251
x. as_document( ) . unwrap( ) ,
240
252
& doc! { "date" : ( Bson :: DateTime ( now) ) }
241
253
) ;
242
254
243
- let xfoo: Foo = bson :: from_bson ( x) . unwrap ( ) ;
255
+ let xfoo: Foo = from_bson ( x) . unwrap ( ) ;
244
256
assert_eq ! ( xfoo, foo) ;
245
257
}
246
258
@@ -249,15 +261,15 @@ fn test_compat_u2f() {
249
261
let _guard = LOCK . run_concurrently ( ) ;
250
262
#[ derive( Serialize , Deserialize , Eq , PartialEq , Debug ) ]
251
263
struct Foo {
252
- #[ serde( with = "bson::compat:: u2f" ) ]
264
+ #[ serde( with = "u2f" ) ]
253
265
x : u32 ,
254
266
}
255
267
256
268
let foo = Foo { x : 20 } ;
257
- let b = bson :: to_bson ( & foo) . unwrap ( ) ;
269
+ let b = to_bson ( & foo) . unwrap ( ) ;
258
270
assert_eq ! ( b, Bson :: Document ( doc! { "x" : ( Bson :: Double ( 20.0 ) ) } ) ) ;
259
271
260
- let de_foo = bson :: from_bson :: < Foo > ( b) . unwrap ( ) ;
272
+ let de_foo = from_bson :: < Foo > ( b) . unwrap ( ) ;
261
273
assert_eq ! ( de_foo, foo) ;
262
274
}
263
275
@@ -276,13 +288,13 @@ fn test_binary_generic_roundtrip() {
276
288
} ) ,
277
289
} ;
278
290
279
- let b = bson :: to_bson ( & x) . unwrap ( ) ;
291
+ let b = to_bson ( & x) . unwrap ( ) ;
280
292
assert_eq ! (
281
293
b. as_document( ) . unwrap( ) ,
282
294
& doc! { "data" : Bson :: Binary ( Binary { subtype: BinarySubtype :: Generic , bytes: b"12345abcde" . to_vec( ) } ) }
283
295
) ;
284
296
285
- let f = bson :: from_bson :: < Foo > ( b) . unwrap ( ) ;
297
+ let f = from_bson :: < Foo > ( b) . unwrap ( ) ;
286
298
assert_eq ! ( x, f) ;
287
299
}
288
300
@@ -301,13 +313,13 @@ fn test_binary_non_generic_roundtrip() {
301
313
} ) ,
302
314
} ;
303
315
304
- let b = bson :: to_bson ( & x) . unwrap ( ) ;
316
+ let b = to_bson ( & x) . unwrap ( ) ;
305
317
assert_eq ! (
306
318
b. as_document( ) . unwrap( ) ,
307
319
& doc! { "data" : Bson :: Binary ( Binary { subtype: BinarySubtype :: BinaryOld , bytes: b"12345abcde" . to_vec( ) } ) }
308
320
) ;
309
321
310
- let f = bson :: from_bson :: < Foo > ( b) . unwrap ( ) ;
322
+ let f = from_bson :: < Foo > ( b) . unwrap ( ) ;
311
323
assert_eq ! ( x, f) ;
312
324
}
313
325
@@ -326,13 +338,13 @@ fn test_binary_helper_generic_roundtrip() {
326
338
} ,
327
339
} ;
328
340
329
- let b = bson :: to_bson ( & x) . unwrap ( ) ;
341
+ let b = to_bson ( & x) . unwrap ( ) ;
330
342
assert_eq ! (
331
343
b. as_document( ) . unwrap( ) ,
332
344
& doc! { "data" : Bson :: Binary ( Binary { subtype: BinarySubtype :: Generic , bytes: b"12345abcde" . to_vec( ) } ) }
333
345
) ;
334
346
335
- let f = bson :: from_bson :: < Foo > ( b) . unwrap ( ) ;
347
+ let f = from_bson :: < Foo > ( b) . unwrap ( ) ;
336
348
assert_eq ! ( x, f) ;
337
349
}
338
350
@@ -351,13 +363,13 @@ fn test_binary_helper_non_generic_roundtrip() {
351
363
} ,
352
364
} ;
353
365
354
- let b = bson :: to_bson ( & x) . unwrap ( ) ;
366
+ let b = to_bson ( & x) . unwrap ( ) ;
355
367
assert_eq ! (
356
368
b. as_document( ) . unwrap( ) ,
357
369
& doc! { "data" : Bson :: Binary ( Binary { subtype: BinarySubtype :: BinaryOld , bytes: b"12345abcde" . to_vec( ) } ) }
358
370
) ;
359
371
360
- let f = bson :: from_bson :: < Foo > ( b) . unwrap ( ) ;
372
+ let f = from_bson :: < Foo > ( b) . unwrap ( ) ;
361
373
assert_eq ! ( x, f) ;
362
374
}
363
375
@@ -374,11 +386,11 @@ fn test_byte_vec() {
374
386
challenge : b"18762b98b7c34c25bf9dc3154e4a5ca3" ,
375
387
} ;
376
388
377
- let b = bson :: to_bson ( & x) . unwrap ( ) ;
389
+ let b = to_bson ( & x) . unwrap ( ) ;
378
390
assert_eq ! (
379
391
b,
380
392
Bson :: Document (
381
- doc! { "challenge" : ( Bson :: Binary ( Binary { subtype: bson :: spec :: BinarySubtype :: Generic , bytes: x. challenge. to_vec( ) } ) ) }
393
+ doc! { "challenge" : ( Bson :: Binary ( Binary { subtype: BinarySubtype :: Generic , bytes: x. challenge. to_vec( ) } ) ) }
382
394
)
383
395
) ;
384
396
@@ -402,13 +414,13 @@ fn test_serde_bytes() {
402
414
data : b"12345abcde" . to_vec ( ) ,
403
415
} ;
404
416
405
- let b = bson :: to_bson ( & x) . unwrap ( ) ;
417
+ let b = to_bson ( & x) . unwrap ( ) ;
406
418
assert_eq ! (
407
419
b. as_document( ) . unwrap( ) ,
408
- & doc! { "data" : Bson :: Binary ( Binary { subtype: bson :: spec :: BinarySubtype :: Generic , bytes: b"12345abcde" . to_vec( ) } ) }
420
+ & doc! { "data" : Bson :: Binary ( Binary { subtype: BinarySubtype :: Generic , bytes: b"12345abcde" . to_vec( ) } ) }
409
421
) ;
410
422
411
- let f = bson :: from_bson :: < Foo > ( b) . unwrap ( ) ;
423
+ let f = from_bson :: < Foo > ( b) . unwrap ( ) ;
412
424
assert_eq ! ( x, f) ;
413
425
}
414
426
@@ -419,12 +431,12 @@ fn test_serde_newtype_struct() {
419
431
struct Email ( String ) ;
420
432
421
433
let email_1 =
Email ( String :: from ( "[email protected] " ) ) ;
422
- let b = bson :: to_bson ( & email_1) . unwrap ( ) ;
434
+ let b = to_bson ( & email_1) . unwrap ( ) ;
423
435
assert_eq ! ( b, Bson :: String ( email_1. 0 ) ) ;
424
436
425
437
let s =
String :: from ( "[email protected] " ) ;
426
438
let de = Bson :: String ( s. clone ( ) ) ;
427
- let email_2 = bson :: from_bson :: < Email > ( de) . unwrap ( ) ;
439
+ let email_2 = from_bson :: < Email > ( de) . unwrap ( ) ;
428
440
assert_eq ! ( email_2, Email ( s) ) ;
429
441
}
430
442
@@ -435,12 +447,12 @@ fn test_serde_tuple_struct() {
435
447
struct Name ( String , String ) ; // first, last
436
448
437
449
let name_1 = Name ( String :: from ( "Graydon" ) , String :: from ( "Hoare" ) ) ;
438
- let b = bson :: to_bson ( & name_1) . unwrap ( ) ;
450
+ let b = to_bson ( & name_1) . unwrap ( ) ;
439
451
assert_eq ! ( b, bson!( [ name_1. 0 . clone( ) , name_1. 1 ] ) ) ;
440
452
441
453
let ( first, last) = ( String :: from ( "Donald" ) , String :: from ( "Knuth" ) ) ;
442
454
let de = bson ! ( [ first. clone( ) , last. clone( ) ] ) ;
443
- let name_2 = bson :: from_bson :: < Name > ( de) . unwrap ( ) ;
455
+ let name_2 = from_bson :: < Name > ( de) . unwrap ( ) ;
444
456
assert_eq ! ( name_2, Name ( first, last) ) ;
445
457
}
446
458
@@ -456,12 +468,12 @@ fn test_serde_newtype_variant() {
456
468
457
469
let n = 42 ;
458
470
let num_1 = Number :: Int ( n) ;
459
- let b = bson :: to_bson ( & num_1) . unwrap ( ) ;
471
+ let b = to_bson ( & num_1) . unwrap ( ) ;
460
472
assert_eq ! ( b, bson!( { "type" : "Int" , "value" : n } ) ) ;
461
473
462
474
let x = 1337.0 ;
463
475
let de = bson ! ( { "type" : "Float" , "value" : x } ) ;
464
- let num_2 = bson :: from_bson :: < Number > ( de) . unwrap ( ) ;
476
+ let num_2 = from_bson :: < Number > ( de) . unwrap ( ) ;
465
477
assert_eq ! ( num_2, Number :: Float ( x) ) ;
466
478
}
467
479
@@ -477,12 +489,12 @@ fn test_serde_tuple_variant() {
477
489
#[ allow( clippy:: approx_constant) ]
478
490
let ( x1, y1) = ( 3.14 , -2.71 ) ;
479
491
let p1 = Point :: TwoDim ( x1, y1) ;
480
- let b = bson :: to_bson ( & p1) . unwrap ( ) ;
492
+ let b = to_bson ( & p1) . unwrap ( ) ;
481
493
assert_eq ! ( b, bson!( { "TwoDim" : [ x1, y1] } ) ) ;
482
494
483
495
let ( x2, y2, z2) = ( 0.0 , -13.37 , 4.2 ) ;
484
496
let de = bson ! ( { "ThreeDim" : [ x2, y2, z2] } ) ;
485
- let p2 = bson :: from_bson :: < Point > ( de) . unwrap ( ) ;
497
+ let p2 = from_bson :: < Point > ( de) . unwrap ( ) ;
486
498
assert_eq ! ( p2, Point :: ThreeDim ( x2, y2, z2) ) ;
487
499
}
488
500
@@ -510,13 +522,13 @@ fn test_ser_db_pointer() {
510
522
db_pointer : db_pointer. clone ( ) ,
511
523
} ;
512
524
513
- let x = bson :: to_bson ( & foo) . unwrap ( ) ;
525
+ let x = to_bson ( & foo) . unwrap ( ) ;
514
526
assert_eq ! (
515
527
x. as_document( ) . unwrap( ) ,
516
528
& doc! { "db_pointer" : Bson :: DbPointer ( db_pointer. clone( ) ) }
517
529
) ;
518
530
519
- let xfoo: Foo = bson :: from_bson ( x) . unwrap ( ) ;
531
+ let xfoo: Foo = from_bson ( x) . unwrap ( ) ;
520
532
assert_eq ! ( xfoo, foo) ;
521
533
}
522
534
@@ -539,7 +551,7 @@ fn test_de_db_pointer() {
539
551
. unwrap ( ) ;
540
552
let db_pointer = db_pointer. as_db_pointer ( ) . unwrap ( ) ;
541
553
542
- let foo: Foo = bson :: from_bson ( Bson :: Document (
554
+ let foo: Foo = from_bson ( Bson :: Document (
543
555
doc ! { "db_pointer" : Bson :: DbPointer ( db_pointer. clone( ) ) } ,
544
556
) )
545
557
. unwrap ( ) ;
0 commit comments