Simplify the Form construction pattern

This commit is contained in:
Temirkhan Myrzamadi 2019-09-03 12:35:20 +06:00
parent c6f818e8bb
commit 6531a72ebe
3 changed files with 38 additions and 50 deletions

View file

@ -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<T>(form: Form, name: &str, value: Option<T>) -> Form
where
T: Display,
{
value.map_or_else(
|| form,
|value| form.text(name.to_owned(), format!("{}", value)),
)
}

View file

@ -1,3 +1,5 @@
mod helpers;
use reqwest::r#async::multipart::Form; use reqwest::r#async::multipart::Form;
use serde::de::DeserializeOwned; 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) /// 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 { pub enum ChatId {
/// chat identifier /// chat identifier
#[display(fmt = "{}", _0)]
Id(i64), Id(i64),
/// _channel_ username (in the format @channelusername) /// _channel_ username (in the format @channelusername)
#[display(fmt = "{}", _0)]
ChannelUsername(String), ChannelUsername(String),
} }

View file

@ -1,5 +1,6 @@
use reqwest::r#async::multipart::Form; use reqwest::r#async::multipart::Form;
use super::helpers;
use crate::core::types::Message; use crate::core::types::Message;
use super::{ChatId, Request}; use super::{ChatId, Request};
@ -30,56 +31,26 @@ impl Request for SendMessage {
"getMe" "getMe"
} }
fn params(self) -> Option<Form> { fn params(self) -> Option<Form> {
use apply::Apply;
// TODO: we need better serialization
let params = Form::new() let params = Form::new()
.text("chat_id", format!("{:?}", self.chat_id)) .text("chat_id", format!("{}", self.chat_id))
.text("text", self.text) .text("text", self.text);
.apply(|f| { let params = helpers::add_to_form_if_some(params, "parse_mode", self.parse_mode);
self.parse_mode let params = helpers::add_to_form_if_some(
.map_or_else(|| f, |parse_mode| f.text("parse_mode", parse_mode)) params,
})
.apply(|f| {
self.disable_web_page_preview.map_or_else(
|| f,
|disable_web_page_preview| {
f.text(
"disable_web_page_preview", "disable_web_page_preview",
format!("{:?}", 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 =
.apply(|f| { helpers::add_to_form_if_some(params, "reply_to_message_id", self.reply_to_message_id);
self.disable_notification.map_or_else(
|| f, // TODO:
|disable_notification| { // helpers::add_to_form_if_some(
f.text( // f,
"disable_notification", // "reply_markup",
format!("{:?}", disable_notification), // self.reply_markup,
) // )
},
)
})
.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
},
)
});
Some(params) Some(params)
} }