remove add method from FormBuilder

This commit is contained in:
p0lunin 2020-07-04 16:46:00 +03:00
parent 96a92b6ec2
commit c04032cdf0
13 changed files with 168 additions and 248 deletions

View file

@ -31,16 +31,12 @@ impl Request for AddStickerToSet {
self.bot.token(),
"addStickerToSet",
FormBuilder::new()
.add("user_id", &self.user_id)
.await
.add("name", &self.name)
.await
.add("png_sticker", &self.png_sticker)
.await
.add("emojis", &self.emojis)
.await
.add("mask_position", &self.mask_position)
.add_text("user_id", &self.user_id)
.add_text("name", &self.name)
.add_input_file("png_sticker", &self.png_sticker)
.await
.add_text("emojis", &self.emojis)
.add_text("mask_position", &self.mask_position)
.build(),
)
.await

View file

@ -32,20 +32,14 @@ impl Request for CreateNewStickerSet {
self.bot.token(),
"createNewStickerSet",
FormBuilder::new()
.add("user_id", &self.user_id)
.await
.add("name", &self.name)
.await
.add("title", &self.title)
.await
.add("png_sticker", &self.png_sticker)
.await
.add("emojis", &self.emojis)
.await
.add("contains_masks", &self.contains_masks)
.await
.add("mask_position", &self.mask_position)
.add_text("user_id", &self.user_id)
.add_text("name", &self.name)
.add_text("title", &self.title)
.add_input_file("png_sticker", &self.png_sticker)
.await
.add_text("emojis", &self.emojis)
.add_text("contains_masks", &self.contains_masks)
.add_text("mask_position", &self.mask_position)
.build(),
)
.await

View file

@ -38,14 +38,12 @@ impl Request for EditMessageMedia {
match &self.chat_or_inline_message {
ChatOrInlineMessage::Chat { chat_id, message_id } => {
params = params
.add("chat_id", chat_id)
.await
.add("message_id", message_id)
.await;
.add_text("chat_id", chat_id)
.add_text("message_id", message_id);
}
ChatOrInlineMessage::Inline { inline_message_id } => {
params =
params.add("inline_message_id", inline_message_id).await;
params.add_text("inline_message_id", inline_message_id);
}
}
@ -54,10 +52,8 @@ impl Request for EditMessageMedia {
self.bot.token(),
"editMessageMedia",
params
.add("media", &self.media)
.await
.add("reply_markup", &self.reply_markup)
.await
.add_text("media", &self.media)
.add_text("reply_markup", &self.reply_markup)
.build(),
)
.await

View file

@ -34,34 +34,27 @@ impl Request for SendAnimation {
type Output = Message;
async fn send(&self) -> ResponseResult<Message> {
let mut builder =
FormBuilder::new()
.add_text("chat_id", &self.chat_id)
.add_input_file("animation", &self.animation)
.await
.add_text("duration", &self.duration)
.add_text("width", &self.width)
.add_text("height", &self.height)
.add_text("caption", &self.caption)
.add_text("parse_mode", &self.parse_mode)
.add_text("disable_notification", &self.disable_notification)
.add_text("reply_to_message_id", &self.reply_to_message_id)
.add_text("reply_markup", &self.reply_markup);
if let Some(thumb) = self.thumb.as_ref() {
builder = builder.add_input_file("thumb", thumb).await;
}
net::request_multipart(
self.bot.client(),
self.bot.token(),
"sendAnimation",
FormBuilder::new()
.add("chat_id", &self.chat_id)
.await
.add("animation", &self.animation)
.await
.add("duration", &self.duration)
.await
.add("width", &self.width)
.await
.add("height", &self.height)
.await
.add("thumb", &self.thumb)
.await
.add("caption", &self.caption)
.await
.add("parse_mode", &self.parse_mode)
.await
.add("disable_notification", &self.disable_notification)
.await
.add("reply_to_message_id", &self.reply_to_message_id)
.await
.add("reply_markup", &self.reply_markup)
.await
.build(),
builder.build(),
)
.await
}

View file

@ -38,34 +38,26 @@ impl Request for SendAudio {
type Output = Message;
async fn send(&self) -> ResponseResult<Message> {
let mut builder = FormBuilder::new()
.add_text("chat_id", &self.chat_id)
.add_input_file("audio", &self.audio)
.await
.add_text("caption", &self.caption)
.add_text("parse_mode", &self.parse_mode)
.add_text("duration", &self.duration)
.add_text("performer", &self.performer)
.add_text("title", &self.title)
.add_text("disable_notification", &self.disable_notification)
.add_text("reply_to_message_id", &self.reply_to_message_id)
.add_text("reply_markup", &self.reply_markup);
if let Some(thumb) = self.thumb.as_ref() {
builder = builder.add_input_file("thumb", thumb).await;
}
net::request_multipart(
self.bot.client(),
self.bot.token(),
"sendAudio",
FormBuilder::new()
.add("chat_id", &self.chat_id)
.await
.add("audio", &self.audio)
.await
.add("caption", &self.caption)
.await
.add("parse_mode", &self.parse_mode)
.await
.add("duration", &self.duration)
.await
.add("performer", &self.performer)
.await
.add("title", &self.title)
.await
.add("thumb", &self.thumb)
.await
.add("disable_notification", &self.disable_notification)
.await
.add("reply_to_message_id", &self.reply_to_message_id)
.await
.add("reply_markup", &self.reply_markup)
.await
.build(),
builder.build()
)
.await
}

View file

@ -30,28 +30,24 @@ impl Request for SendDocument {
type Output = Message;
async fn send(&self) -> ResponseResult<Message> {
let mut builder = FormBuilder::new()
.add_text("chat_id", &self.chat_id)
.add_input_file("document", &self.document)
.await
.add_text("caption", &self.caption)
.add_text("parse_mode", &self.parse_mode)
.add_text("disable_notification", &self.disable_notification)
.add_text("reply_to_message_id", &self.reply_to_message_id)
.add_text("reply_markup", &self.reply_markup);
if let Some(thumb) = self.thumb.as_ref() {
builder = builder.add_input_file("thumb", thumb)
.await;
}
net::request_multipart(
self.bot.client(),
self.bot.token(),
"sendDocument",
FormBuilder::new()
.add("chat_id", &self.chat_id)
.await
.add("document", &self.document)
.await
.add("thumb", &self.thumb)
.await
.add("caption", &self.caption)
.await
.add("parse_mode", &self.parse_mode)
.await
.add("disable_notification", &self.disable_notification)
.await
.add("reply_to_message_id", &self.reply_to_message_id)
.await
.add("reply_markup", &self.reply_markup)
.await
.build(),
builder.build()
)
.await
}

View file

@ -28,14 +28,10 @@ impl Request for SendMediaGroup {
self.bot.token(),
"sendMediaGroup",
FormBuilder::new()
.add("chat_id", &self.chat_id)
.await
.add("media", &self.media)
.await
.add("disable_notification", &self.disable_notification)
.await
.add("reply_to_message_id", &self.reply_to_message_id)
.await
.add_text("chat_id", &self.chat_id)
.add_text("media", &self.media)
.add_text("disable_notification", &self.disable_notification)
.add_text("reply_to_message_id", &self.reply_to_message_id)
.build(),
)
.await

View file

@ -31,20 +31,14 @@ impl Request for SendPhoto {
self.bot.token(),
"sendPhoto",
FormBuilder::new()
.add("chat_id", &self.chat_id)
.await
.add("photo", &self.photo)
.await
.add("caption", &self.caption)
.await
.add("parse_mode", &self.parse_mode)
.await
.add("disable_notification", &self.disable_notification)
.await
.add("reply_to_message_id", &self.reply_to_message_id)
.await
.add("reply_markup", &self.reply_markup)
.add_text("chat_id", &self.chat_id)
.add_input_file("photo", &self.photo)
.await
.add_text("caption", &self.caption)
.add_text("parse_mode", &self.parse_mode)
.add_text("disable_notification", &self.disable_notification)
.add_text("reply_to_message_id", &self.reply_to_message_id)
.add_text("reply_markup", &self.reply_markup)
.build(),
)
.await

View file

@ -31,16 +31,12 @@ impl Request for SendSticker {
self.bot.token(),
"sendSticker",
FormBuilder::new()
.add("chat_id", &self.chat_id)
.await
.add("sticker", &self.sticker)
.await
.add("disable_notification", &self.disable_notification)
.await
.add("reply_to_message_id", &self.reply_to_message_id)
.await
.add("reply_markup", &self.reply_markup)
.add_text("chat_id", &self.chat_id)
.add_input_file("sticker", &self.sticker)
.await
.add_text("disable_notification", &self.disable_notification)
.add_text("reply_to_message_id", &self.reply_to_message_id)
.add_text("reply_markup", &self.reply_markup)
.build(),
)
.await

View file

@ -35,36 +35,27 @@ impl Request for SendVideo {
type Output = Message;
async fn send(&self) -> ResponseResult<Message> {
let mut builder = FormBuilder::new()
.add_text("chat_id", &self.chat_id)
.add_input_file("video", &self.video)
.await
.add_text("duration", &self.duration)
.add_text("width", &self.width)
.add_text("height", &self.height)
.add_text("caption", &self.caption)
.add_text("parse_mode", &self.parse_mode)
.add_text("supports_streaming", &self.supports_streaming)
.add_text("disable_notification", &self.disable_notification)
.add_text("reply_to_message_id", &self.reply_to_message_id)
.add_text("reply_markup", &self.reply_markup);
if let Some(thumb) = self.thumb.as_ref() {
builder = builder.add_input_file("thumb", thumb).await;
}
net::request_multipart(
self.bot.client(),
self.bot.token(),
"sendVideo",
FormBuilder::new()
.add("chat_id", &self.chat_id)
.await
.add("video", &self.video)
.await
.add("duration", &self.duration)
.await
.add("width", &self.width)
.await
.add("height", &self.height)
.await
.add("thumb", &self.thumb)
.await
.add("caption", &self.caption)
.await
.add("parse_mode", &self.parse_mode)
.await
.add("supports_streaming", &self.supports_streaming)
.await
.add("disable_notification", &self.disable_notification)
.await
.add("reply_to_message_id", &self.reply_to_message_id)
.await
.add("reply_markup", &self.reply_markup)
.await
.build(),
builder.build(),
)
.await
}

View file

@ -30,28 +30,23 @@ impl Request for SendVideoNote {
type Output = Message;
async fn send(&self) -> ResponseResult<Message> {
let mut builder = FormBuilder::new()
.add_text("chat_id", &self.chat_id)
.add_input_file("video_note", &self.video_note)
.await
.add_text("duration", &self.duration)
.add_text("length", &self.length)
.add_text("disable_notification", &self.disable_notification)
.add_text("reply_to_message_id", &self.reply_to_message_id)
.add_text("reply_markup", &self.reply_markup);
if let Some(thumb) = self.thumb.as_ref() {
builder = builder.add_input_file("thumb", thumb).await;
}
net::request_multipart(
self.bot.client(),
self.bot.token(),
"sendVideoNote",
FormBuilder::new()
.add("chat_id", &self.chat_id)
.await
.add("video_note", &self.video_note)
.await
.add("duration", &self.duration)
.await
.add("length", &self.length)
.await
.add("thumb", &self.thumb)
.await
.add("disable_notification", &self.disable_notification)
.await
.add("reply_to_message_id", &self.reply_to_message_id)
.await
.add("reply_markup", &self.reply_markup)
.await
.build(),
builder.build(),
)
.await
}

View file

@ -41,22 +41,15 @@ impl Request for SendVoice {
self.bot.token(),
"sendVoice",
FormBuilder::new()
.add("chat_id", &self.chat_id)
.await
.add("voice", &self.voice)
.await
.add("caption", &self.caption)
.await
.add("parse_mode", &self.parse_mode)
.await
.add("duration", &self.duration)
.await
.add("disable_notification", &self.disable_notification)
.await
.add("reply_to_message_id", &self.reply_to_message_id)
.await
.add("reply_markup", &self.reply_markup)
.add_text("chat_id", &self.chat_id)
.add_input_file("voice", &self.voice)
.await
.add_text("caption", &self.caption)
.add_text("parse_mode", &self.parse_mode)
.add_text("duration", &self.duration)
.add_text("disable_notification", &self.disable_notification)
.add_text("reply_to_message_id", &self.reply_to_message_id)
.add_text("reply_markup", &self.reply_markup)
.build(),
)
.await

View file

@ -21,22 +21,30 @@ impl FormBuilder {
Self { form: Form::new() }
}
/// Add the supplied key-value pair to this `FormBuilder`.
pub async fn add<'a, T, N>(self, name: N, value: &T) -> Self
pub fn add_text<'a, T, N>(self, name: N, value: &T) -> Self
where
N: Into<Cow<'a, str>>,
T: IntoFormValue,
T: IntoFormText,
{
let name = name.into().into_owned();
match value.into_form_value() {
Some(FormValue::Str(string)) => {
Self { form: self.form.text(name, string) }
match value.into_form_text() {
Some(val) => Self { form: self.form.text(name.into().into_owned(), val) },
None => self
}
Some(FormValue::File(path)) => self.add_file(name, path).await,
Some(FormValue::Memory { file_name, data }) => {
self.add_file_from_memory(name, file_name, data)
}
None => self,
pub async fn add_input_file<'a, N>(self, name: N, value: &InputFile) -> Self
where
N: Into<Cow<'a, str>>,
{
match value {
InputFile::File(path) => self.add_file(name, path.clone()).await,
InputFile::Memory { file_name, data } => self.add_file_from_memory(
name,
file_name.clone(),
data.clone(),
),
InputFile::Url(url) => self.add_text(name, url),
InputFile::FileId(file_id) => self.add_text(name, file_id),
}
}
@ -75,24 +83,18 @@ impl FormBuilder {
}
}
pub(crate) enum FormValue {
File(PathBuf),
Memory { file_name: String, data: Cow<'static, [u8]> },
Str(String),
}
pub(crate) trait IntoFormValue {
fn into_form_value(&self) -> Option<FormValue>;
pub(crate) trait IntoFormText {
fn into_form_text(&self) -> Option<String>;
}
macro_rules! impl_for_struct {
($($name:ty),*) => {
$(
impl IntoFormValue for $name {
fn into_form_value(&self) -> Option<FormValue> {
impl IntoFormText for $name {
fn into_form_text(&self) -> Option<String> {
let json = serde_json::to_string(self)
.expect("serde_json::to_string failed");
Some(FormValue::Str(json))
Some(json)
}
}
)*
@ -109,77 +111,63 @@ impl_for_struct!(
MaskPosition
);
impl<T> IntoFormValue for Option<T>
impl<T> IntoFormText for Option<T>
where
T: IntoFormValue,
T: IntoFormText,
{
fn into_form_value(&self) -> Option<FormValue> {
self.as_ref().and_then(IntoFormValue::into_form_value)
fn into_form_text(&self) -> Option<String> {
self.as_ref().and_then(IntoFormText::into_form_text)
}
}
// TODO: fix InputMedia implementation of IntoFormValue (for now it doesn't
// encode files :|)
impl IntoFormValue for Vec<InputMedia> {
fn into_form_value(&self) -> Option<FormValue> {
impl IntoFormText for Vec<InputMedia> {
fn into_form_text(&self) -> Option<String> {
let json =
serde_json::to_string(self).expect("serde_json::to_string failed");
Some(FormValue::Str(json))
Some(json)
}
}
impl IntoFormValue for InputMedia {
fn into_form_value(&self) -> Option<FormValue> {
impl IntoFormText for InputMedia {
fn into_form_text(&self) -> Option<String> {
let json =
serde_json::to_string(self).expect("serde_json::to_string failed");
Some(FormValue::Str(json))
Some(json)
}
}
impl IntoFormValue for str {
fn into_form_value(&self) -> Option<FormValue> {
Some(FormValue::Str(self.to_owned()))
impl IntoFormText for str {
fn into_form_text(&self) -> Option<String> {
Some(self.to_owned())
}
}
impl IntoFormValue for ParseMode {
fn into_form_value(&self) -> Option<FormValue> {
impl IntoFormText for ParseMode {
fn into_form_text(&self) -> Option<String> {
let string = match self {
ParseMode::MarkdownV2 => String::from("MarkdownV2"),
ParseMode::HTML => String::from("HTML"),
#[allow(deprecated)]
ParseMode::Markdown => String::from("Markdown"),
};
Some(FormValue::Str(string))
Some(string)
}
}
impl IntoFormValue for ChatId {
fn into_form_value(&self) -> Option<FormValue> {
impl IntoFormText for ChatId {
fn into_form_text(&self) -> Option<String> {
let string = match self {
ChatId::Id(id) => id.to_string(),
ChatId::ChannelUsername(username) => username.clone(),
};
Some(FormValue::Str(string))
Some(string)
}
}
impl IntoFormValue for String {
fn into_form_value(&self) -> Option<FormValue> {
Some(FormValue::Str(self.clone()))
}
}
impl IntoFormValue for InputFile {
fn into_form_value(&self) -> Option<FormValue> {
match self {
InputFile::File(path) => Some(FormValue::File(path.clone())),
InputFile::Memory { file_name, data } => Some(FormValue::Memory {
file_name: file_name.clone(),
data: data.clone(),
}),
InputFile::Url(url) => Some(FormValue::Str(url.clone())),
InputFile::FileId(file_id) => Some(FormValue::Str(file_id.clone())),
}
impl IntoFormText for String {
fn into_form_text(&self) -> Option<String> {
Some(self.clone())
}
}