diff --git a/src/core/requests/helpers.rs b/src/core/requests/helpers.rs new file mode 100644 index 00000000..401dfd96 --- /dev/null +++ b/src/core/requests/helpers.rs @@ -0,0 +1,13 @@ +use reqwest::r#async::multipart::Form; +use std::fmt::Display; + +/// 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 +where + T: Display, +{ + value.map_or_else( + || form, + |value| form.text(name.to_owned(), format!("{}", value)), + ) +} diff --git a/src/core/requests/mod.rs b/src/core/requests/mod.rs index f29c294e..6f739fa6 100644 --- a/src/core/requests/mod.rs +++ b/src/core/requests/mod.rs @@ -1,3 +1,5 @@ +mod helpers; + use reqwest::r#async::multipart::Form; use serde::de::DeserializeOwned; @@ -17,11 +19,13 @@ pub trait Request { } /// Unique identifier for the target chat or username of the target channel (in the format @channelusername) -#[derive(Debug, Serialize, From, PartialEq, Eq)] +#[derive(Debug, Display, Serialize, From, PartialEq, Eq)] pub enum ChatId { /// chat identifier + #[display(fmt = "{}", _0)] Id(i64), /// _channel_ username (in the format @channelusername) + #[display(fmt = "{}", _0)] ChannelUsername(String), } diff --git a/src/core/requests/send_message.rs b/src/core/requests/send_message.rs index 6156d817..576c60d9 100644 --- a/src/core/requests/send_message.rs +++ b/src/core/requests/send_message.rs @@ -1,5 +1,6 @@ use reqwest::r#async::multipart::Form; +use super::helpers; use crate::core::types::Message; use super::{ChatId, Request}; @@ -30,56 +31,26 @@ impl Request for SendMessage { "getMe" } fn params(self) -> Option
{ - use apply::Apply; - - // TODO: we need better serialization let params = Form::new() - .text("chat_id", format!("{:?}", self.chat_id)) - .text("text", self.text) - .apply(|f| { - self.parse_mode - .map_or_else(|| f, |parse_mode| f.text("parse_mode", parse_mode)) - }) - .apply(|f| { - self.disable_web_page_preview.map_or_else( - || f, - |disable_web_page_preview| { - f.text( - "disable_web_page_preview", - format!("{:?}", disable_web_page_preview), - ) - }, - ) - }) - .apply(|f| { - self.disable_notification.map_or_else( - || f, - |disable_notification| { - f.text( - "disable_notification", - format!("{:?}", disable_notification), - ) - }, - ) - }) - .apply(|f| { - self.reply_to_message_id.map_or_else( - || f, - |reply_to_message_id| { - f.text("reply_to_message_id", format!("{:?}", reply_to_message_id)) - }, - ) - }) - .apply(|f| { - self.reply_markup.map_or_else( - || f, - |reply_markup| { - unimplemented!(); - //f.text("reply_markup", ); - f - }, - ) - }); + .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, + "disable_web_page_preview", + self.disable_web_page_preview, + ); + let params = + helpers::add_to_form_if_some(params, "disable_notification", self.disable_notification); + let params = + helpers::add_to_form_if_some(params, "reply_to_message_id", self.reply_to_message_id); + + // TODO: + // helpers::add_to_form_if_some( + // f, + // "reply_markup", + // self.reply_markup, + // ) Some(params) }