-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
178 additions
and
136 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,7 @@ | ||
mod button; | ||
mod calendar; | ||
|
||
pub use self::button::button_schema; | ||
pub use self::{ | ||
button::button_schema, | ||
calendar::{calendar_schema, CalendarSchemaTypes}, | ||
}; |
93 changes: 93 additions & 0 deletions
93
crates/teloxide-inline-widgets-macros/src/schemes/calendar.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
use proc_macro2::TokenStream as TokenStream2; | ||
use quote::quote; | ||
use syn::{Ident, Path}; | ||
|
||
use crate::attribute_parameters::CalendarParameters; | ||
|
||
pub struct CalendarSchemaTypes { | ||
pub widget_ty: Ident, | ||
pub bot_ty: Path, | ||
pub dialogue_ty: Path, | ||
} | ||
|
||
/// Handler schema for the [`Calendar`] widget | ||
pub fn calendar_schema( | ||
CalendarSchemaTypes { widget_ty, bot_ty, dialogue_ty }: &CalendarSchemaTypes, | ||
CalendarParameters { | ||
day_click_handler, | ||
day_prefix, | ||
weekday_prefix, | ||
weekday_click_handler, | ||
prev_month, | ||
next_month, | ||
prev_year, | ||
next_year, | ||
noop_data, | ||
}: &CalendarParameters, | ||
) -> TokenStream2 { | ||
let weekday_click_handler = if let Some(weekday_click_handler) = weekday_click_handler { | ||
quote! { | ||
.branch( | ||
dptree::filter_map(|CallbackQueryData(cq_data): CallbackQueryData| { | ||
Weekday::try_from( | ||
cq_data.strip_prefix(#weekday_prefix)?.parse::<u8>().ok()? | ||
).ok() | ||
}) | ||
.endpoint(#weekday_click_handler) | ||
) | ||
} | ||
} else { | ||
quote! {} | ||
}; | ||
|
||
quote! { | ||
.branch( | ||
dptree::entry() | ||
.filter_map(|cq: CallbackQuery| cq.data.map(|data| CallbackQueryData(data))) | ||
.branch( | ||
dptree::filter_map(|CallbackQueryData(cq_data): CallbackQueryData| { | ||
NaiveDate::parse_from_str( | ||
cq_data.strip_prefix(#day_prefix)?, | ||
"%Y/%m/%d", | ||
) | ||
.ok() | ||
}) | ||
.endpoint(#day_click_handler) | ||
) | ||
#weekday_click_handler | ||
.filter_map(|cq: CallbackQuery| cq.message.map(|msg| (msg.chat.id, msg.id, cq.id))) | ||
.endpoint( | ||
| | ||
mut widget: #widget_ty, | ||
bot: #bot_ty, | ||
dialogue: #dialogue_ty, | ||
(chat_id, message_id, cq_id): (ChatId, MessageId, String), | ||
CallbackQueryData(cq_data): CallbackQueryData, | ||
widget_styles: WidgetStyles | ||
| async move { | ||
bot.answer_callback_query(cq_id).await?; | ||
|
||
if cq_data == #noop_data { | ||
// TODO possibly custom notification here? | ||
return Ok(()) | ||
} | ||
|
||
let calendar = widget.get_widget(); | ||
match cq_data.as_str() { | ||
#prev_year => calendar.set_previous_year(), | ||
#next_year => calendar.set_next_year(), | ||
#prev_month => calendar.set_previous_month(), | ||
#next_month => calendar.set_next_month(), | ||
_ => { | ||
log::warn!("`Calendar` widget received strange `CallbackQuery::data`: \"{cq_data}\""); | ||
} | ||
} | ||
widget.redraw(&bot, chat_id, message_id, &widget_styles).await?; | ||
widget.update_state(&dialogue).await?; | ||
|
||
Ok(()) | ||
}, | ||
), | ||
) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,11 @@ | ||
pub use chrono::{NaiveDate, Weekday}; | ||
pub use log; | ||
pub use teloxide::types::{CallbackQuery, MessageId}; | ||
pub use teloxide_inline_widgets_macros::InlineWidget; | ||
|
||
pub use crate::{ | ||
layout::{Layout, LayoutOrientation}, | ||
traits::{GetSize, InlineWidget, WidgetContainer}, | ||
types::{Size, WidgetStyles}, | ||
types::{CallbackQueryData, Size, WidgetStyles}, | ||
widgets::{CalendarSchemaParameters, CheckboxListSchemaParameters, RadioListSchemaParameters}, | ||
}; | ||
|
||
pub use teloxide_inline_widgets_macros::InlineWidget; |
2 changes: 2 additions & 0 deletions
2
crates/teloxide-inline-widgets/src/types/callback_query_data.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
#[derive(Debug, Clone)] | ||
pub struct CallbackQueryData(pub String); |
Oops, something went wrong.