From 3b6a719b8e07f52ca179a6faadfdcebde31b6a7f Mon Sep 17 00:00:00 2001 From: Jonas Platte Date: Fri, 7 May 2021 23:20:49 +0200 Subject: [PATCH] serde: Add as_array_mut and as_object_mut methods to CanonicalJsonValue --- crates/ruma-serde/src/canonical_json/value.rs | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/crates/ruma-serde/src/canonical_json/value.rs b/crates/ruma-serde/src/canonical_json/value.rs index 0b8aaa8d..ffab7760 100644 --- a/crates/ruma-serde/src/canonical_json/value.rs +++ b/crates/ruma-serde/src/canonical_json/value.rs @@ -119,6 +119,22 @@ impl CanonicalJsonValue { _ => None, } } + + /// If the `CanonicalJsonValue` is an `Array`, return a mutable reference to the inner value. + pub fn as_array_mut(&mut self) -> Option<&mut Vec> { + match self { + Self::Array(a) => Some(a), + _ => None, + } + } + + /// If the `CanonicalJsonValue` is an `Object`, return a mutable reference to the inner value. + pub fn as_object_mut(&mut self) -> Option<&mut Object> { + match self { + Self::Object(o) => Some(o), + _ => None, + } + } } impl Default for CanonicalJsonValue {