diff options
Diffstat (limited to 'src/item.rs')
-rw-r--r-- | src/item.rs | 39 |
1 files changed, 36 insertions, 3 deletions
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 } |