diff options
author | Harald Eilertsen <haraldei@anduin.net> | 2023-03-26 15:27:20 +0200 |
---|---|---|
committer | Harald Eilertsen <haraldei@anduin.net> | 2023-03-26 15:27:20 +0200 |
commit | cabdcc0a881354de7901e7c589b29a43048d4c42 (patch) | |
tree | 9041cdb600d0107922f0a898dfa8434e65bd5eed /src | |
parent | 4582390ca55ce440f365d1de19a5971f27d48a9b (diff) | |
download | rust-zotapi-cabdcc0a881354de7901e7c589b29a43048d4c42.tar.gz rust-zotapi-cabdcc0a881354de7901e7c589b29a43048d4c42.tar.bz2 rust-zotapi-cabdcc0a881354de7901e7c589b29a43048d4c42.zip |
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.
Diffstat (limited to 'src')
-rw-r--r-- | src/abook.rs | 19 |
1 files 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 <https://www.gnu.org/licenses/>. -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<Vec<Abook>, 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<Abook> = 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) } } |