From 42c961b595321ec301a61bc5c61d70e64db54df5 Mon Sep 17 00:00:00 2001 From: Waffle Date: Wed, 4 Sep 2019 11:00:15 +0300 Subject: [PATCH] Implement request builders by hand instead of `TypedBuilder` --- Cargo.toml | 3 +- src/core/requests/get_me.rs | 8 +++- src/core/requests/send_message.rs | 71 +++++++++++++++++++++++++------ src/lib.rs | 2 - 4 files changed, 66 insertions(+), 18 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index b3fcc9b1..ec12fd2f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -12,5 +12,4 @@ serde_json = "1.0.39" serde = {version = "1.0.92", features = ["derive"] } lazy_static = "1.3" apply = "0.2.2" -typed-builder = "0.3.0" -derive_more = "0.15.0" \ No newline at end of file +derive_more = "0.15.0" diff --git a/src/core/requests/get_me.rs b/src/core/requests/get_me.rs index 2db453dc..6633d77a 100644 --- a/src/core/requests/get_me.rs +++ b/src/core/requests/get_me.rs @@ -2,7 +2,7 @@ use crate::core::network; use crate::core::requests::{Request, RequestFuture, RequestInfo, ResponseResult}; use crate::core::types::User; -#[derive(Debug, Constructor, TypedBuilder)] +#[derive(Debug)] pub struct GetMe<'a> { info: RequestInfo<'a>, } @@ -16,3 +16,9 @@ impl<'a> Request<'a> for GetMe<'a> { }) } } + +impl<'a> GetMe<'a> { + pub(crate) fn new(info: RequestInfo<'a>) -> Self { + GetMe { info } + } +} diff --git a/src/core/requests/send_message.rs b/src/core/requests/send_message.rs index bcc0cf1c..9df3ae59 100644 --- a/src/core/requests/send_message.rs +++ b/src/core/requests/send_message.rs @@ -2,23 +2,18 @@ use crate::core::requests::form_builder::FormBuilder; use crate::core::requests::{ChatId, Request, RequestFuture, RequestInfo, ResponseResult}; use crate::core::{network, types::Message}; -#[derive(Debug, TypedBuilder)] +#[derive(Debug)] pub struct SendMessage<'a> { info: RequestInfo<'a>, - chat_id: ChatId, - text: String, + pub chat_id: ChatId, + pub text: String, - #[builder(default)] - parse_mode: Option, // TODO: ParseMode enum - #[builder(default)] - disable_web_page_preview: Option, - #[builder(default)] - disable_notification: Option, - #[builder(default)] - reply_to_message_id: Option, - #[builder(default)] - reply_markup: Option<()>, // TODO: ReplyMarkup enum + pub parse_mode: Option, // TODO: ParseMode enum + pub disable_web_page_preview: Option, + pub disable_notification: Option, + pub reply_to_message_id: Option, + pub reply_markup: Option<()>, // TODO: ReplyMarkup enum } impl<'a> Request<'a> for SendMessage<'a> { @@ -48,3 +43,53 @@ impl<'a> Request<'a> for SendMessage<'a> { }) } } + +impl<'a> SendMessage<'a> { + pub(crate) fn new(info: RequestInfo<'a>, chat_id: ChatId, text: String) -> Self { + SendMessage { + info, + chat_id, + text, + parse_mode: None, + disable_web_page_preview: None, + disable_notification: None, + reply_to_message_id: None, + reply_markup: None, + } + } + + pub fn chat_id>(mut self, val: T) -> Self { + self.chat_id = val.into(); + self + } + + pub fn text>(mut self, val: T) -> Self { + self.text = val.into(); + self + } + + pub fn parse_mode>(mut self, val: T) -> Self { + self.parse_mode = Some(val.into()); + self + } + + pub fn disable_web_page_preview>(mut self, val: T) -> Self { + self.disable_web_page_preview = Some(val.into()); + self + } + + pub fn disable_notification>(mut self, val: T) -> Self { + self.disable_notification = Some(val.into()); + self + } + + pub fn reply_to_message_id>(mut self, val: T) -> Self { + self.reply_to_message_id = Some(val.into()); + self + } + + pub fn reply_markup>(mut self, val: T) -> Self { + self.reply_markup = Some(val.into()); + self + } +} diff --git a/src/lib.rs b/src/lib.rs index c9bdacec..d8ba1c5f 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -2,7 +2,5 @@ extern crate derive_more; #[macro_use] extern crate serde; -#[macro_use] -extern crate typed_builder; pub mod core;