Skip to content

Commit dbb9b6c

Browse files
jcdyerzonyitoo
authored andcommitted
Add mutable accessors for Bson variants. (#123)
This includes both a full set of as_<type>_mut methods on the Bson type, as well as get_<type>_mut methods on the OrderedDocument type. Copy types also implement these methods, to facilitate the pattern of modifying a document by using: let mut bson_doc = doc!{"number": 14}; let number = bson_doc.get_i32_mut("number"); *number = 15; println!("{}", bson_doc); // { number: 15 } Bson::as_null is not given a mutable alternative, as there is only one possible unit value.
1 parent d8485d6 commit dbb9b6c

File tree

2 files changed

+204
-5
lines changed

2 files changed

+204
-5
lines changed

src/bson.rs

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -492,6 +492,14 @@ impl Bson {
492492
}
493493
}
494494

495+
/// If `Bson` is `FloatingPoint`, return a mutable reference to its value. Returns `None` otherwise
496+
pub fn as_f64_mut(&mut self) -> Option<&mut f64> {
497+
match *self {
498+
Bson::FloatingPoint(ref mut v) => Some(v),
499+
_ => None,
500+
}
501+
}
502+
495503
/// If `Bson` is `String`, return its value. Returns `None` otherwise
496504
pub fn as_str(&self) -> Option<&str> {
497505
match *self {
@@ -500,6 +508,14 @@ impl Bson {
500508
}
501509
}
502510

511+
/// If `Bson` is `String`, return a mutable reference to its value. Returns `None` otherwise
512+
pub fn as_str_mut(&mut self) -> Option<&mut str> {
513+
match *self {
514+
Bson::String(ref mut s) => Some(s),
515+
_ => None,
516+
}
517+
}
518+
503519
/// If `Bson` is `Array`, return its value. Returns `None` otherwise
504520
pub fn as_array(&self) -> Option<&Array> {
505521
match *self {
@@ -508,6 +524,14 @@ impl Bson {
508524
}
509525
}
510526

527+
/// If `Bson` is `Array`, return a mutable reference to its value. Returns `None` otherwise
528+
pub fn as_array_mut(&mut self) -> Option<&mut Array> {
529+
match *self {
530+
Bson::Array(ref mut v) => Some(v),
531+
_ => None,
532+
}
533+
}
534+
511535
/// If `Bson` is `Document`, return its value. Returns `None` otherwise
512536
pub fn as_document(&self) -> Option<&Document> {
513537
match *self {
@@ -516,6 +540,14 @@ impl Bson {
516540
}
517541
}
518542

543+
/// If `Bson` is `Document`, return a mutable reference to its value. Returns `None` otherwise
544+
pub fn as_document_mut(&mut self) -> Option<&mut Document> {
545+
match *self {
546+
Bson::Document(ref mut v) => Some(v),
547+
_ => None,
548+
}
549+
}
550+
519551
/// If `Bson` is `Boolean`, return its value. Returns `None` otherwise
520552
pub fn as_bool(&self) -> Option<bool> {
521553
match *self {
@@ -524,6 +556,14 @@ impl Bson {
524556
}
525557
}
526558

559+
/// If `Bson` is `Boolean`, return a mutable reference to its value. Returns `None` otherwise
560+
pub fn as_bool_mut(&mut self) -> Option<&mut bool> {
561+
match *self {
562+
Bson::Boolean(ref mut v) => Some(v),
563+
_ => None,
564+
}
565+
}
566+
527567
/// If `Bson` is `I32`, return its value. Returns `None` otherwise
528568
pub fn as_i32(&self) -> Option<i32> {
529569
match *self {
@@ -532,6 +572,14 @@ impl Bson {
532572
}
533573
}
534574

575+
/// If `Bson` is `I32`, return a mutable reference to its value. Returns `None` otherwise
576+
pub fn as_i32_mut(&mut self) -> Option<&mut i32> {
577+
match *self {
578+
Bson::I32(ref mut v) => Some(v),
579+
_ => None,
580+
}
581+
}
582+
535583
/// If `Bson` is `I64`, return its value. Returns `None` otherwise
536584
pub fn as_i64(&self) -> Option<i64> {
537585
match *self {
@@ -540,6 +588,14 @@ impl Bson {
540588
}
541589
}
542590

591+
/// If `Bson` is `I64`, return a mutable reference to its value. Returns `None` otherwise
592+
pub fn as_i64_mut(&mut self) -> Option<&mut i64> {
593+
match *self {
594+
Bson::I64(ref mut v) => Some(v),
595+
_ => None,
596+
}
597+
}
598+
543599
/// If `Bson` is `Objectid`, return its value. Returns `None` otherwise
544600
pub fn as_object_id(&self) -> Option<&oid::ObjectId> {
545601
match *self {
@@ -548,6 +604,14 @@ impl Bson {
548604
}
549605
}
550606

607+
/// If `Bson` is `Objectid`, return a mutable reference to its value. Returns `None` otherwise
608+
pub fn as_object_id_mut(&mut self) -> Option<&mut oid::ObjectId> {
609+
match *self {
610+
Bson::ObjectId(ref mut v) => Some(v),
611+
_ => None,
612+
}
613+
}
614+
551615
/// If `Bson` is `UtcDateTime`, return its value. Returns `None` otherwise
552616
pub fn as_utc_date_time(&self) -> Option<&DateTime<Utc>> {
553617
match *self {
@@ -556,6 +620,14 @@ impl Bson {
556620
}
557621
}
558622

623+
/// If `Bson` is `UtcDateTime`, return a mutable reference to its value. Returns `None` otherwise
624+
pub fn as_utc_date_time_mut(&mut self) -> Option<&mut DateTime<Utc>> {
625+
match *self {
626+
Bson::UtcDatetime(ref mut v) => Some(v),
627+
_ => None,
628+
}
629+
}
630+
559631
/// If `Bson` is `Symbol`, return its value. Returns `None` otherwise
560632
pub fn as_symbol(&self) -> Option<&str> {
561633
match *self {
@@ -564,6 +636,14 @@ impl Bson {
564636
}
565637
}
566638

639+
/// If `Bson` is `Symbol`, return a mutable reference to its value. Returns `None` otherwise
640+
pub fn as_symbol_mut(&mut self) -> Option<&mut str> {
641+
match *self {
642+
Bson::Symbol(ref mut v) => Some(v),
643+
_ => None,
644+
}
645+
}
646+
567647
/// If `Bson` is `TimeStamp`, return its value. Returns `None` otherwise
568648
pub fn as_timestamp(&self) -> Option<i64> {
569649
match *self {
@@ -572,6 +652,14 @@ impl Bson {
572652
}
573653
}
574654

655+
/// If `Bson` is `TimeStamp`, return a mutable reference to its value. Returns `None` otherwise
656+
pub fn as_timestamp_mut(&mut self) -> Option<&mut i64> {
657+
match *self {
658+
Bson::TimeStamp(ref mut v) => Some(v),
659+
_ => None,
660+
}
661+
}
662+
575663
/// If `Bson` is `Null`, return its value. Returns `None` otherwise
576664
pub fn as_null(&self) -> Option<()> {
577665
match *self {

src/ordered.rs

Lines changed: 116 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,17 @@ impl OrderedDocument {
207207
}
208208
}
209209

210-
/// Get Decimal128 value for key, if it exists.
210+
/// Get a mutable reference to a floating point value for this key if it exists and has
211+
/// the correct type.
212+
pub fn get_f64_mut(&mut self, key: &str) -> ValueAccessResult<&mut f64> {
213+
match self.get_mut(key) {
214+
Some(&mut Bson::FloatingPoint(ref mut v)) => Ok(v),
215+
Some(_) => Err(ValueAccessError::UnexpectedType),
216+
None => Err(ValueAccessError::NotPresent),
217+
}
218+
}
219+
220+
/// Get a reference to a Decimal128 value for key, if it exists.
211221
pub fn get_decimal128(&self, key: &str) -> ValueAccessResult<&Decimal128> {
212222
match self.get(key) {
213223
Some(&Bson::Decimal128(ref v)) => Ok(v),
@@ -216,6 +226,15 @@ impl OrderedDocument {
216226
}
217227
}
218228

229+
/// Get a mutable reference to a Decimal128 value for key, if it exists.
230+
pub fn get_decimal128_mut(&mut self, key: &str) -> ValueAccessResult<&mut Decimal128> {
231+
match self.get_mut(key) {
232+
Some(&mut Bson::Decimal128(ref mut v)) => Ok(v),
233+
Some(_) => Err(ValueAccessError::UnexpectedType),
234+
None => Err(ValueAccessError::NotPresent),
235+
}
236+
}
237+
219238
/// Get a string slice this key if it exists and has the correct type.
220239
pub fn get_str(&self, key: &str) -> ValueAccessResult<&str> {
221240
match self.get(key) {
@@ -225,6 +244,15 @@ impl OrderedDocument {
225244
}
226245
}
227246

247+
/// Get a mutable string slice this key if it exists and has the correct type.
248+
pub fn get_str_mut(&mut self, key: &str) -> ValueAccessResult<&mut str> {
249+
match self.get_mut(key) {
250+
Some(&mut Bson::String(ref mut v)) => Ok(v),
251+
Some(_) => Err(ValueAccessError::UnexpectedType),
252+
None => Err(ValueAccessError::NotPresent),
253+
}
254+
}
255+
228256
/// Get a reference to an array for this key if it exists and has
229257
/// the correct type.
230258
pub fn get_array(&self, key: &str) -> ValueAccessResult<&Array> {
@@ -235,6 +263,16 @@ impl OrderedDocument {
235263
}
236264
}
237265

266+
/// Get a mutable reference to an array for this key if it exists and has
267+
/// the correct type.
268+
pub fn get_array_mut(&mut self, key: &str) -> ValueAccessResult<&mut Array> {
269+
match self.get_mut(key) {
270+
Some(&mut Bson::Array(ref mut v)) => Ok(v),
271+
Some(_) => Err(ValueAccessError::UnexpectedType),
272+
None => Err(ValueAccessError::NotPresent),
273+
}
274+
}
275+
238276
/// Get a reference to a document for this key if it exists and has
239277
/// the correct type.
240278
pub fn get_document(&self, key: &str) -> ValueAccessResult<&Document> {
@@ -245,6 +283,16 @@ impl OrderedDocument {
245283
}
246284
}
247285

286+
/// Get a mutable reference to a document for this key if it exists and has
287+
/// the correct type.
288+
pub fn get_document_mut(&mut self, key: &str) -> ValueAccessResult<&mut Document> {
289+
match self.get_mut(key) {
290+
Some(&mut Bson::Document(ref mut v)) => Ok(v),
291+
Some(_) => Err(ValueAccessError::UnexpectedType),
292+
None => Err(ValueAccessError::NotPresent),
293+
}
294+
}
295+
248296
/// Get a bool value for this key if it exists and has the correct type.
249297
pub fn get_bool(&self, key: &str) -> ValueAccessResult<bool> {
250298
match self.get(key) {
@@ -254,6 +302,15 @@ impl OrderedDocument {
254302
}
255303
}
256304

305+
/// Get a mutable reference to a bool value for this key if it exists and has the correct type.
306+
pub fn get_bool_mut(&mut self, key: &str) -> ValueAccessResult<&mut bool> {
307+
match self.get_mut(key) {
308+
Some(&mut Bson::Boolean(ref mut v)) => Ok(v),
309+
Some(_) => Err(ValueAccessError::UnexpectedType),
310+
None => Err(ValueAccessError::NotPresent),
311+
}
312+
}
313+
257314
/// Returns wether this key has a null value
258315
pub fn is_null(&self, key: &str) -> bool {
259316
self.get(key) == Some(&Bson::Null)
@@ -268,6 +325,15 @@ impl OrderedDocument {
268325
}
269326
}
270327

328+
/// Get a mutable reference to an i32 value for this key if it exists and has the correct type.
329+
pub fn get_i32_mut(&mut self, key: &str) -> ValueAccessResult<&mut i32> {
330+
match self.get_mut(key) {
331+
Some(&mut Bson::I32(ref mut v)) => Ok(v),
332+
Some(_) => Err(ValueAccessError::UnexpectedType),
333+
None => Err(ValueAccessError::NotPresent),
334+
}
335+
}
336+
271337
/// Get an i64 value for this key if it exists and has the correct type.
272338
pub fn get_i64(&self, key: &str) -> ValueAccessResult<i64> {
273339
match self.get(key) {
@@ -277,6 +343,15 @@ impl OrderedDocument {
277343
}
278344
}
279345

346+
/// Get a mutable reference to an i64 value for this key if it exists and has the correct type.
347+
pub fn get_i64_mut(&mut self, key: &str) -> ValueAccessResult<&mut i64> {
348+
match self.get_mut(key) {
349+
Some(&mut Bson::I64(ref mut v)) => Ok(v),
350+
Some(_) => Err(ValueAccessError::UnexpectedType),
351+
None => Err(ValueAccessError::NotPresent),
352+
}
353+
}
354+
280355
/// Get a time stamp value for this key if it exists and has the correct type.
281356
pub fn get_time_stamp(&self, key: &str) -> ValueAccessResult<i64> {
282357
match self.get(key) {
@@ -286,7 +361,16 @@ impl OrderedDocument {
286361
}
287362
}
288363

289-
/// Get a generic binary value for this key if it exists and has the correct type.
364+
/// Get a mutable reference to a time stamp value for this key if it exists and has the correct type.
365+
pub fn get_time_stamp_mut(&mut self, key: &str) -> ValueAccessResult<&mut i64> {
366+
match self.get_mut(key) {
367+
Some(&mut Bson::TimeStamp(ref mut v)) => Ok(v),
368+
Some(_) => Err(ValueAccessError::UnexpectedType),
369+
None => Err(ValueAccessError::NotPresent),
370+
}
371+
}
372+
373+
/// Get a reference to a generic binary value for this key if it exists and has the correct type.
290374
pub fn get_binary_generic(&self, key: &str) -> ValueAccessResult<&Vec<u8>> {
291375
match self.get(key) {
292376
Some(&Bson::Binary(BinarySubtype::Generic, ref v)) => Ok(v),
@@ -295,7 +379,16 @@ impl OrderedDocument {
295379
}
296380
}
297381

298-
/// Get an object id value for this key if it exists and has the correct type.
382+
/// Get a mutable reference generic binary value for this key if it exists and has the correct type.
383+
pub fn get_binary_generic_mut(&mut self, key: &str) -> ValueAccessResult<&mut Vec<u8>> {
384+
match self.get_mut(key) {
385+
Some(&mut Bson::Binary(BinarySubtype::Generic, ref mut v)) => Ok(v),
386+
Some(_) => Err(ValueAccessError::UnexpectedType),
387+
None => Err(ValueAccessError::NotPresent),
388+
}
389+
}
390+
391+
/// Get a reference to an object id value for this key if it exists and has the correct type.
299392
pub fn get_object_id(&self, key: &str) -> ValueAccessResult<&ObjectId> {
300393
match self.get(key) {
301394
Some(&Bson::ObjectId(ref v)) => Ok(v),
@@ -304,7 +397,16 @@ impl OrderedDocument {
304397
}
305398
}
306399

307-
/// Get a UTC datetime value for this key if it exists and has the correct type.
400+
/// Get a mutable reference to an object id value for this key if it exists and has the correct type.
401+
pub fn get_object_id_mut(&mut self, key: &str) -> ValueAccessResult<&mut ObjectId> {
402+
match self.get_mut(key) {
403+
Some(&mut Bson::ObjectId(ref mut v)) => Ok(v),
404+
Some(_) => Err(ValueAccessError::UnexpectedType),
405+
None => Err(ValueAccessError::NotPresent),
406+
}
407+
}
408+
409+
/// Get a reference to a UTC datetime value for this key if it exists and has the correct type.
308410
pub fn get_utc_datetime(&self, key: &str) -> ValueAccessResult<&DateTime<Utc>> {
309411
match self.get(key) {
310412
Some(&Bson::UtcDatetime(ref v)) => Ok(v),
@@ -313,6 +415,15 @@ impl OrderedDocument {
313415
}
314416
}
315417

418+
/// Get a mutable reference to a UTC datetime value for this key if it exists and has the correct type.
419+
pub fn get_utc_datetime_mut(&mut self, key: &str) -> ValueAccessResult<&mut DateTime<Utc>> {
420+
match self.get_mut(key) {
421+
Some(&mut Bson::UtcDatetime(ref mut v)) => Ok(v),
422+
Some(_) => Err(ValueAccessError::UnexpectedType),
423+
None => Err(ValueAccessError::NotPresent),
424+
}
425+
}
426+
316427
/// Returns true if the map contains a value for the specified key.
317428
pub fn contains_key(&self, key: &str) -> bool {
318429
self.inner.contains_key(key)
@@ -442,4 +553,4 @@ impl Extend<(String, Bson)> for OrderedDocument {
442553
self.insert(k, v);
443554
}
444555
}
445-
}
556+
}

0 commit comments

Comments
 (0)