mirror of
https://github.com/TheFunny/TelegramTwitterMediaBot.git
synced 2026-10-02 00:42:10 +00:00
refactor(send): inline hidden_template_count into its two call sites
A one-line saturating_sub wrapper exported for one production line (the prompt text) and two assertions — the expression is shorter than its name at every use.
This commit is contained in:
@@ -907,12 +907,21 @@ mod tests {
|
|||||||
// Names are folded, not one per row.
|
// Names are folded, not one per row.
|
||||||
assert_eq!(keyboard.inline_keyboard[0].len(), 3);
|
assert_eq!(keyboard.inline_keyboard[0].len(), 3);
|
||||||
assert_eq!(keyboard.inline_keyboard.last().unwrap().len(), 2);
|
assert_eq!(keyboard.inline_keyboard.last().unwrap().len(), 2);
|
||||||
assert_eq!(super::post_send::hidden_template_count(&templates), 140);
|
assert_eq!(
|
||||||
|
templates
|
||||||
|
.len()
|
||||||
|
.saturating_sub(super::post_send::MAX_TEMPLATE_BUTTONS),
|
||||||
|
140
|
||||||
|
);
|
||||||
// Under the cap nothing is hidden and every name gets a button.
|
// Under the cap nothing is hidden and every name gets a button.
|
||||||
let few: HashMap<String, String> = (0..4)
|
let few: HashMap<String, String> = (0..4)
|
||||||
.map(|i| (format!("t{i}"), "[]".to_string()))
|
.map(|i| (format!("t{i}"), "[]".to_string()))
|
||||||
.collect();
|
.collect();
|
||||||
assert_eq!(super::post_send::hidden_template_count(&few), 0);
|
assert_eq!(
|
||||||
|
few.len()
|
||||||
|
.saturating_sub(super::post_send::MAX_TEMPLATE_BUTTONS),
|
||||||
|
0
|
||||||
|
);
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
build_edit_markup(&few)
|
build_edit_markup(&few)
|
||||||
.inline_keyboard
|
.inline_keyboard
|
||||||
|
|||||||
@@ -212,11 +212,6 @@ pub(super) fn build_edit_markup(templates: &HashMap<String, String>) -> InlineKe
|
|||||||
InlineKeyboardMarkup::new(rows)
|
InlineKeyboardMarkup::new(rows)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// How many templates the markup could not fit, for the prompt text.
|
|
||||||
pub(super) fn hidden_template_count(templates: &HashMap<String, String>) -> usize {
|
|
||||||
templates.len().saturating_sub(MAX_TEMPLATE_BUTTONS)
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Notifies a chat about a dead-lettered task (skips when `notify_chat_id` is
|
/// Notifies a chat about a dead-lettered task (skips when `notify_chat_id` is
|
||||||
/// absent).
|
/// absent).
|
||||||
pub(crate) async fn notify_failure(
|
pub(crate) async fn notify_failure(
|
||||||
@@ -282,7 +277,7 @@ pub(crate) async fn post_send_actions(ctx: &AppContext<'_>, task: &Task, message
|
|||||||
let templates = ctx.chat_store.get(chat_id).await.template;
|
let templates = ctx.chat_store.get(chat_id).await.template;
|
||||||
let keyboard = build_edit_markup(&templates);
|
let keyboard = build_edit_markup(&templates);
|
||||||
let mut text = edit_prompt_text(ctx.config.edit_message_ttl);
|
let mut text = edit_prompt_text(ctx.config.edit_message_ttl);
|
||||||
let hidden = hidden_template_count(&templates);
|
let hidden = templates.len().saturating_sub(MAX_TEMPLATE_BUTTONS);
|
||||||
if hidden > 0 {
|
if hidden > 0 {
|
||||||
// The keyboard is capped; say so instead of silently hiding them.
|
// The keyboard is capped; say so instead of silently hiding them.
|
||||||
text.push_str(&format!(
|
text.push_str(&format!(
|
||||||
|
|||||||
Reference in New Issue
Block a user