serde: Add &self accessor methods to CanonicalJsonValue

This commit is contained in:
Jonas Platte 2021-04-26 16:23:10 +02:00
parent 28a9083a26
commit d2eb399bf6
No known key found for this signature in database
GPG Key ID: CC154DE0E30B7C67

View File

@ -78,6 +78,48 @@ pub enum CanonicalJsonValue {
Object(Object), Object(Object),
} }
impl CanonicalJsonValue {
/// If the `CanonicalJsonValue` is a `Bool`, return the inner value.
pub fn as_bool(&self) -> Option<bool> {
match self {
Self::Bool(b) => Some(*b),
_ => None,
}
}
/// If the `CanonicalJsonValue` is an `Integer`, return the inner value.
pub fn as_integer(&self) -> Option<Int> {
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 { impl Default for CanonicalJsonValue {
fn default() -> Self { fn default() -> Self {
Self::Null Self::Null