From f984428169705edc0e6fb25bdea5851710768d9d Mon Sep 17 00:00:00 2001 From: YoursFunny Date: Thu, 24 Sep 2026 04:03:59 +0800 Subject: [PATCH] refactor(photo): fold photo_plan into its only caller photo_plan wrapped the header dispatch for exactly one consumer (decode_budget_bytes); plan_photo itself has three callers and stays. The dispatch is inlined into the budget function with its doc merged, saving the wrapper's signature, doc duplication and call indirection. --- crates/xmedia-bot/src/photo.rs | 26 +++++++++++--------------- 1 file changed, 11 insertions(+), 15 deletions(-) diff --git a/crates/xmedia-bot/src/photo.rs b/crates/xmedia-bot/src/photo.rs index 81338e7..7720777 100644 --- a/crates/xmedia-bot/src/photo.rs +++ b/crates/xmedia-bot/src/photo.rs @@ -119,24 +119,20 @@ fn plan_photo(w: u32, h: u32, len: usize, channels: usize) -> PhotoPlan { } } -/// [`PhotoPlan`] from the downloaded bytes: the PNG or JPEG header decides -/// (palette counted as RGB, which `EXPAND` produces); anything else is uploaded -/// as-is, since [`prepare_photo`] does not decode it. -fn photo_plan(bytes: &[u8]) -> PhotoPlan { - if let Some((w, h, _depth, color)) = parse_png_header(bytes) { - return plan_photo(w, h, bytes.len(), output_channels(color)); - } - if let Some((w, h)) = jpeg_dims(bytes) { - return plan_photo(w, h, bytes.len(), 3); - } - PhotoPlan::AsIs -} - /// The decode buffer a downloaded photo will allocate, from its header alone — /// zero when it is already within Telegram's limits and is uploaded as-is, zero -/// for a format [`prepare_photo`] does not decode. +/// for a format [`prepare_photo`] does not decode. The PNG or JPEG header +/// decides (palette counted as RGB, which `EXPAND` produces); anything else is +/// uploaded as-is, since [`prepare_photo`] does not decode it. pub(crate) fn decode_budget_bytes(bytes: &[u8]) -> u64 { - match photo_plan(bytes) { + let plan = if let Some((w, h, _depth, color)) = parse_png_header(bytes) { + plan_photo(w, h, bytes.len(), output_channels(color)) + } else if let Some((w, h)) = jpeg_dims(bytes) { + plan_photo(w, h, bytes.len(), 3) + } else { + PhotoPlan::AsIs + }; + match plan { PhotoPlan::Decode(bytes) => bytes, PhotoPlan::AsIs | PhotoPlan::TooLarge => 0, }