Skip to main content

grp_core/
json.rs

1use super::error::structs::Error;
2
3/// default implementation for deserialize a json, returning a _grp_core::Error_ directly, 
4/// very usefull for use _?_ in an already returning _Result<T, Error>_
5/// 
6/// # Example
7/// ~~~
8/// use serde::Deserialize;
9/// use grp_core::JSON;
10/// 
11/// #[derive(Deserialize, Clone, Debug, PartialEq)]
12/// pub(crate) struct Version {
13///     pub name: String,
14///     pub version: String,
15/// }
16/// 
17/// let version: Version = JSON::from_str(&"{\"name\":\"grp\",\"version\": \"v1.0.2\"}").unwrap();
18/// 
19/// assert_eq!(version, Version{name: "grp".to_string(), version: "v1.0.2".to_string()})
20/// ~~~
21/// 
22pub struct JSON;
23
24impl JSON {
25    pub fn from_str<T, S>(text: &S) -> Result<T, Error> 
26    where 
27        T: serde::de::DeserializeOwned,
28        S: AsRef<str>,
29    {
30        let text = text.as_ref();
31        serde_json::from_str(text).map_err(Error::from_serde(&text.to_string()))
32    }
33}