summaryrefslogtreecommitdiffstats
path: root/src/value_ext.rs
blob: 71b4175443794b312372f1da3580ba365c6e5da3 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
use crate::error::{Error, Result};
use serde_json::Value;

/// Some extensions for serde_json::Value to be used as TDLib object
pub trait ValueExt {
    /// get @type from json
    fn get_type<'a>(&'a self) -> Result<&'a str>;

    /// get @extra from json
    fn get_extra<'a>(&'a self) -> Result<&'a str>;
}

impl ValueExt for Value {
    fn get_type<'a>(&'a self) -> Result<&'a str> {
        self["@type"].as_str().ok_or(Error::HasNoTypeInJson)
    }

    fn get_extra<'a>(&'a self) -> Result<&'a str> {
        self["@extra"]
            .as_str()
            .ok_or_else(|| Error::InvalidJson("no @extra in json".into()))
    }
}