added new type keyboard_button_poll_type.rs

This commit is contained in:
p0lunin 2020-02-04 22:59:44 +02:00
parent fb3f0558c2
commit 357e2640f4
4 changed files with 28 additions and 3 deletions

View file

@ -1,6 +1,6 @@
use serde::{de::Error, Deserialize, Deserializer, Serialize, Serializer}; use serde::{de::Error, Deserialize, Deserializer, Serialize, Serializer};
use crate::types::True; use crate::types::{True, KeyboardButtonPollType};
/// This object represents one button of the reply keyboard. For filter text /// This object represents one button of the reply keyboard. For filter text
/// buttons String can be used instead of this object to specify text of the /// buttons String can be used instead of this object to specify text of the
@ -24,10 +24,11 @@ pub struct KeyboardButton {
} }
// Serialize + Deserialize are implemented by hand // Serialize + Deserialize are implemented by hand
#[derive(Copy, Clone, Debug, Eq, Hash, PartialEq)] #[derive(Clone, Debug, Eq, Hash, PartialEq)]
pub enum ButtonRequest { pub enum ButtonRequest {
Location, Location,
Contact, Contact,
KeyboardButtonPollType(KeyboardButtonPollType)
} }
/// Helper struct for (de)serializing [`ButtonRequest`](ButtonRequest) /// Helper struct for (de)serializing [`ButtonRequest`](ButtonRequest)
@ -43,6 +44,10 @@ struct RawRequest {
/// button is pressed. Available in private chats only /// button is pressed. Available in private chats only
#[serde(rename = "request_location")] #[serde(rename = "request_location")]
location: Option<True>, location: Option<True>,
/// Optional. If specified, the user will be asked to create a poll and send it to the bot when the button is pressed. Available in private chats only
#[serde(rename = "request_poll")]
poll: Option<KeyboardButtonPollType>,
} }
impl<'de> Deserialize<'de> for ButtonRequest { impl<'de> Deserialize<'de> for ButtonRequest {
@ -55,6 +60,7 @@ impl<'de> Deserialize<'de> for ButtonRequest {
RawRequest { RawRequest {
contact: Some(_), contact: Some(_),
location: Some(_), location: Some(_),
poll: Some(_),
} => Err(D::Error::custom( } => Err(D::Error::custom(
"`request_contact` and `request_location` fields are mutually \ "`request_contact` and `request_location` fields are mutually \
exclusive, but both were provided", exclusive, but both were provided",
@ -65,6 +71,9 @@ impl<'de> Deserialize<'de> for ButtonRequest {
RawRequest { RawRequest {
location: Some(_), .. location: Some(_), ..
} => Ok(Self::Location), } => Ok(Self::Location),
RawRequest {
poll: Some(poll_type), ..
} => Ok(Self::KeyboardButtonPollType(poll_type)),
_ => Err(D::Error::custom( _ => Err(D::Error::custom(
"Either one of `request_contact` and `request_location` \ "Either one of `request_contact` and `request_location` \
fields is required", fields is required",
@ -82,13 +91,21 @@ impl Serialize for ButtonRequest {
Self::Contact => RawRequest { Self::Contact => RawRequest {
contact: Some(True), contact: Some(True),
location: None, location: None,
poll: None,
} }
.serialize(serializer), .serialize(serializer),
Self::Location => RawRequest { Self::Location => RawRequest {
contact: None, contact: None,
location: Some(True), location: Some(True),
poll: None,
} }
.serialize(serializer), .serialize(serializer),
Self::KeyboardButtonPollType(poll_type) => RawRequest {
contact: None,
location: None,
poll: Some(poll_type.clone())
}
.serialize(serializer)
} }
} }
} }

View file

@ -0,0 +1,6 @@
use serde::{Deserialize, Serialize};
#[derive(Clone, Debug, Eq, Hash, PartialEq, Serialize, Deserialize)]
pub struct KeyboardButtonPollType {
poll_type: String
}

View file

@ -50,6 +50,7 @@ pub use input_media::*;
pub use input_message_content::*; pub use input_message_content::*;
pub use invoice::*; pub use invoice::*;
pub use keyboard_button::*; pub use keyboard_button::*;
pub use keyboard_button_poll_type::*;
pub use label_price::*; pub use label_price::*;
pub use location::*; pub use location::*;
pub use login_url::*; pub use login_url::*;
@ -113,6 +114,7 @@ mod input_media;
mod input_message_content; mod input_message_content;
mod invoice; mod invoice;
mod keyboard_button; mod keyboard_button;
mod keyboard_button_poll_type;
mod label_price; mod label_price;
mod location; mod location;
mod login_url; mod login_url;

View file

@ -1,6 +1,6 @@
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)] #[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")] #[serde(rename_all = "lowercase")]
pub enum PollType { pub enum PollType {
Quiz, Quiz,