diff options
author | Harald Eilertsen <haraldei@anduin.net> | 2018-08-23 21:23:49 +0200 |
---|---|---|
committer | Harald Eilertsen <haraldei@anduin.net> | 2018-08-23 21:23:49 +0200 |
commit | a5121b3fc8fd9ae15c27aac2e93eaf081bf82290 (patch) | |
tree | 76f17711c72711bfec5e3434c24ff75bcff2d20f /src | |
parent | cc8207865b950de54fa3d20ee7b2bb9b29382c67 (diff) | |
download | rust-zotapi-a5121b3fc8fd9ae15c27aac2e93eaf081bf82290.tar.gz rust-zotapi-a5121b3fc8fd9ae15c27aac2e93eaf081bf82290.tar.bz2 rust-zotapi-a5121b3fc8fd9ae15c27aac2e93eaf081bf82290.zip |
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.
Diffstat (limited to 'src')
-rw-r--r-- | src/xchan.rs | 33 |
1 files changed, 26 insertions, 7 deletions
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<S>(&self, serializer: S) -> Result<S::Ok, S::Error> + 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<XChanSelector<'a>>, } 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<String, Error> { - self.client.fetch_stream(client::ZOTAPI_XCHAN_PATH, &self.data) + self.client.fetch_stream(client::ZOTAPI_XCHAN_PATH, &self.data.as_ref().unwrap()) } } |