aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/xchan.rs33
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())
}
}