mirror of
https://github.com/teloxide/teloxide.git
synced 2025-03-24 15:49:27 +01:00
File refactor: InputMedia: remove derive(Deserialize), add
files`
This commit is contained in:
parent
c92190e313
commit
96266228d8
1 changed files with 41 additions and 20 deletions
|
@ -1,3 +1,5 @@
|
||||||
|
use std::iter;
|
||||||
|
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
|
|
||||||
use crate::types::{InputFile, MessageEntity, ParseMode};
|
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.
|
/// This object represents the content of a media message to be sent.
|
||||||
///
|
///
|
||||||
/// [The official docs](https://core.telegram.org/bots/api#inputmedia).
|
/// [The official docs](https://core.telegram.org/bots/api#inputmedia).
|
||||||
#[derive(Clone, Debug, Serialize, Deserialize)]
|
#[derive(Clone, Debug, Serialize)]
|
||||||
#[serde(tag = "type")]
|
#[serde(tag = "type")]
|
||||||
#[serde(rename_all = "snake_case")]
|
#[serde(rename_all = "snake_case")]
|
||||||
pub enum InputMedia {
|
pub enum InputMedia {
|
||||||
|
@ -20,7 +22,7 @@ pub enum InputMedia {
|
||||||
///
|
///
|
||||||
/// [The official docs](https://core.telegram.org/bots/api#inputmediaphoto).
|
/// [The official docs](https://core.telegram.org/bots/api#inputmediaphoto).
|
||||||
#[serde_with_macros::skip_serializing_none]
|
#[serde_with_macros::skip_serializing_none]
|
||||||
#[derive(Clone, Debug, Serialize, Deserialize)]
|
#[derive(Clone, Debug, Serialize)]
|
||||||
pub struct InputMediaPhoto {
|
pub struct InputMediaPhoto {
|
||||||
/// File to send.
|
/// File to send.
|
||||||
pub media: InputFile,
|
pub media: InputFile,
|
||||||
|
@ -82,7 +84,7 @@ impl InputMediaPhoto {
|
||||||
///
|
///
|
||||||
/// [The official docs](https://core.telegram.org/bots/api#inputmediavideo).
|
/// [The official docs](https://core.telegram.org/bots/api#inputmediavideo).
|
||||||
#[serde_with_macros::skip_serializing_none]
|
#[serde_with_macros::skip_serializing_none]
|
||||||
#[derive(Clone, Debug, Serialize, Deserialize)]
|
#[derive(Clone, Debug, Serialize)]
|
||||||
pub struct InputMediaVideo {
|
pub struct InputMediaVideo {
|
||||||
// File to send.
|
// File to send.
|
||||||
pub media: InputFile,
|
pub media: InputFile,
|
||||||
|
@ -194,7 +196,7 @@ impl InputMediaVideo {
|
||||||
///
|
///
|
||||||
/// [The official docs](https://core.telegram.org/bots/api#inputmediaanimation).
|
/// [The official docs](https://core.telegram.org/bots/api#inputmediaanimation).
|
||||||
#[serde_with_macros::skip_serializing_none]
|
#[serde_with_macros::skip_serializing_none]
|
||||||
#[derive(Clone, Debug, Serialize, Deserialize)]
|
#[derive(Clone, Debug, Serialize)]
|
||||||
pub struct InputMediaAnimation {
|
pub struct InputMediaAnimation {
|
||||||
/// File to send.
|
/// File to send.
|
||||||
pub media: InputFile,
|
pub media: InputFile,
|
||||||
|
@ -296,7 +298,7 @@ impl InputMediaAnimation {
|
||||||
///
|
///
|
||||||
/// [The official docs](https://core.telegram.org/bots/api#inputmediaaudio).
|
/// [The official docs](https://core.telegram.org/bots/api#inputmediaaudio).
|
||||||
#[serde_with_macros::skip_serializing_none]
|
#[serde_with_macros::skip_serializing_none]
|
||||||
#[derive(Clone, Debug, Serialize, Deserialize)]
|
#[derive(Clone, Debug, Serialize)]
|
||||||
pub struct InputMediaAudio {
|
pub struct InputMediaAudio {
|
||||||
/// File to send.
|
/// File to send.
|
||||||
pub media: InputFile,
|
pub media: InputFile,
|
||||||
|
@ -404,7 +406,7 @@ impl InputMediaAudio {
|
||||||
///
|
///
|
||||||
/// [The official docs](https://core.telegram.org/bots/api#inputmediadocument).
|
/// [The official docs](https://core.telegram.org/bots/api#inputmediadocument).
|
||||||
#[serde_with_macros::skip_serializing_none]
|
#[serde_with_macros::skip_serializing_none]
|
||||||
#[derive(Clone, Debug, Serialize, Deserialize)]
|
#[derive(Clone, Debug, Serialize)]
|
||||||
pub struct InputMediaDocument {
|
pub struct InputMediaDocument {
|
||||||
/// File to send.
|
/// File to send.
|
||||||
pub media: InputFile,
|
pub media: InputFile,
|
||||||
|
@ -494,15 +496,34 @@ impl From<InputMedia> for InputFile {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl InputMedia {
|
impl InputMedia {
|
||||||
#[allow(dead_code)]
|
/// Returns an iterator of all files in this input media
|
||||||
pub(crate) fn media(&self) -> &InputFile {
|
pub(crate) fn files(&self) -> impl Iterator<Item = &InputFile> {
|
||||||
match self {
|
use InputMedia::*;
|
||||||
InputMedia::Photo(InputMediaPhoto { media, .. })
|
|
||||||
| InputMedia::Document(InputMediaDocument { media, .. })
|
let (media, thumb) = match self {
|
||||||
| InputMedia::Audio(InputMediaAudio { media, .. })
|
Photo(InputMediaPhoto { media, .. }) => (media, None),
|
||||||
| InputMedia::Animation(InputMediaAnimation { media, .. })
|
Document(InputMediaDocument { media, thumb, .. })
|
||||||
| InputMedia::Video(InputMediaVideo { media, .. }) => media,
|
| 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<Item = &mut InputFile> {
|
||||||
|
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() {
|
fn photo_serialize() {
|
||||||
let expected_json = r#"{"type":"photo","media":{"FileId":"123456"}}"#;
|
let expected_json = r#"{"type":"photo","media":{"FileId":"123456"}}"#;
|
||||||
let photo = InputMedia::Photo(InputMediaPhoto {
|
let photo = InputMedia::Photo(InputMediaPhoto {
|
||||||
media: InputFile::FileId(String::from("123456")),
|
media: InputFile::file_id("123456"),
|
||||||
caption: None,
|
caption: None,
|
||||||
parse_mode: None,
|
parse_mode: None,
|
||||||
caption_entities: None,
|
caption_entities: None,
|
||||||
|
@ -528,7 +549,7 @@ mod tests {
|
||||||
fn video_serialize() {
|
fn video_serialize() {
|
||||||
let expected_json = r#"{"type":"video","media":{"FileId":"123456"}}"#;
|
let expected_json = r#"{"type":"video","media":{"FileId":"123456"}}"#;
|
||||||
let video = InputMedia::Video(InputMediaVideo {
|
let video = InputMedia::Video(InputMediaVideo {
|
||||||
media: InputFile::FileId(String::from("123456")),
|
media: InputFile::file_id("123456"),
|
||||||
thumb: None,
|
thumb: None,
|
||||||
caption: None,
|
caption: None,
|
||||||
parse_mode: None,
|
parse_mode: None,
|
||||||
|
@ -547,7 +568,7 @@ mod tests {
|
||||||
fn animation_serialize() {
|
fn animation_serialize() {
|
||||||
let expected_json = r#"{"type":"animation","media":{"FileId":"123456"}}"#;
|
let expected_json = r#"{"type":"animation","media":{"FileId":"123456"}}"#;
|
||||||
let video = InputMedia::Animation(InputMediaAnimation {
|
let video = InputMedia::Animation(InputMediaAnimation {
|
||||||
media: InputFile::FileId(String::from("123456")),
|
media: InputFile::file_id("123456"),
|
||||||
thumb: None,
|
thumb: None,
|
||||||
caption: None,
|
caption: None,
|
||||||
parse_mode: None,
|
parse_mode: None,
|
||||||
|
@ -565,7 +586,7 @@ mod tests {
|
||||||
fn audio_serialize() {
|
fn audio_serialize() {
|
||||||
let expected_json = r#"{"type":"audio","media":{"FileId":"123456"}}"#;
|
let expected_json = r#"{"type":"audio","media":{"FileId":"123456"}}"#;
|
||||||
let video = InputMedia::Audio(InputMediaAudio {
|
let video = InputMedia::Audio(InputMediaAudio {
|
||||||
media: InputFile::FileId(String::from("123456")),
|
media: InputFile::file_id("123456"),
|
||||||
thumb: None,
|
thumb: None,
|
||||||
caption: None,
|
caption: None,
|
||||||
parse_mode: None,
|
parse_mode: None,
|
||||||
|
@ -583,7 +604,7 @@ mod tests {
|
||||||
fn document_serialize() {
|
fn document_serialize() {
|
||||||
let expected_json = r#"{"type":"document","media":{"FileId":"123456"}}"#;
|
let expected_json = r#"{"type":"document","media":{"FileId":"123456"}}"#;
|
||||||
let video = InputMedia::Document(InputMediaDocument {
|
let video = InputMedia::Document(InputMediaDocument {
|
||||||
media: InputFile::FileId(String::from("123456")),
|
media: InputFile::file_id("123456"),
|
||||||
thumb: None,
|
thumb: None,
|
||||||
caption: None,
|
caption: None,
|
||||||
parse_mode: None,
|
parse_mode: None,
|
||||||
|
|
Loading…
Add table
Reference in a new issue