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

View file

@ -1,25 +1,18 @@
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)]
pub struct KeyboardButtonPollType {
poll_type: String,
}
impl KeyboardButtonPollType {
pub fn new<S>(poll_type: S) -> Self
where
S: Into<String>,
{
Self {
poll_type: poll_type.into(),
}
}
pub fn poll_type<S>(mut self, val: S) -> Self
where
S: Into<String>,
{
self.poll_type = val.into();
self
}
#[serde(rename_all = "snake_case")]
#[serde(tag = "type")]
pub enum KeyboardButtonPollType {
/// If [`Quiz`] is passed, the user will be allowed to create only polls in
/// the quiz mode.
Quiz,
/// If [`Regular`] is passed, only regular polls will be allowed.
Regular,
/// If [`Any`] is passed, the user will be allowed to create a poll of any
/// type.
#[serde(rename = "")]
Any,
}