mirror of
https://github.com/teloxide/teloxide.git
synced 2025-01-10 20:12:25 +01:00
Rework HandlerAndFilter::new
This commit is contained in:
parent
60391de28a
commit
f8851eabff
2 changed files with 16 additions and 151 deletions
|
@ -18,11 +18,15 @@ struct FilterAndHandler<'a, T, E> {
|
|||
}
|
||||
|
||||
impl<'a, T, E> FilterAndHandler<'a, T, E> {
|
||||
fn new(
|
||||
filter: Box<dyn Filter<T> + 'a>,
|
||||
handler: Box<dyn Handler<'a, T, E> + 'a>,
|
||||
) -> Self {
|
||||
FilterAndHandler { filter, handler }
|
||||
fn new<F, H>(filter: F, handler: H) -> Self
|
||||
where
|
||||
F: Filter<T> + 'a,
|
||||
H: Handler<'a, T, E> + 'a,
|
||||
{
|
||||
FilterAndHandler {
|
||||
filter: Box::new(filter),
|
||||
handler: Box::new(handler),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -121,7 +125,7 @@ where
|
|||
H: Handler<'a, Message, E> + 'a,
|
||||
{
|
||||
self.message_handlers
|
||||
.push(FilterAndHandler::new(Box::new(filter), Box::new(handler)));
|
||||
.push(FilterAndHandler::new(filter, handler));
|
||||
self
|
||||
}
|
||||
|
||||
|
@ -131,7 +135,7 @@ where
|
|||
H: Handler<'a, Message, E> + 'a,
|
||||
{
|
||||
self.edited_message_handlers
|
||||
.push(FilterAndHandler::new(Box::new(filter), Box::new(handler)));
|
||||
.push(FilterAndHandler::new(filter, handler));
|
||||
self
|
||||
}
|
||||
|
||||
|
@ -141,7 +145,7 @@ where
|
|||
H: Handler<'a, Message, E> + 'a,
|
||||
{
|
||||
self.channel_post_handlers
|
||||
.push(FilterAndHandler::new(Box::new(filter), Box::new(handler)));
|
||||
.push(FilterAndHandler::new(filter, handler));
|
||||
self
|
||||
}
|
||||
|
||||
|
@ -155,7 +159,7 @@ where
|
|||
H: Handler<'a, Message, E> + 'a,
|
||||
{
|
||||
self.edited_channel_post_handlers
|
||||
.push(FilterAndHandler::new(Box::new(filter), Box::new(handler)));
|
||||
.push(FilterAndHandler::new(filter, handler));
|
||||
self
|
||||
}
|
||||
|
||||
|
@ -165,7 +169,7 @@ where
|
|||
H: Handler<'a, (), E> + 'a,
|
||||
{
|
||||
self.inline_query_handlers
|
||||
.push(FilterAndHandler::new(Box::new(filter), Box::new(handler)));
|
||||
.push(FilterAndHandler::new(filter, handler));
|
||||
self
|
||||
}
|
||||
|
||||
|
@ -179,7 +183,7 @@ where
|
|||
H: Handler<'a, ChosenInlineResult, E> + 'a,
|
||||
{
|
||||
self.chosen_inline_result_handlers
|
||||
.push(FilterAndHandler::new(Box::new(filter), Box::new(handler)));
|
||||
.push(FilterAndHandler::new(filter, handler));
|
||||
self
|
||||
}
|
||||
|
||||
|
@ -189,7 +193,7 @@ where
|
|||
H: Handler<'a, CallbackQuery, E> + 'a,
|
||||
{
|
||||
self.callback_query_handlers
|
||||
.push(FilterAndHandler::new(Box::new(filter), Box::new(handler)));
|
||||
.push(FilterAndHandler::new(filter, handler));
|
||||
self
|
||||
}
|
||||
|
||||
|
|
|
@ -1,139 +0,0 @@
|
|||
use std::{
|
||||
path::PathBuf,
|
||||
borrow::Cow,
|
||||
};
|
||||
|
||||
use reqwest::multipart::Form;
|
||||
|
||||
use crate::{
|
||||
requests::utils::file_to_part,
|
||||
types::{ChatId, InputMedia, ParseMode, InputFile},
|
||||
};
|
||||
|
||||
/// This is a convenient struct that builds `reqwest::multipart::Form`
|
||||
/// from scratch.
|
||||
pub(crate) struct FormBuilder {
|
||||
form: Form,
|
||||
}
|
||||
|
||||
impl FormBuilder {
|
||||
pub fn new() -> Self {
|
||||
Self { form: Form::new() }
|
||||
}
|
||||
|
||||
/// Add the supplied key-value pair to this `FormBuilder`.
|
||||
pub fn add<'a, T, N>(self, name: N, value: T) -> Self
|
||||
where
|
||||
N: Into<Cow<'a, str>>,
|
||||
T: IntoFormValue,
|
||||
{
|
||||
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),
|
||||
None => self,
|
||||
}
|
||||
}
|
||||
|
||||
// used in SendMediaGroup
|
||||
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.into().into_owned(), file_to_part(path_to_file)),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn build(self) -> Form {
|
||||
self.form
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) enum FormValue {
|
||||
File(PathBuf),
|
||||
Str(String),
|
||||
}
|
||||
|
||||
pub(crate) trait IntoFormValue {
|
||||
fn into_form_value(self) -> Option<FormValue>;
|
||||
}
|
||||
|
||||
macro_rules! impl_for_struct {
|
||||
($($name:ty),*) => {
|
||||
$(
|
||||
impl IntoFormValue for $name {
|
||||
fn into_form_value(self) -> Option<FormValue> {
|
||||
let json = serde_json::to_string(&self)
|
||||
.expect("serde_json::to_string failed");
|
||||
Some(FormValue::Str(json))
|
||||
}
|
||||
}
|
||||
)*
|
||||
};
|
||||
}
|
||||
|
||||
impl_for_struct!(bool, i32, i64);
|
||||
|
||||
impl<T> IntoFormValue for Option<T>
|
||||
where
|
||||
T: IntoFormValue,
|
||||
{
|
||||
fn into_form_value(self) -> Option<FormValue> {
|
||||
self.and_then(IntoFormValue::into_form_value)
|
||||
}
|
||||
}
|
||||
|
||||
impl IntoFormValue for &[InputMedia] {
|
||||
fn into_form_value(self) -> Option<FormValue> {
|
||||
let json =
|
||||
serde_json::to_string(self).expect("serde_json::to_string failed");
|
||||
Some(FormValue::Str(json))
|
||||
}
|
||||
}
|
||||
|
||||
impl IntoFormValue for &str {
|
||||
fn into_form_value(self) -> Option<FormValue> {
|
||||
Some(FormValue::Str(self.to_owned()))
|
||||
}
|
||||
}
|
||||
|
||||
impl IntoFormValue for ParseMode {
|
||||
fn into_form_value(self) -> Option<FormValue> {
|
||||
let string = match self {
|
||||
ParseMode::HTML => String::from("HTML"),
|
||||
ParseMode::Markdown => String::from("Markdown"),
|
||||
};
|
||||
Some(FormValue::Str(string))
|
||||
}
|
||||
}
|
||||
|
||||
impl IntoFormValue for ChatId {
|
||||
fn into_form_value(self) -> Option<FormValue> {
|
||||
let string = match self {
|
||||
ChatId::Id(id) => id.to_string(),
|
||||
ChatId::ChannelUsername(username) => username,
|
||||
};
|
||||
Some(FormValue::Str(string))
|
||||
}
|
||||
}
|
||||
|
||||
impl IntoFormValue for String {
|
||||
fn into_form_value(self) -> Option<FormValue> {
|
||||
Some(FormValue::Str(self))
|
||||
}
|
||||
}
|
||||
|
||||
impl IntoFormValue for InputFile {
|
||||
fn into_form_value(self) -> Option<FormValue> {
|
||||
match self {
|
||||
InputFile::File(path) => Some(FormValue::File(path)),
|
||||
InputFile::Url(url) => Some(FormValue::Str(url)),
|
||||
InputFile::FileId(file_id) => Some(FormValue::Str(file_id)),
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue