mirror of
https://github.com/teloxide/teloxide.git
synced 2024-12-22 14:35:36 +01:00
Simplify the Form construction pattern
This commit is contained in:
parent
c6f818e8bb
commit
6531a72ebe
3 changed files with 38 additions and 50 deletions
13
src/core/requests/helpers.rs
Normal file
13
src/core/requests/helpers.rs
Normal 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)),
|
||||
)
|
||||
}
|
|
@ -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),
|
||||
}
|
||||
|
||||
|
|
|
@ -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<Form> {
|
||||
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)
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue