File tree Expand file tree Collapse file tree 3 files changed +60
-1
lines changed Expand file tree Collapse file tree 3 files changed +60
-1
lines changed Original file line number Diff line number Diff line change @@ -23,7 +23,7 @@ rust-crypto = "0.2"
2323serde = " 1.0"
2424serde_json = { version = " 1.0.0" , features = [" preserve_order" ] }
2525time = " 0.1"
26- linked-hash-map = " 0.3 "
26+ linked-hash-map = " 0.5 "
2727hostname = " ^0.1"
2828hex = " ^0.2"
2929
Original file line number Diff line number Diff line change @@ -354,6 +354,30 @@ impl OrderedDocument {
354354 pub fn remove ( & mut self , key : & str ) -> Option < Bson > {
355355 self . inner . remove ( key)
356356 }
357+
358+ pub fn entry ( & mut self , k : String ) -> Entry {
359+ Entry {
360+ inner : self . inner . entry ( k) ,
361+ }
362+ }
363+ }
364+
365+ pub struct Entry < ' a > {
366+ inner : linked_hash_map:: Entry < ' a , String , Bson > ,
367+ }
368+
369+ impl < ' a > Entry < ' a > {
370+ pub fn key ( & self ) -> & str {
371+ self . inner . key ( )
372+ }
373+
374+ pub fn or_insert ( self , default : Bson ) -> & ' a mut Bson {
375+ self . inner . or_insert ( default)
376+ }
377+
378+ pub fn or_insert_with < F : FnOnce ( ) -> Bson > ( self , default : F ) -> & ' a mut Bson {
379+ self . inner . or_insert_with ( default)
380+ }
357381}
358382
359383impl From < LinkedHashMap < String , Bson > > for OrderedDocument {
Original file line number Diff line number Diff line change @@ -121,3 +121,38 @@ fn remove() {
121121 let keys: Vec < _ > = doc. iter ( ) . map ( |( key, _) | key. to_owned ( ) ) . collect ( ) ;
122122 assert_eq ! ( expected_keys, keys) ;
123123}
124+
125+ #[ test]
126+ fn entry ( ) {
127+ let mut doc = doc ! {
128+ "first" : 1i32 ,
129+ "second" : "foo" ,
130+ "alphanumeric" : "bar" ,
131+ } ;
132+
133+ {
134+ let first_entry = doc. entry ( "first" . to_owned ( ) ) ;
135+ assert_eq ! ( first_entry. key( ) , "first" ) ;
136+
137+ let v = first_entry. or_insert_with ( || Bson :: TimeStamp ( 27 ) ) ;
138+ assert_eq ! ( v, & mut Bson :: I32 ( 1 ) ) ;
139+ }
140+
141+ {
142+ let fourth_entry = doc. entry ( "fourth" . to_owned ( ) ) ;
143+ assert_eq ! ( fourth_entry. key( ) , "fourth" ) ;
144+
145+ let v = fourth_entry. or_insert ( Bson :: Null ) ;
146+ assert_eq ! ( v, & mut Bson :: Null ) ;
147+ }
148+
149+ assert_eq ! (
150+ doc,
151+ doc! {
152+ "first" : 1i32 ,
153+ "second" : "foo" ,
154+ "alphanumeric" : "bar" ,
155+ "fourth" : Bson :: Null ,
156+ } ,
157+ ) ;
158+ }
You can’t perform that action at this time.
0 commit comments