Merge pull request #73 from telebofr/cows_for_form_builder

little simplification/optimization (use Cow in `FormBuilder`)
This commit is contained in:
Temirkhan Myrzamadi 2019-11-02 13:32:58 +00:00 committed by GitHub
commit 4cd9c56622

View file

@ -1,10 +1,13 @@
use std::path::PathBuf; use std::{
path::PathBuf,
borrow::Cow,
};
use reqwest::multipart::Form; use reqwest::multipart::Form;
use crate::{ use crate::{
requests::utils, requests::utils::file_to_part,
types::{ChatId, InputFile, InputMedia, ParseMode}, types::{ChatId, InputMedia, ParseMode, InputFile},
}; };
/// This is a convenient struct that builds `reqwest::multipart::Form` /// This is a convenient struct that builds `reqwest::multipart::Form`
@ -19,26 +22,30 @@ impl FormBuilder {
} }
/// Add the supplied key-value pair to this `FormBuilder`. /// Add the supplied key-value pair to this `FormBuilder`.
pub fn add<T>(self, name: &str, value: T) -> Self pub fn add<'a, T, N>(self, name: N, value: T) -> Self
where where
N: Into<Cow<'a, str>>,
T: IntoFormValue, T: IntoFormValue,
{ {
let name = name.to_owned(); let name = name.into().into_owned();
match value.into_form_value() { match value.into_form_value() {
Some(FormValue::Str(string)) => Self { Some(FormValue::Str(string)) => Self {
form: self.form.text(name, string), form: self.form.text(name, string),
}, },
Some(FormValue::File(path)) => self.add_file(&name, path), Some(FormValue::File(path)) => self.add_file(name, path),
None => self, None => self,
} }
} }
// used in SendMediaGroup // used in SendMediaGroup
pub fn add_file(self, name: &str, path_to_file: PathBuf) -> Self { pub fn add_file<'a, N>(self, name: N, path_to_file: PathBuf) -> Self
where
N: Into<Cow<'a, str>>
{
Self { Self {
form: self form: self
.form .form
.part(name.to_owned(), utils::file_to_part(path_to_file)), .part(name.into().into_owned(), file_to_part(path_to_file)),
} }
} }