diff --git a/src/types/input_media.rs b/src/types/input_media.rs index 1cae6649..4ef04ab9 100644 --- a/src/types/input_media.rs +++ b/src/types/input_media.rs @@ -1,3 +1,5 @@ +use std::iter; + use serde::{Deserialize, Serialize}; use crate::types::{InputFile, MessageEntity, ParseMode}; @@ -5,7 +7,7 @@ use crate::types::{InputFile, MessageEntity, ParseMode}; /// This object represents the content of a media message to be sent. /// /// [The official docs](https://core.telegram.org/bots/api#inputmedia). -#[derive(Clone, Debug, Serialize, Deserialize)] +#[derive(Clone, Debug, Serialize)] #[serde(tag = "type")] #[serde(rename_all = "snake_case")] pub enum InputMedia { @@ -20,7 +22,7 @@ pub enum InputMedia { /// /// [The official docs](https://core.telegram.org/bots/api#inputmediaphoto). #[serde_with_macros::skip_serializing_none] -#[derive(Clone, Debug, Serialize, Deserialize)] +#[derive(Clone, Debug, Serialize)] pub struct InputMediaPhoto { /// File to send. pub media: InputFile, @@ -82,7 +84,7 @@ impl InputMediaPhoto { /// /// [The official docs](https://core.telegram.org/bots/api#inputmediavideo). #[serde_with_macros::skip_serializing_none] -#[derive(Clone, Debug, Serialize, Deserialize)] +#[derive(Clone, Debug, Serialize)] pub struct InputMediaVideo { // File to send. pub media: InputFile, @@ -194,7 +196,7 @@ impl InputMediaVideo { /// /// [The official docs](https://core.telegram.org/bots/api#inputmediaanimation). #[serde_with_macros::skip_serializing_none] -#[derive(Clone, Debug, Serialize, Deserialize)] +#[derive(Clone, Debug, Serialize)] pub struct InputMediaAnimation { /// File to send. pub media: InputFile, @@ -296,7 +298,7 @@ impl InputMediaAnimation { /// /// [The official docs](https://core.telegram.org/bots/api#inputmediaaudio). #[serde_with_macros::skip_serializing_none] -#[derive(Clone, Debug, Serialize, Deserialize)] +#[derive(Clone, Debug, Serialize)] pub struct InputMediaAudio { /// File to send. pub media: InputFile, @@ -404,7 +406,7 @@ impl InputMediaAudio { /// /// [The official docs](https://core.telegram.org/bots/api#inputmediadocument). #[serde_with_macros::skip_serializing_none] -#[derive(Clone, Debug, Serialize, Deserialize)] +#[derive(Clone, Debug, Serialize)] pub struct InputMediaDocument { /// File to send. pub media: InputFile, @@ -494,15 +496,34 @@ impl From for InputFile { } impl InputMedia { - #[allow(dead_code)] - pub(crate) fn media(&self) -> &InputFile { - match self { - InputMedia::Photo(InputMediaPhoto { media, .. }) - | InputMedia::Document(InputMediaDocument { media, .. }) - | InputMedia::Audio(InputMediaAudio { media, .. }) - | InputMedia::Animation(InputMediaAnimation { media, .. }) - | InputMedia::Video(InputMediaVideo { media, .. }) => media, - } + /// Returns an iterator of all files in this input media + pub(crate) fn files(&self) -> impl Iterator { + use InputMedia::*; + + let (media, thumb) = match self { + Photo(InputMediaPhoto { media, .. }) => (media, None), + Document(InputMediaDocument { media, thumb, .. }) + | Audio(InputMediaAudio { media, thumb, .. }) + | Animation(InputMediaAnimation { media, thumb, .. }) + | Video(InputMediaVideo { media, thumb, .. }) => (media, thumb.as_ref()), + }; + + iter::once(media).chain(thumb) + } + + /// Returns an iterator of all files in this input media + pub(crate) fn files_mut(&mut self) -> impl Iterator { + use InputMedia::*; + + let (media, thumb) = match self { + Photo(InputMediaPhoto { media, .. }) => (media, None), + Document(InputMediaDocument { media, thumb, .. }) + | Audio(InputMediaAudio { media, thumb, .. }) + | Animation(InputMediaAnimation { media, thumb, .. }) + | Video(InputMediaVideo { media, thumb, .. }) => (media, thumb.as_mut()), + }; + + iter::once(media).chain(thumb) } } @@ -514,7 +535,7 @@ mod tests { fn photo_serialize() { let expected_json = r#"{"type":"photo","media":{"FileId":"123456"}}"#; let photo = InputMedia::Photo(InputMediaPhoto { - media: InputFile::FileId(String::from("123456")), + media: InputFile::file_id("123456"), caption: None, parse_mode: None, caption_entities: None, @@ -528,7 +549,7 @@ mod tests { fn video_serialize() { let expected_json = r#"{"type":"video","media":{"FileId":"123456"}}"#; let video = InputMedia::Video(InputMediaVideo { - media: InputFile::FileId(String::from("123456")), + media: InputFile::file_id("123456"), thumb: None, caption: None, parse_mode: None, @@ -547,7 +568,7 @@ mod tests { fn animation_serialize() { let expected_json = r#"{"type":"animation","media":{"FileId":"123456"}}"#; let video = InputMedia::Animation(InputMediaAnimation { - media: InputFile::FileId(String::from("123456")), + media: InputFile::file_id("123456"), thumb: None, caption: None, parse_mode: None, @@ -565,7 +586,7 @@ mod tests { fn audio_serialize() { let expected_json = r#"{"type":"audio","media":{"FileId":"123456"}}"#; let video = InputMedia::Audio(InputMediaAudio { - media: InputFile::FileId(String::from("123456")), + media: InputFile::file_id("123456"), thumb: None, caption: None, parse_mode: None, @@ -583,7 +604,7 @@ mod tests { fn document_serialize() { let expected_json = r#"{"type":"document","media":{"FileId":"123456"}}"#; let video = InputMedia::Document(InputMediaDocument { - media: InputFile::FileId(String::from("123456")), + media: InputFile::file_id("123456"), thumb: None, caption: None, parse_mode: None,