diff --git a/src/requests/all/add_sticker_to_set.rs b/src/requests/all/add_sticker_to_set.rs index 119bd7e1..f76a6856 100644 --- a/src/requests/all/add_sticker_to_set.rs +++ b/src/requests/all/add_sticker_to_set.rs @@ -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 diff --git a/src/requests/all/create_new_sticker_set.rs b/src/requests/all/create_new_sticker_set.rs index cfb98634..8cffc8ae 100644 --- a/src/requests/all/create_new_sticker_set.rs +++ b/src/requests/all/create_new_sticker_set.rs @@ -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 diff --git a/src/requests/all/edit_message_media.rs b/src/requests/all/edit_message_media.rs index e2d365b0..27cb1dd0 100644 --- a/src/requests/all/edit_message_media.rs +++ b/src/requests/all/edit_message_media.rs @@ -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 diff --git a/src/requests/all/send_animation.rs b/src/requests/all/send_animation.rs index cbd40485..f79c2c0b 100644 --- a/src/requests/all/send_animation.rs +++ b/src/requests/all/send_animation.rs @@ -34,34 +34,27 @@ impl Request for SendAnimation { type Output = Message; async fn send(&self) -> ResponseResult { + 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 } diff --git a/src/requests/all/send_audio.rs b/src/requests/all/send_audio.rs index 829e41f9..85add351 100644 --- a/src/requests/all/send_audio.rs +++ b/src/requests/all/send_audio.rs @@ -38,34 +38,26 @@ impl Request for SendAudio { type Output = Message; async fn send(&self) -> ResponseResult { + 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 } diff --git a/src/requests/all/send_document.rs b/src/requests/all/send_document.rs index 3e2cfd46..482f4a69 100644 --- a/src/requests/all/send_document.rs +++ b/src/requests/all/send_document.rs @@ -30,28 +30,24 @@ impl Request for SendDocument { type Output = Message; async fn send(&self) -> ResponseResult { + 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 } diff --git a/src/requests/all/send_media_group.rs b/src/requests/all/send_media_group.rs index cae6604d..b6f2d9c3 100644 --- a/src/requests/all/send_media_group.rs +++ b/src/requests/all/send_media_group.rs @@ -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 diff --git a/src/requests/all/send_photo.rs b/src/requests/all/send_photo.rs index ea603eca..3191373c 100644 --- a/src/requests/all/send_photo.rs +++ b/src/requests/all/send_photo.rs @@ -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 diff --git a/src/requests/all/send_sticker.rs b/src/requests/all/send_sticker.rs index e44dbb80..ae5c898f 100644 --- a/src/requests/all/send_sticker.rs +++ b/src/requests/all/send_sticker.rs @@ -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 diff --git a/src/requests/all/send_video.rs b/src/requests/all/send_video.rs index 8e89ef2e..450fd224 100644 --- a/src/requests/all/send_video.rs +++ b/src/requests/all/send_video.rs @@ -35,36 +35,27 @@ impl Request for SendVideo { type Output = Message; async fn send(&self) -> ResponseResult { + 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 } diff --git a/src/requests/all/send_video_note.rs b/src/requests/all/send_video_note.rs index e3099c35..cf32b467 100644 --- a/src/requests/all/send_video_note.rs +++ b/src/requests/all/send_video_note.rs @@ -30,28 +30,23 @@ impl Request for SendVideoNote { type Output = Message; async fn send(&self) -> ResponseResult { + 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 } diff --git a/src/requests/all/send_voice.rs b/src/requests/all/send_voice.rs index b5ef7b5d..71b042c3 100644 --- a/src/requests/all/send_voice.rs +++ b/src/requests/all/send_voice.rs @@ -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 diff --git a/src/requests/form_builder.rs b/src/requests/form_builder.rs index 1f433035..3d0e48ad 100644 --- a/src/requests/form_builder.rs +++ b/src/requests/form_builder.rs @@ -21,25 +21,33 @@ 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 - where - N: Into>, - T: IntoFormValue, + pub fn add_text<'a, T, N>(self, name: N, value: &T) -> Self + where + N: Into>, + T: IntoFormText, { - let name = name.into().into_owned(); - match value.into_form_value() { - Some(FormValue::Str(string)) => { - Self { form: self.form.text(name, string) } - } - 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, + match value.into_form_text() { + Some(val) => Self { form: self.form.text(name.into().into_owned(), val) }, + None => self } } + pub async fn add_input_file<'a, N>(self, name: N, value: &InputFile) -> Self + where + N: Into>, + { + 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), + } + } + // used in SendMediaGroup pub async fn add_file<'a, N>(self, name: N, path_to_file: PathBuf) -> Self where @@ -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; +pub(crate) trait IntoFormText { + fn into_form_text(&self) -> Option; } macro_rules! impl_for_struct { ($($name:ty),*) => { $( - impl IntoFormValue for $name { - fn into_form_value(&self) -> Option { + impl IntoFormText for $name { + fn into_form_text(&self) -> Option { 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 IntoFormValue for Option +impl IntoFormText for Option where - T: IntoFormValue, + T: IntoFormText, { - fn into_form_value(&self) -> Option { - self.as_ref().and_then(IntoFormValue::into_form_value) + fn into_form_text(&self) -> Option { + 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 { - fn into_form_value(&self) -> Option { +impl IntoFormText for Vec { + fn into_form_text(&self) -> Option { 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 { +impl IntoFormText for InputMedia { + fn into_form_text(&self) -> Option { 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 { - Some(FormValue::Str(self.to_owned())) +impl IntoFormText for str { + fn into_form_text(&self) -> Option { + Some(self.to_owned()) } } -impl IntoFormValue for ParseMode { - fn into_form_value(&self) -> Option { +impl IntoFormText for ParseMode { + fn into_form_text(&self) -> Option { 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 { +impl IntoFormText for ChatId { + fn into_form_text(&self) -> Option { 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 { - Some(FormValue::Str(self.clone())) - } -} - -impl IntoFormValue for InputFile { - fn into_form_value(&self) -> Option { - 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 { + Some(self.clone()) } }