@@ -4,6 +4,7 @@ extern crate chrono;
44extern crate serde;
55#[ macro_use]
66extern crate serde_derive;
7+ extern crate serde_bytes;
78
89use bson:: { Bson , Decoder , Encoder } ;
910use serde:: { Deserialize , Serialize } ;
@@ -88,11 +89,10 @@ fn test_ser_datetime() {
8889 // FIXME: Due to BSON's datetime precision
8990 let now = now. with_nanosecond ( now. nanosecond ( ) / 1000000 * 1000000 ) . unwrap ( ) ;
9091
91- let foo = Foo { date : From :: from ( now) , } ;
92+ let foo = Foo { date : From :: from ( now) } ;
9293
9394 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) ) } ) ;
9696
9797 let xfoo: Foo = bson:: from_bson ( x) . unwrap ( ) ;
9898 assert_eq ! ( xfoo, foo) ;
@@ -116,21 +116,38 @@ fn test_compat_u2f() {
116116
117117#[ test]
118118fn test_byte_vec ( ) {
119- use std:: io:: Cursor ;
120-
121- #[ derive( Serialize , Deserialize , Debug , Eq , PartialEq ) ]
119+ #[ derive( Serialize , Debug , Eq , PartialEq ) ]
122120 pub struct AuthChallenge < ' a > {
121+ #[ serde( with = "serde_bytes" ) ]
123122 pub challenge : & ' a [ u8 ] ,
124123 }
125124
126- let x = AuthChallenge { challenge : b"18762b98b7c34c25bf9dc3154e4a5ca3" } ;
125+ let x = AuthChallenge { challenge : b"18762b98b7c34c25bf9dc3154e4a5ca3" , } ;
127126
128127 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 ( ) ) ) } ) ) ;
130129
131130 // let mut buf = Vec::new();
132131 // bson::encode_document(&mut buf, b.as_document().unwrap()).unwrap();
133132
134133 // let xb = bson::decode_document(&mut Cursor::new(buf)).unwrap();
135134 // assert_eq!(b.as_document().unwrap(), &xb);
136135}
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