From 47d56c7ddd7dbea1577905fdbcedef3ab49153db Mon Sep 17 00:00:00 2001 From: Temirkhan Myrzamadi Date: Tue, 3 Sep 2019 13:01:36 +0600 Subject: [PATCH] Use serde::Serialize instead of Display (helpers.rs) --- src/core/requests/helpers.rs | 11 ++++++----- src/core/requests/mod.rs | 3 ++- src/core/requests/send_message.rs | 8 ++++---- 3 files changed, 12 insertions(+), 10 deletions(-) diff --git a/src/core/requests/helpers.rs b/src/core/requests/helpers.rs index 401dfd96..8523133b 100644 --- a/src/core/requests/helpers.rs +++ b/src/core/requests/helpers.rs @@ -1,13 +1,14 @@ use reqwest::r#async::multipart::Form; -use std::fmt::Display; +use serde::Serialize; -/// Adds a key-value pair to the supplied form if `value` is some. Don't forget to implement `std::fmt::Display` for `T`! -pub fn add_to_form_if_some(form: Form, name: &str, value: Option) -> Form +/// Adds a key-value pair to the supplied form if `value` is some. Don't forget +/// to implement `serde::Serialize` for `T`! +pub fn add_to_form_if_some(form: Form, name: &str, value: Option<&T>) -> Form where - T: Display, + T: Serialize, { value.map_or_else( || form, - |value| form.text(name.to_owned(), format!("{}", value)), + |value| form.text(name.to_owned(), serde_json::to_string(value).expect("Failed to serialize a value to JSON")), ) } diff --git a/src/core/requests/mod.rs b/src/core/requests/mod.rs index 6f739fa6..19c3b76c 100644 --- a/src/core/requests/mod.rs +++ b/src/core/requests/mod.rs @@ -18,7 +18,8 @@ pub trait Request { fn token(&self) -> &str; } -/// Unique identifier for the target chat or username of the target channel (in the format @channelusername) +/// Unique identifier for the target chat or username of the target channel (in +/// the format @channelusername) #[derive(Debug, Display, Serialize, From, PartialEq, Eq)] pub enum ChatId { /// chat identifier diff --git a/src/core/requests/send_message.rs b/src/core/requests/send_message.rs index 576c60d9..84141fc8 100644 --- a/src/core/requests/send_message.rs +++ b/src/core/requests/send_message.rs @@ -34,16 +34,16 @@ impl Request for SendMessage { let params = Form::new() .text("chat_id", format!("{}", self.chat_id)) .text("text", self.text); - let params = helpers::add_to_form_if_some(params, "parse_mode", self.parse_mode); + let params = helpers::add_to_form_if_some(params, "parse_mode",self.parse_mode.as_ref()); let params = helpers::add_to_form_if_some( params, "disable_web_page_preview", - self.disable_web_page_preview, + self.disable_web_page_preview.as_ref(), ); let params = - helpers::add_to_form_if_some(params, "disable_notification", self.disable_notification); + helpers::add_to_form_if_some(params, "disable_notification", self.disable_notification.as_ref()); let params = - helpers::add_to_form_if_some(params, "reply_to_message_id", self.reply_to_message_id); + helpers::add_to_form_if_some(params, "reply_to_message_id", self.reply_to_message_id.as_ref()); // TODO: // helpers::add_to_form_if_some(