22use bson:: { self , Bson , oid} ;
33use bson:: spec:: BinarySubtype ;
44
5- use chrono:: { DateTime , UTC } ;
5+ use chrono:: { DateTime , Utc } ;
66use crypto:: digest:: Digest ;
77use crypto:: md5:: Md5 ;
88
@@ -80,7 +80,7 @@ pub struct GfsFile {
8080 // The filename of the document.
8181 pub name : Option < String > ,
8282 // The date the document was first stored in GridFS.
83- pub upload_date : Option < DateTime < UTC > > ,
83+ pub upload_date : Option < DateTime < Utc > > ,
8484 // The content type of the file.
8585 pub content_type : Option < String > ,
8686 // Any additional metadata provided by the user.
@@ -195,7 +195,7 @@ impl File {
195195 if self . mode == Mode :: Write {
196196 if try!( self . err_description ( ) ) . is_none ( ) {
197197 if self . doc . upload_date . is_none ( ) {
198- self . doc . upload_date = Some ( UTC :: now ( ) ) ;
198+ self . doc . upload_date = Some ( Utc :: now ( ) ) ;
199199 }
200200 self . doc . md5 = self . wsum . result_str ( ) ;
201201 try!( self . gfs . files . insert_one ( self . doc . to_bson ( ) , None ) ) ;
@@ -205,11 +205,15 @@ impl File {
205205
206206 let mut opts = IndexOptions :: new ( ) ;
207207 opts. unique = Some ( true ) ;
208- try!( self . gfs . chunks . create_index ( doc ! { "files_id" => 1 , "n" => 1 } , Some ( opts) ) ) ;
208+ try!( self . gfs . chunks . create_index (
209+ doc ! { "files_id" => 1 , "n" => 1 } ,
210+ Some ( opts) ,
211+ ) ) ;
209212 } else {
210- try!( self . gfs
211- . chunks
212- . delete_many ( doc ! { "files_id" => ( self . doc. id. clone( ) ) } , None ) ) ;
213+ try!( self . gfs . chunks . delete_many (
214+ doc ! { "files_id" => ( self . doc. id. clone( ) ) } ,
215+ None ,
216+ ) ) ;
213217 }
214218 }
215219
@@ -240,7 +244,8 @@ impl File {
240244 let mut vec_buf = Vec :: with_capacity ( buf. len ( ) ) ;
241245 vec_buf. extend ( buf. iter ( ) . cloned ( ) ) ;
242246
243- let document = doc ! {
247+ let document =
248+ doc ! {
244249 "_id" => ( try!( oid:: ObjectId :: new( ) ) ) ,
245250 "files_id" => ( self . doc. id. clone( ) ) ,
246251 "n" => n,
@@ -322,8 +327,10 @@ impl File {
322327 }
323328 } ;
324329
325- let result = arc_gfs. chunks
326- . find_one ( Some ( doc ! { "files_id" => ( id) , "n" => ( next_chunk_num) } ) , None ) ;
330+ let result = arc_gfs. chunks . find_one (
331+ Some ( doc ! { "files_id" => ( id) , "n" => ( next_chunk_num) } ) ,
332+ None ,
333+ ) ;
327334
328335 match result {
329336 Ok ( Some ( doc) ) => {
@@ -333,8 +340,10 @@ impl File {
333340 cache. err = None ;
334341 }
335342 _ => {
336- cache. err = Some ( OperationError ( String :: from ( "Chunk contained \
337- no data.") ) )
343+ cache. err = Some ( OperationError ( String :: from (
344+ "Chunk contained \
345+ no data.",
346+ ) ) )
338347 }
339348 }
340349 }
@@ -361,7 +370,10 @@ impl io::Write for File {
361370
362371 let description = try!( self . err_description ( ) ) ;
363372 if description. is_some ( ) {
364- return Err ( io:: Error :: new ( io:: ErrorKind :: Other , OperationError ( description. unwrap ( ) ) ) ) ;
373+ return Err ( io:: Error :: new (
374+ io:: ErrorKind :: Other ,
375+ OperationError ( description. unwrap ( ) ) ,
376+ ) ) ;
365377 }
366378
367379 let mut data = buf;
@@ -393,16 +405,19 @@ impl io::Write for File {
393405
394406 // If over a megabyte is being written at once, wait for the load to reduce.
395407 while self . doc . chunk_size * self . wpending . load ( Ordering :: SeqCst ) as i32 >=
396- MEGABYTE as i32 {
408+ MEGABYTE as i32
409+ {
397410 guard = match self . condvar . wait ( guard) {
398411 Ok ( guard) => guard,
399412 Err ( _) => return Err ( io:: Error :: new ( io:: ErrorKind :: Other , PoisonLockError ) ) ,
400413 } ;
401414
402415 let description = try!( self . err_description ( ) ) ;
403416 if description. is_some ( ) {
404- return Err ( io:: Error :: new ( io:: ErrorKind :: Other ,
405- OperationError ( description. unwrap ( ) ) ) ) ;
417+ return Err ( io:: Error :: new (
418+ io:: ErrorKind :: Other ,
419+ OperationError ( description. unwrap ( ) ) ,
420+ ) ) ;
406421 }
407422 }
408423
@@ -423,16 +438,19 @@ impl io::Write for File {
423438
424439 // Pending megabyte
425440 while self . doc . chunk_size * self . wpending . load ( Ordering :: SeqCst ) as i32 >=
426- MEGABYTE as i32 {
441+ MEGABYTE as i32
442+ {
427443 guard = match self . condvar . wait ( guard) {
428444 Ok ( guard) => guard,
429445 Err ( _) => return Err ( io:: Error :: new ( io:: ErrorKind :: Other , PoisonLockError ) ) ,
430446 } ;
431447
432448 let description = try!( self . err_description ( ) ) ;
433449 if description. is_some ( ) {
434- return Err ( io:: Error :: new ( io:: ErrorKind :: Other ,
435- OperationError ( description. unwrap ( ) ) ) ) ;
450+ return Err ( io:: Error :: new (
451+ io:: ErrorKind :: Other ,
452+ OperationError ( description. unwrap ( ) ) ,
453+ ) ) ;
436454 }
437455 }
438456
@@ -461,7 +479,8 @@ impl io::Write for File {
461479
462480 // Pending megabyte
463481 while self . doc . chunk_size * self . wpending . load ( Ordering :: SeqCst ) as i32 >=
464- MEGABYTE as i32 {
482+ MEGABYTE as i32
483+ {
465484 guard = match self . condvar . wait ( guard) {
466485 Ok ( guard) => guard,
467486 Err ( _) => return Err ( io:: Error :: new ( io:: ErrorKind :: Other , PoisonLockError ) ) ,
@@ -486,7 +505,10 @@ impl io::Write for File {
486505
487506 let description = try!( self . err_description ( ) ) ;
488507 if description. is_some ( ) {
489- return Err ( io:: Error :: new ( io:: ErrorKind :: Other , OperationError ( description. unwrap ( ) ) ) ) ;
508+ return Err ( io:: Error :: new (
509+ io:: ErrorKind :: Other ,
510+ OperationError ( description. unwrap ( ) ) ,
511+ ) ) ;
490512 }
491513
492514 Ok ( ( ) )
@@ -613,7 +635,8 @@ impl GfsFile {
613635
614636 /// Converts a GfsFile into a bson document.
615637 pub fn to_bson ( & self ) -> bson:: Document {
616- let mut doc = doc ! {
638+ let mut doc =
639+ doc ! {
617640 "_id" => ( self . id. clone( ) ) ,
618641 "chunkSize" => ( self . chunk_size) ,
619642 "length" => ( self . len) ,
@@ -622,19 +645,27 @@ impl GfsFile {
622645 } ;
623646
624647 if self . name . is_some ( ) {
625- doc. insert ( "filename" ,
626- Bson :: String ( self . name . as_ref ( ) . unwrap ( ) . to_owned ( ) ) ) ;
648+ doc. insert (
649+ "filename" ,
650+ Bson :: String ( self . name . as_ref ( ) . unwrap ( ) . to_owned ( ) ) ,
651+ ) ;
627652 }
628653
629654 if self . content_type . is_some ( ) {
630- doc. insert ( "contentType" ,
631- Bson :: String ( self . content_type . as_ref ( ) . unwrap ( ) . to_owned ( ) ) ) ;
655+ doc. insert (
656+ "contentType" ,
657+ Bson :: String ( self . content_type . as_ref ( ) . unwrap ( ) . to_owned ( ) ) ,
658+ ) ;
632659 }
633660
634661 if self . metadata . is_some ( ) {
635- doc. insert ( "metadata" ,
636- Bson :: Binary ( BinarySubtype :: Generic ,
637- self . metadata . as_ref ( ) . unwrap ( ) . clone ( ) ) ) ;
662+ doc. insert (
663+ "metadata" ,
664+ Bson :: Binary (
665+ BinarySubtype :: Generic ,
666+ self . metadata . as_ref ( ) . unwrap ( ) . clone ( ) ,
667+ ) ,
668+ ) ;
638669 }
639670
640671 doc
@@ -647,7 +678,9 @@ impl CachedChunk {
647678 CachedChunk {
648679 n : n,
649680 data : Vec :: new ( ) ,
650- err : Some ( Error :: DefaultError ( String :: from ( "Chunk has not yet been initialized" ) ) ) ,
681+ err : Some ( Error :: DefaultError (
682+ String :: from ( "Chunk has not yet been initialized" ) ,
683+ ) ) ,
651684 }
652685 }
653686}
0 commit comments