mirror of
https://github.com/teloxide/teloxide.git
synced 2024-12-23 15:01:45 +01:00
Merge pull request #73 from telebofr/cows_for_form_builder
little simplification/optimization (use Cow in `FormBuilder`)
This commit is contained in:
commit
4cd9c56622
1 changed files with 15 additions and 8 deletions
|
@ -1,10 +1,13 @@
|
|||
use std::path::PathBuf;
|
||||
use std::{
|
||||
path::PathBuf,
|
||||
borrow::Cow,
|
||||
};
|
||||
|
||||
use reqwest::multipart::Form;
|
||||
|
||||
use crate::{
|
||||
requests::utils,
|
||||
types::{ChatId, InputFile, InputMedia, ParseMode},
|
||||
requests::utils::file_to_part,
|
||||
types::{ChatId, InputMedia, ParseMode, InputFile},
|
||||
};
|
||||
|
||||
/// 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`.
|
||||
pub fn add<T>(self, name: &str, value: T) -> Self
|
||||
pub fn add<'a, T, N>(self, name: N, value: T) -> Self
|
||||
where
|
||||
N: Into<Cow<'a, str>>,
|
||||
T: IntoFormValue,
|
||||
{
|
||||
let name = name.to_owned();
|
||||
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),
|
||||
Some(FormValue::File(path)) => self.add_file(name, path),
|
||||
None => self,
|
||||
}
|
||||
}
|
||||
|
||||
// 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 {
|
||||
form: self
|
||||
.form
|
||||
.part(name.to_owned(), utils::file_to_part(path_to_file)),
|
||||
.part(name.into().into_owned(), file_to_part(path_to_file)),
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue