From d2eb399bf62782f34aadf0ea288b68c5f696a456 Mon Sep 17 00:00:00 2001 From: Jonas Platte Date: Mon, 26 Apr 2021 16:23:10 +0200 Subject: [PATCH] serde: Add &self accessor methods to CanonicalJsonValue --- ruma-serde/src/canonical_json/value.rs | 42 ++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/ruma-serde/src/canonical_json/value.rs b/ruma-serde/src/canonical_json/value.rs index 385c63db..66e61166 100644 --- a/ruma-serde/src/canonical_json/value.rs +++ b/ruma-serde/src/canonical_json/value.rs @@ -78,6 +78,48 @@ pub enum CanonicalJsonValue { Object(Object), } +impl CanonicalJsonValue { + /// If the `CanonicalJsonValue` is a `Bool`, return the inner value. + pub fn as_bool(&self) -> Option { + match self { + Self::Bool(b) => Some(*b), + _ => None, + } + } + + /// If the `CanonicalJsonValue` is an `Integer`, return the inner value. + pub fn as_integer(&self) -> Option { + match self { + Self::Integer(i) => Some(*i), + _ => None, + } + } + + /// If the `CanonicalJsonValue` is a `String`, return a reference to the inner value. + pub fn as_str(&self) -> Option<&str> { + match self { + Self::String(s) => Some(s), + _ => None, + } + } + + /// If the `CanonicalJsonValue` is an `Array`, return a reference to the inner value. + pub fn as_array(&self) -> Option<&[CanonicalJsonValue]> { + match self { + Self::Array(a) => Some(a), + _ => None, + } + } + + /// If the `CanonicalJsonValue` is an `Object`, return a reference to the inner value. + pub fn as_object(&self) -> Option<&Object> { + match self { + Self::Object(o) => Some(o), + _ => None, + } + } +} + impl Default for CanonicalJsonValue { fn default() -> Self { Self::Null