Move Output type and method fn from dynamic::Payload to new DynMethod trait

This commit is contained in:
Waffle 2019-11-05 18:55:13 +03:00
parent 521eef7550
commit 80ad646869
4 changed files with 27 additions and 15 deletions

View file

@ -2,21 +2,16 @@ use serde::de::DeserializeOwned;
use reqwest::multipart;
use crate::{Bot, network};
use super::{ResponseResult, Method};
use super::{ResponseResult, DynMethod};
/// [`Payload`] kind. Used to determinate the way for sending request.
pub enum Kind {
Simple,
Json(String),
Multipart(multipart::Form),
}
pub trait Payload {
// NOTE: This payload doesn't use `Method` and reinvent `type Output`
// because the trait `Method` cannot be made into an object.
type Output;
fn method(&self) -> &str;
pub trait Payload: DynMethod {
fn kind(&self) -> Kind;
}

View file

@ -7,9 +7,9 @@ pub trait Payload: Serialize + Method {}
/// Ready-to-send telegram request.
///
/// Note: params will be sent to telegram using [`json`]
/// Note: params will be sent to telegram using [`application/json`]
///
/// [`json`]: // TODO: libk to tgdoc
/// [`application/json`]: https://core.telegram.org/bots/api#making-requests
#[must_use = "requests do nothing until sent"]
pub struct Request<'b, P> {
bot: &'b Bot,

View file

@ -118,7 +118,24 @@ pub trait Method {
const METHOD: &'static str;
}
pub mod json;
pub mod multipart;
pub mod simple;
pub mod dynamic;
/// Signature of telegram method.
///
/// Note: this trait is very similar to [`Method`] trait, however it can be used
/// as trait object.
pub trait DynMethod {
type Output;
/// Return name of the method.
fn method(&self) -> &str;
}
impl<T> DynMethod for T
where
T: Method
{
type Output = T::Output;
fn method(&self) -> &str {
T::METHOD
}
}

View file

@ -12,7 +12,7 @@ pub trait Payload: Method {
///
/// Note: params will be sent to telegram using [`multipart/form-data`]
///
/// [`multipart/form-data`]: // TODO: libk to tgdoc
/// [`multipart/form-data`]: https://core.telegram.org/bots/api#making-requests
#[must_use = "requests do nothing until sent"]
pub struct Request<'b, P> {
bot: &'b Bot,