Refactor KeyboardButtonPollType

- Make it enum
- Rename `ButtonRequest::KeyboardButtonPollType` => `<_>::poll`
This commit is contained in:
Waffle 2021-01-26 16:02:05 +03:00
parent 393f6ee7a4
commit 09d0d886fa
2 changed files with 17 additions and 24 deletions

View file

@ -49,7 +49,7 @@ impl KeyboardButton {
pub enum ButtonRequest { pub enum ButtonRequest {
Location, Location,
Contact, Contact,
KeyboardButtonPollType(KeyboardButtonPollType), Poll(KeyboardButtonPollType),
} }
/// Helper struct for (de)serializing [`ButtonRequest`](ButtonRequest) /// Helper struct for (de)serializing [`ButtonRequest`](ButtonRequest)
@ -97,7 +97,7 @@ impl<'de> Deserialize<'de> for ButtonRequest {
RawRequest { RawRequest {
poll: Some(poll_type), poll: Some(poll_type),
.. ..
} => Ok(Self::KeyboardButtonPollType(poll_type)), } => Ok(Self::Poll(poll_type)),
_ => Err(D::Error::custom( _ => Err(D::Error::custom(
"Either one of `request_contact` and `request_location` fields is required", "Either one of `request_contact` and `request_location` fields is required",
)), )),
@ -123,7 +123,7 @@ impl Serialize for ButtonRequest {
poll: None, poll: None,
} }
.serialize(serializer), .serialize(serializer),
Self::KeyboardButtonPollType(poll_type) => RawRequest { Self::Poll(poll_type) => RawRequest {
contact: None, contact: None,
location: None, location: None,
poll: Some(poll_type.clone()), poll: Some(poll_type.clone()),

View file

@ -1,25 +1,18 @@
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
/// This object represents type of a poll, which is allowed to be created and
/// sent when the corresponding button is pressed.
#[derive(Clone, Debug, Eq, Hash, PartialEq, Serialize, Deserialize)] #[derive(Clone, Debug, Eq, Hash, PartialEq, Serialize, Deserialize)]
pub struct KeyboardButtonPollType { #[serde(rename_all = "snake_case")]
poll_type: String, #[serde(tag = "type")]
} pub enum KeyboardButtonPollType {
/// If [`Quiz`] is passed, the user will be allowed to create only polls in
impl KeyboardButtonPollType { /// the quiz mode.
pub fn new<S>(poll_type: S) -> Self Quiz,
where /// If [`Regular`] is passed, only regular polls will be allowed.
S: Into<String>, Regular,
{ /// If [`Any`] is passed, the user will be allowed to create a poll of any
Self { /// type.
poll_type: poll_type.into(), #[serde(rename = "")]
} Any,
}
pub fn poll_type<S>(mut self, val: S) -> Self
where
S: Into<String>,
{
self.poll_type = val.into();
self
}
} }