From cabdcc0a881354de7901e7c589b29a43048d4c42 Mon Sep 17 00:00:00 2001 From: Harald Eilertsen Date: Sun, 26 Mar 2023 15:27:20 +0200 Subject: Include XChan in the Abook struct. I feel this is a better representation than what is coming directly from the API where it's all returned as one json object with fieldnames prefixed with abook or xchan respectively. --- src/abook.rs | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/src/abook.rs b/src/abook.rs index b6cd59b..d7d38cd 100644 --- a/src/abook.rs +++ b/src/abook.rs @@ -14,7 +14,7 @@ // You should have received a copy of the GNU General Public License // along with this program. If not, see . -use crate::{client::Client, error::Error}; +use crate::{client::Client, error::Error, XChan}; use serde::Deserialize; use std::convert::TryFrom; @@ -30,7 +30,10 @@ pub struct Abook { pub channel: u32, #[serde(rename = "abook_xchan")] - pub xchan: String, + pub xchan_id: String, + + #[serde(skip)] + pub xchan: XChan, #[serde(rename = "abook_my_perms")] pub my_perms: u16, @@ -142,6 +145,16 @@ impl AbookRequest { } pub async fn fetch(&self, client: &Client) -> Result, Error> { - Ok(serde_json::from_str(&self.fetch_raw(&client).await?)?) + let json: serde_json::Value = serde_json::from_str(&self.fetch_raw(&client).await?)?; + let arr = json.as_array().ok_or("Abook: Not an array")?; + let res: Vec = arr.iter() + .map(|v| { + let mut abook: Abook = serde_json::from_value(v.clone()).unwrap(); + abook.xchan = serde_json::from_value(v.clone()).unwrap(); + abook + }) + .collect::<_>(); + + Ok(res) } } -- cgit v1.2.3