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(), self.bot.token(),
"addStickerToSet", "addStickerToSet",
FormBuilder::new() FormBuilder::new()
.add("user_id", &self.user_id) .add_text("user_id", &self.user_id)
.await .add_text("name", &self.name)
.add("name", &self.name) .add_input_file("png_sticker", &self.png_sticker)
.await
.add("png_sticker", &self.png_sticker)
.await
.add("emojis", &self.emojis)
.await
.add("mask_position", &self.mask_position)
.await .await
.add_text("emojis", &self.emojis)
.add_text("mask_position", &self.mask_position)
.build(), .build(),
) )
.await .await

View file

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

View file

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

View file

@ -34,34 +34,27 @@ impl Request for SendAnimation {
type Output = Message; type Output = Message;
async fn send(&self) -> ResponseResult<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( net::request_multipart(
self.bot.client(), self.bot.client(),
self.bot.token(), self.bot.token(),
"sendAnimation", "sendAnimation",
FormBuilder::new() builder.build(),
.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(),
) )
.await .await
} }

View file

@ -38,34 +38,26 @@ impl Request for SendAudio {
type Output = Message; type Output = Message;
async fn send(&self) -> ResponseResult<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( net::request_multipart(
self.bot.client(), self.bot.client(),
self.bot.token(), self.bot.token(),
"sendAudio", "sendAudio",
FormBuilder::new() builder.build()
.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(),
) )
.await .await
} }

View file

@ -30,28 +30,24 @@ impl Request for SendDocument {
type Output = Message; type Output = Message;
async fn send(&self) -> ResponseResult<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( net::request_multipart(
self.bot.client(), self.bot.client(),
self.bot.token(), self.bot.token(),
"sendDocument", "sendDocument",
FormBuilder::new() builder.build()
.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(),
) )
.await .await
} }

View file

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

View file

@ -31,20 +31,14 @@ impl Request for SendPhoto {
self.bot.token(), self.bot.token(),
"sendPhoto", "sendPhoto",
FormBuilder::new() FormBuilder::new()
.add("chat_id", &self.chat_id) .add_text("chat_id", &self.chat_id)
.await .add_input_file("photo", &self.photo)
.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)
.await .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(), .build(),
) )
.await .await

View file

@ -31,16 +31,12 @@ impl Request for SendSticker {
self.bot.token(), self.bot.token(),
"sendSticker", "sendSticker",
FormBuilder::new() FormBuilder::new()
.add("chat_id", &self.chat_id) .add_text("chat_id", &self.chat_id)
.await .add_input_file("sticker", &self.sticker)
.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)
.await .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(), .build(),
) )
.await .await

View file

@ -35,36 +35,27 @@ impl Request for SendVideo {
type Output = Message; type Output = Message;
async fn send(&self) -> ResponseResult<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( net::request_multipart(
self.bot.client(), self.bot.client(),
self.bot.token(), self.bot.token(),
"sendVideo", "sendVideo",
FormBuilder::new() builder.build(),
.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(),
) )
.await .await
} }

View file

@ -30,28 +30,23 @@ impl Request for SendVideoNote {
type Output = Message; type Output = Message;
async fn send(&self) -> ResponseResult<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( net::request_multipart(
self.bot.client(), self.bot.client(),
self.bot.token(), self.bot.token(),
"sendVideoNote", "sendVideoNote",
FormBuilder::new() builder.build(),
.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(),
) )
.await .await
} }

View file

@ -41,22 +41,15 @@ impl Request for SendVoice {
self.bot.token(), self.bot.token(),
"sendVoice", "sendVoice",
FormBuilder::new() FormBuilder::new()
.add("chat_id", &self.chat_id) .add_text("chat_id", &self.chat_id)
.await .add_input_file("voice", &self.voice)
.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)
.await .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(), .build(),
) )
.await .await

View file

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