From a5121b3fc8fd9ae15c27aac2e93eaf081bf82290 Mon Sep 17 00:00:00 2001 From: Harald Eilertsen Date: Thu, 23 Aug 2018 21:23:49 +0200 Subject: Use enum instead of BTreeMap to select xchan. Since it only makes sence to fetch an xchan by one of the methods (address, hash or guid) we don't need a data type that can hold more than one value. Had to implement my own serializer for it, since serde_urlencoded don't know how to serialize enums by default. --- src/xchan.rs | 33 ++++++++++++++++++++++++++------- 1 file changed, 26 insertions(+), 7 deletions(-) (limited to 'src') diff --git a/src/xchan.rs b/src/xchan.rs index 88b3cb1..d81758a 100644 --- a/src/xchan.rs +++ b/src/xchan.rs @@ -16,37 +16,56 @@ use client::{self, Client}; use error::Error; -use std::collections::BTreeMap; +use serde::{Serialize, Serializer}; + +enum XChanSelector<'a> { + Address(&'a str), + Hash(&'a str), + GUID(&'a str), +} + +impl<'a> Serialize for XChanSelector<'a> { + fn serialize(&self, serializer: S) -> Result + where + S: Serializer, + { + match *self { + XChanSelector::Address(addr) => [("address", addr)].serialize(serializer), + XChanSelector::Hash(hash) => [("hash", hash)].serialize(serializer), + XChanSelector::GUID(guid) => [("guid", guid)].serialize(serializer), + } + } +} pub struct XChanFetcher<'a> { client: &'a Client, - data: BTreeMap<&'a str, &'a str>, + data: Option>, } impl<'a> XChanFetcher<'a> { pub fn new(client: &'a Client) -> XChanFetcher<'a> { XChanFetcher { client, - data: BTreeMap::new(), + data: None, } } pub fn by_address(&mut self, addr: &'a str) -> &mut XChanFetcher<'a> { - self.data.insert("address", addr); + self.data = Some(XChanSelector::Address(addr)); self } pub fn by_hash(&mut self, hash: &'a str) -> &mut XChanFetcher<'a> { - self.data.insert("hash", hash); + self.data = Some(XChanSelector::Hash(hash)); self } pub fn by_guid(&mut self, guid: &'a str) -> &mut XChanFetcher<'a> { - self.data.insert("guid", guid); + self.data = Some(XChanSelector::GUID(guid)); self } pub fn fetch(&self) -> Result { - self.client.fetch_stream(client::ZOTAPI_XCHAN_PATH, &self.data) + self.client.fetch_stream(client::ZOTAPI_XCHAN_PATH, &self.data.as_ref().unwrap()) } } -- cgit v1.2.3