From 80acb66445a612798ddf74bd6df60b51fd674fa0 Mon Sep 17 00:00:00 2001 From: Jesse Luehrs Date: Sat, 23 May 2020 16:16:25 -0400 Subject: better error messages when parsing a server message --- src/json.rs | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 src/json.rs (limited to 'src/json.rs') diff --git a/src/json.rs b/src/json.rs new file mode 100644 index 0000000..2816a1f --- /dev/null +++ b/src/json.rs @@ -0,0 +1,38 @@ +use crate::prelude::*; + +pub trait DeserializeJsonWithPath { + fn json_with_path(self) -> Result; +} + +impl DeserializeJsonWithPath for String { + fn json_with_path(self) -> Result { + let jd = &mut serde_json::Deserializer::from_str(&self); + serde_path_to_error::deserialize(jd).context(crate::error::JSON) + } +} + +impl DeserializeJsonWithPath for reqwest::blocking::Response { + fn json_with_path(self) -> Result { + let bytes = self.bytes().context(crate::error::Reqwest)?; + let jd = &mut serde_json::Deserializer::from_slice(&bytes); + serde_path_to_error::deserialize(jd).context(crate::error::JSON) + } +} + +#[async_trait::async_trait] +pub trait DeserializeJsonWithPathAsync { + async fn json_with_path( + self, + ) -> Result; +} + +#[async_trait::async_trait] +impl DeserializeJsonWithPathAsync for reqwest::Response { + async fn json_with_path( + self, + ) -> Result { + let bytes = self.bytes().await.context(crate::error::Reqwest)?; + let jd = &mut serde_json::Deserializer::from_slice(&bytes); + serde_path_to_error::deserialize(jd).context(crate::error::JSON) + } +} -- cgit v1.2.3-54-g00ecf