aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--src/client.rs3
-rw-r--r--src/item.rs39
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
}