Skip to content

Commit

Permalink
make allowed_update type safe (#65)
Browse files Browse the repository at this point in the history
* make allowed_update type safe

* minor adjustments to the AllowedUpdates enum

* cargo fmt

* simplify examples

* remove unused import
  • Loading branch information
johannesvollmer authored May 11, 2022
1 parent 22d3762 commit d70f11d
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 12 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ let api = Api::new(token);

```rust
let update_params = GetUpdatesParams::builder()
.allowed_updates(vec!["message".to_string()])
.allowed_updates(vec![AllowedUpdate::Message])
.build();

let result = api.get_updates(&update_params);
Expand Down
3 changes: 1 addition & 2 deletions examples/async_reply_to_message_updates.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@ static TOKEN: &str = "API_TOKEN";
async fn main() {
let api = AsyncApi::new(TOKEN);

let update_params_builder =
GetUpdatesParams::builder().allowed_updates(vec!["message".to_string()]);
let update_params_builder = GetUpdatesParams::builder();
let mut update_params = update_params_builder.clone().build();

loop {
Expand Down
3 changes: 1 addition & 2 deletions examples/reply_to_message_updates.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@ static TOKEN: &str = "API_TOKEN";
fn main() {
let api = Api::new(TOKEN);

let update_params_builder =
GetUpdatesParams::builder().allowed_updates(vec!["message".to_string()]);
let update_params_builder = GetUpdatesParams::builder();
let mut update_params = update_params_builder.clone().build();

loop {
Expand Down
5 changes: 3 additions & 2 deletions src/api/telegram_api_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,7 @@ mod tests {
use crate::objects::BotCommand;
use crate::objects::ChatPermissions;
use crate::objects::InlineQueryResultVenue;
use crate::AllowedUpdate;

#[test]
fn new_sets_correct_url() {
Expand All @@ -269,7 +270,7 @@ mod tests {
fn get_updates_success() {
let response_string = "{\"ok\":true,\"result\":[{\"update_id\":379656753,\"message\":{\"message_id\":2741,\"from\":{\"id\":275808073,\"is_bot\":false,\"first_name\":\"Ayrat\",\"last_name\":\"Badykov\",\"username\":\"Ayrat555\",\"language_code\":\"en\"},\"date\":1618149703,\"chat\":{\"id\":275808073,\"type\":\"private\",\"username\":\"Ayrat555\",\"first_name\":\"Ayrat\",\"last_name\":\"Badykov\"},\"text\":\"dsaf\"}}]}";
let params = GetUpdatesParams::builder()
.allowed_updates(vec!["message".to_string()])
.allowed_updates(vec![AllowedUpdate::Message])
.build();

let _m = mockito::mock("POST", "/getUpdates")
Expand Down Expand Up @@ -1696,7 +1697,7 @@ mod tests {
fn returns_decode_error_if_response_can_not_be_decoded() {
let response_string = "{hey this json is invalid}";
let params = GetUpdatesParams::builder()
.allowed_updates(vec!["message".to_string()])
.allowed_updates(vec![AllowedUpdate::Message])
.build();

let _m = mockito::mock("POST", "/getUpdates")
Expand Down
6 changes: 3 additions & 3 deletions src/api_params.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use crate::objects::{
PassportElementErrorTranslationFiles, PassportElementErrorUnspecified, PollType,
ReplyKeyboardMarkup, ReplyKeyboardRemove, ShippingOption,
};
use crate::ParseMode;
use crate::{AllowedUpdate, ParseMode};
use serde::Deserialize;
use serde::Serialize;
use std::path::PathBuf;
Expand Down Expand Up @@ -245,7 +245,7 @@ pub struct GetUpdatesParams {

#[serde(skip_serializing_if = "Option::is_none")]
#[builder(setter(into, strip_option), default)]
pub allowed_updates: Option<Vec<String>>,
pub allowed_updates: Option<Vec<AllowedUpdate>>,
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Builder)]
Expand All @@ -267,7 +267,7 @@ pub struct SetWebhookParams {

#[serde(skip_serializing_if = "Option::is_none")]
#[builder(setter(into, strip_option), default)]
pub allowed_updates: Option<Vec<String>>,
pub allowed_updates: Option<Vec<AllowedUpdate>>,

#[serde(skip_serializing_if = "Option::is_none")]
#[builder(setter(into, strip_option), default)]
Expand Down
27 changes: 25 additions & 2 deletions src/objects.rs
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,7 @@ pub struct Update {
pub update_id: u32,

/// Maps to exactly one of the many optional fields
/// from [the official documentation.](https://core.telegram.org/bots/api#update).
/// from [the official documentation](https://core.telegram.org/bots/api#update).
#[serde(flatten)]
pub content: UpdateContent,
}
Expand Down Expand Up @@ -330,7 +330,30 @@ pub struct WebhookInfo {

#[serde(skip_serializing_if = "Option::is_none")]
#[builder(setter(into, strip_option), default)]
pub allowed_updates: Option<Vec<String>>,
pub allowed_updates: Option<Vec<AllowedUpdate>>,
}

/// Control which updates to receive.
/// Specify an empty list to receive all update types except `ChatMember`.
/// [Official documentation](https://core.telegram.org/bots/api#getupdates).
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
#[non_exhaustive]
pub enum AllowedUpdate {
Message,
EditedMessage,
ChannelPost,
EditedChannelPost,
InlineQuery,
ChosenInlineResult,
CallbackQuery,
ShippingQuery,
PreCheckoutQuery,
Poll,
PollAnswer,
MyChatMember,
ChatMember,
ChatJoinRequest,
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Builder)]
Expand Down

0 comments on commit d70f11d

Please sign in to comment.