diff options
author | Harald Eilertsen <haraldei@anduin.net> | 2019-07-29 11:02:32 +0200 |
---|---|---|
committer | Harald Eilertsen <haraldei@anduin.net> | 2019-07-29 11:02:32 +0200 |
commit | 9806d9e094271f76c99589d252a57117d35ae7e5 (patch) | |
tree | 6e8276d68e164a076274b7b394b103fa67040dc7 /src | |
parent | bd7e83d326f3f96c6483b9668a59f261eedf9991 (diff) | |
download | rust-zotapi-9806d9e094271f76c99589d252a57117d35ae7e5.tar.gz rust-zotapi-9806d9e094271f76c99589d252a57117d35ae7e5.tar.bz2 rust-zotapi-9806d9e094271f76c99589d252a57117d35ae7e5.zip |
Abstract away type of stored data in an Item.
Currently this seems rather pointless, but will make more sense when
we need to store arrays in the Item data set.
Diffstat (limited to 'src')
-rw-r--r-- | src/client.rs | 3 | ||||
-rw-r--r-- | src/item.rs | 39 |
2 files changed, 38 insertions, 4 deletions
diff --git a/src/client.rs b/src/client.rs index e1561bf..95643a1 100644 --- a/src/client.rs +++ b/src/client.rs @@ -128,7 +128,8 @@ impl Client { handle_result(res) } - pub fn post_multipart(&self, path: &str, data: &BTreeMap<&str, &str>, files: &Vec<&str>) -> Result<String, Error> + pub fn post_multipart<T>(&self, path: &str, data: &BTreeMap<&str, T>, files: &Vec<&str>) -> Result<String, Error> + where T: ToString, { let url = dbg!(self.url(path, &())); let mut form = reqwest::multipart::Form::new(); diff --git a/src/item.rs b/src/item.rs index 510918f..09ec4c0 100644 --- a/src/item.rs +++ b/src/item.rs @@ -18,8 +18,41 @@ use crate::{ client::{self, Client}, error::Error, }; +use serde::{Serialize, Serializer}; use std::collections::BTreeMap; + +/// Data type for values that an Item can hold. +pub enum ItemData<'a> { + /// A single value, either textual or numeric. + Value(&'a str), +} + +impl<'a> From<&'a str> for ItemData<'a> { + fn from(orig: &'a str) -> ItemData<'a> { + ItemData::Value(orig) + } +} + +impl<'a> Serialize for ItemData<'a> { + fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error> + where + S: Serializer + { + match &self { + ItemData::Value(s) => s.serialize(serializer), + } + } +} + +impl<'a> ToString for ItemData<'a> { + fn to_string(&self) -> String { + match &self { + ItemData::Value(s) => s.to_string(), + } + } +} + /// A structure to help you create an item in a declarative way. /// /// Typical usage: @@ -35,7 +68,7 @@ use std::collections::BTreeMap; /// ``` pub struct ItemBuilder<'a> { client : &'a Client, - data : BTreeMap<&'a str, &'a str>, + data : BTreeMap<&'a str, ItemData<'a>>, files: Vec<&'a str>, } @@ -50,13 +83,13 @@ impl<'a> ItemBuilder<'a> { /// Add a title to the post pub fn title(&mut self, text: &'a str) -> &mut ItemBuilder<'a> { - self.data.insert("title", text); + self.data.insert("title", ItemData::Value(text)); self } /// Add the body of the post pub fn body(&mut self, text: &'a str) -> &mut ItemBuilder<'a> { - self.data.insert("body", text); + self.data.insert("body", ItemData::Value(text)); self } |