From 2dc695577e251bf852d73d1acf7fc0287e65424c Mon Sep 17 00:00:00 2001 From: Neshura Date: Mon, 6 May 2024 21:27:47 +0200 Subject: [PATCH] Further Bugfixing --- src/lemmy.rs | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/src/lemmy.rs b/src/lemmy.rs index e3a1f56..e9c0ad8 100644 --- a/src/lemmy.rs +++ b/src/lemmy.rs @@ -90,14 +90,14 @@ pub(crate) async fn login(config: &Config) -> Result { impl Lemmy { pub(crate) async fn post(&self, post: CreatePost) -> Result { let response: String = self.post_data_json("/api/v3/post", &post).await?; - let json_data: PostView = self.parse_json(&response).await?; + let json_data: PostView = self.parse_json_map(&response).await?; Ok(json_data.post.id) } async fn feature(&self, params: FeaturePost) -> Result { let response: String = self.post_data_json("/api/v3/post/feature", ¶ms).await?; - let json_data: PostView = self.parse_json(&response).await?; + let json_data: PostView = self.parse_json_map(&response).await?; Ok(json_data) } @@ -212,6 +212,17 @@ impl Lemmy { } async fn parse_json<'a, T: Deserialize<'a>>(&self, response: &'a str) -> Result { + match serde_json::from_str::(response) { + Ok(data) => Ok(data), + Err(e) => { + let err_msg = format!("{e}"); + error!(err_msg); + Err(()) + } + } + } + + async fn parse_json_map<'a, T: Deserialize<'a>>(&self, response: &'a str) -> Result { match serde_json::from_str::>(response) { Ok(mut data) => Ok(data.remove("post_view").expect("Element should be present")), Err(e) => {