@@ -4,6 +4,7 @@ extern crate chrono;
4
4
extern crate serde;
5
5
#[ macro_use]
6
6
extern crate serde_derive;
7
+ extern crate serde_bytes;
7
8
8
9
use bson:: { Bson , Decoder , Encoder } ;
9
10
use serde:: { Deserialize , Serialize } ;
@@ -88,11 +89,10 @@ fn test_ser_datetime() {
88
89
// FIXME: Due to BSON's datetime precision
89
90
let now = now. with_nanosecond ( now. nanosecond ( ) / 1000000 * 1000000 ) . unwrap ( ) ;
90
91
91
- let foo = Foo { date : From :: from ( now) , } ;
92
+ let foo = Foo { date : From :: from ( now) } ;
92
93
93
94
let x = bson:: to_bson ( & foo) . unwrap ( ) ;
94
- assert_eq ! ( x. as_document( ) . unwrap( ) ,
95
- & doc! { "date" : ( Bson :: UtcDatetime ( now) ) } ) ;
95
+ assert_eq ! ( x. as_document( ) . unwrap( ) , & doc! { "date" : ( Bson :: UtcDatetime ( now) ) } ) ;
96
96
97
97
let xfoo: Foo = bson:: from_bson ( x) . unwrap ( ) ;
98
98
assert_eq ! ( xfoo, foo) ;
@@ -116,21 +116,38 @@ fn test_compat_u2f() {
116
116
117
117
#[ test]
118
118
fn test_byte_vec ( ) {
119
- use std:: io:: Cursor ;
120
-
121
- #[ derive( Serialize , Deserialize , Debug , Eq , PartialEq ) ]
119
+ #[ derive( Serialize , Debug , Eq , PartialEq ) ]
122
120
pub struct AuthChallenge < ' a > {
121
+ #[ serde( with = "serde_bytes" ) ]
123
122
pub challenge : & ' a [ u8 ] ,
124
123
}
125
124
126
- let x = AuthChallenge { challenge : b"18762b98b7c34c25bf9dc3154e4a5ca3" } ;
125
+ let x = AuthChallenge { challenge : b"18762b98b7c34c25bf9dc3154e4a5ca3" , } ;
127
126
128
127
let b = bson:: to_bson ( & x) . unwrap ( ) ;
129
- // assert_eq!(b, Bson::Document(doc! { "challenge": (Bson::Binary(bson::spec::BinarySubtype::Generic, x.challenge.clone ()))}));
128
+ assert_eq ! ( b, Bson :: Document ( doc! { "challenge" : ( Bson :: Binary ( bson:: spec:: BinarySubtype :: Generic , x. challenge. to_vec ( ) ) ) } ) ) ;
130
129
131
130
// let mut buf = Vec::new();
132
131
// bson::encode_document(&mut buf, b.as_document().unwrap()).unwrap();
133
132
134
133
// let xb = bson::decode_document(&mut Cursor::new(buf)).unwrap();
135
134
// assert_eq!(b.as_document().unwrap(), &xb);
136
135
}
136
+
137
+ #[ test]
138
+ fn test_serde_bytes ( ) {
139
+ #[ derive( Serialize , Deserialize , Debug , Eq , PartialEq ) ]
140
+ pub struct Foo {
141
+ #[ serde( with = "serde_bytes" ) ]
142
+ data : Vec < u8 > ,
143
+ }
144
+
145
+ let x = Foo { data : b"12345abcde" . to_vec ( ) , } ;
146
+
147
+ let b = bson:: to_bson ( & x) . unwrap ( ) ;
148
+ assert_eq ! ( b. as_document( ) . unwrap( ) ,
149
+ & doc! { "data" : Bson :: Binary ( bson:: spec:: BinarySubtype :: Generic , b"12345abcde" . to_vec( ) ) } ) ;
150
+
151
+ let f = bson:: from_bson :: < Foo > ( b) . unwrap ( ) ;
152
+ assert_eq ! ( x, f) ;
153
+ }
0 commit comments