Merge pull request #253 from teloxide/flatten_meta

Replace `file_{[unique_]id,size}` fields with `FileMeta`
This commit is contained in:
Waffle Maybe 2022-10-01 15:08:39 +04:00 committed by GitHub
commit a304ea4174
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
9 changed files with 92 additions and 115 deletions

View file

@ -9,11 +9,15 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Changed ### Changed
- `Animation`, `Audio`, `Document`, `PassportFile`, `PhotoSize`, `Video`, `VideoNote` and `Voice` now contain `FileMeta` instead of its fields ([#253][pr253])
- Field access should still work via `Deref` impls
- **You can now `.await` any `Request`!** ([#249][pr249]) - **You can now `.await` any `Request`!** ([#249][pr249])
- `Request` now requires `Self: IntoFuture` - `Request` now requires `Self: IntoFuture`
- There is no need for `AutoSend` anymore - There is no need for `AutoSend` anymore
- MSRV (Minimal Supported Rust Version) was bumped from `1.58.0` to `1.64.0` - MSRV (Minimal Supported Rust Version) was bumped from `1.58.0` to `1.64.0`
[pr253]: https://github.com/teloxide/teloxide-core/pull/253
### Removed ### Removed
- Methods for creating `InlineQuery` ([#246][pr244]) - Methods for creating `InlineQuery` ([#246][pr244])

View file

@ -1,22 +1,20 @@
use derive_more::Deref;
use mime::Mime; use mime::Mime;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use crate::types::PhotoSize; use crate::types::{FileMeta, PhotoSize};
/// This object represents an animation file (GIF or H.264/MPEG-4 AVC video /// This object represents an animation file (GIF or H.264/MPEG-4 AVC video
/// without sound). /// without sound).
/// ///
/// [The official docs](https://core.telegram.org/bots/api#animation). /// [The official docs](https://core.telegram.org/bots/api#animation).
#[serde_with_macros::skip_serializing_none] #[serde_with_macros::skip_serializing_none]
#[derive(Clone, Debug, Eq, Hash, PartialEq, Serialize, Deserialize)] #[derive(Clone, Debug, Eq, Hash, PartialEq, Serialize, Deserialize, Deref)]
pub struct Animation { pub struct Animation {
/// An identifier for this file. /// Metadata of the animation file.
pub file_id: String, #[deref]
#[serde(flatten)]
/// Unique identifier for this file, which is supposed to be the same over pub file: FileMeta,
/// time and for different bots. Can't be used to download or reuse the
/// file.
pub file_unique_id: String,
/// A video width as defined by a sender. /// A video width as defined by a sender.
pub width: u32, pub width: u32,
@ -36,14 +34,12 @@ pub struct Animation {
/// A MIME type of the file as defined by a sender. /// A MIME type of the file as defined by a sender.
#[serde(with = "crate::types::non_telegram_types::mime::opt_deser")] #[serde(with = "crate::types::non_telegram_types::mime::opt_deser")]
pub mime_type: Option<Mime>, pub mime_type: Option<Mime>,
/// File size in bytes.
#[serde(default = "crate::types::file::file_size_fallback")]
pub file_size: u32,
} }
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use crate::types::FileMeta;
use super::*; use super::*;
#[test] #[test]
@ -65,21 +61,25 @@ mod tests {
"mime_type":"video/gif", "mime_type":"video/gif",
"file_size":6500}"#; "file_size":6500}"#;
let expected = Animation { let expected = Animation {
file: FileMeta {
file_id: "id".to_string(), file_id: "id".to_string(),
file_unique_id: "".to_string(), file_unique_id: "".to_string(),
file_size: 6500,
},
width: 320, width: 320,
height: 320, height: 320,
duration: 59, duration: 59,
thumb: Some(PhotoSize { thumb: Some(PhotoSize {
file_id: "id".to_string(), file: FileMeta {
file_unique_id: "".to_string(), file_id: "id".to_owned(),
file_unique_id: "".to_owned(),
file_size: 3452,
},
width: 320, width: 320,
height: 320, height: 320,
file_size: 3452,
}), }),
file_name: Some("some".to_string()), file_name: Some("some".to_string()),
mime_type: Some("video/gif".parse().unwrap()), mime_type: Some("video/gif".parse().unwrap()),
file_size: 6500,
}; };
let actual = serde_json::from_str::<Animation>(json).unwrap(); let actual = serde_json::from_str::<Animation>(json).unwrap();
assert_eq!(actual, expected) assert_eq!(actual, expected)

View file

@ -1,22 +1,20 @@
use derive_more::Deref;
use mime::Mime; use mime::Mime;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use crate::types::PhotoSize; use crate::types::{FileMeta, PhotoSize};
/// This object represents an audio file to be treated as music by the Telegram /// This object represents an audio file to be treated as music by the Telegram
/// clients. /// clients.
/// ///
/// [The official docs](https://core.telegram.org/bots/api#audio). /// [The official docs](https://core.telegram.org/bots/api#audio).
#[serde_with_macros::skip_serializing_none] #[serde_with_macros::skip_serializing_none]
#[derive(Clone, Debug, Eq, Hash, PartialEq, Serialize, Deserialize)] #[derive(Clone, Debug, Eq, Hash, PartialEq, Serialize, Deserialize, Deref)]
pub struct Audio { pub struct Audio {
/// An identifier for this file. /// Metadata of the audio file.
pub file_id: String, #[deref]
#[serde(flatten)]
/// Unique identifier for this file, which is supposed to be the same over pub file: FileMeta,
/// time and for different bots. Can't be used to download or reuse the
/// file.
pub file_unique_id: String,
/// A duration of the audio in seconds as defined by a sender. /// A duration of the audio in seconds as defined by a sender.
pub duration: u32, pub duration: u32,
@ -34,16 +32,14 @@ pub struct Audio {
#[serde(with = "crate::types::non_telegram_types::mime::opt_deser")] #[serde(with = "crate::types::non_telegram_types::mime::opt_deser")]
pub mime_type: Option<Mime>, pub mime_type: Option<Mime>,
/// File size in bytes.
#[serde(default = "crate::types::file::file_size_fallback")]
pub file_size: u32,
/// A thumbnail of the album cover to which the music file belongs. /// A thumbnail of the album cover to which the music file belongs.
pub thumb: Option<PhotoSize>, pub thumb: Option<PhotoSize>,
} }
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use crate::types::FileMeta;
use super::*; use super::*;
#[test] #[test]
@ -65,19 +61,23 @@ mod tests {
} }
}"#; }"#;
let expected = Audio { let expected = Audio {
file: FileMeta {
file_id: "id".to_string(), file_id: "id".to_string(),
file_unique_id: "".to_string(), file_unique_id: "".to_string(),
file_size: 123_456,
},
duration: 60, duration: 60,
performer: Some("Performer".to_string()), performer: Some("Performer".to_string()),
title: Some("Title".to_string()), title: Some("Title".to_string()),
mime_type: Some("application/zip".parse().unwrap()), mime_type: Some("application/zip".parse().unwrap()),
file_size: 123_456,
thumb: Some(PhotoSize { thumb: Some(PhotoSize {
file_id: "id".to_string(), file: FileMeta {
file_unique_id: "".to_string(), file_id: "id".to_owned(),
file_unique_id: "".to_owned(),
file_size: 3452,
},
width: 320, width: 320,
height: 320, height: 320,
file_size: 3452,
}), }),
file_name: None, file_name: None,
}; };

View file

@ -1,7 +1,8 @@
use derive_more::Deref;
use mime::Mime; use mime::Mime;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use crate::types::PhotoSize; use crate::types::{FileMeta, PhotoSize};
/// This object represents a general file (as opposed to [photos], [voice /// This object represents a general file (as opposed to [photos], [voice
/// messages] and [audio files]). /// messages] and [audio files]).
@ -12,15 +13,12 @@ use crate::types::PhotoSize;
/// [voice messages]: https://core.telegram.org/bots/api#voice /// [voice messages]: https://core.telegram.org/bots/api#voice
/// [audio files]: https://core.telegram.org/bots/api#audio /// [audio files]: https://core.telegram.org/bots/api#audio
#[serde_with_macros::skip_serializing_none] #[serde_with_macros::skip_serializing_none]
#[derive(Clone, Debug, Eq, Hash, PartialEq, Serialize, Deserialize)] #[derive(Clone, Debug, Eq, Hash, PartialEq, Serialize, Deserialize, Deref)]
pub struct Document { pub struct Document {
/// An identifier for this file. /// Metadata of the document file.
pub file_id: String, #[deref]
#[serde(flatten)]
/// Unique identifier for this file, which is supposed to be the same over pub file: FileMeta,
/// time and for different bots. Can't be used to download or reuse the
/// file.
pub file_unique_id: String,
/// A document thumbnail as defined by a sender. /// A document thumbnail as defined by a sender.
pub thumb: Option<PhotoSize>, pub thumb: Option<PhotoSize>,
@ -31,8 +29,4 @@ pub struct Document {
/// A MIME type of the file as defined by a sender. /// A MIME type of the file as defined by a sender.
#[serde(default, with = "crate::types::non_telegram_types::mime::opt_deser")] #[serde(default, with = "crate::types::non_telegram_types::mime::opt_deser")]
pub mime_type: Option<Mime>, pub mime_type: Option<Mime>,
/// File size in bytes.
#[serde(default = "crate::types::file::file_size_fallback")]
pub file_size: u32,
} }

View file

@ -1,24 +1,21 @@
use chrono::{DateTime, Utc}; use chrono::{DateTime, Utc};
use derive_more::Deref;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use crate::types::FileMeta;
/// This object represents a file uploaded to Telegram Passport. /// This object represents a file uploaded to Telegram Passport.
/// ///
/// Currently all Telegram Passport files are in JPEG format when decrypted and /// Currently all Telegram Passport files are in JPEG format when decrypted and
/// don't exceed 10MB. /// don't exceed 10MB.
/// ///
/// [The official docs](https://core.telegram.org/bots/api#passportfile). /// [The official docs](https://core.telegram.org/bots/api#passportfile).
#[derive(Clone, Debug, Eq, Hash, PartialEq, Serialize, Deserialize)] #[derive(Clone, Debug, Eq, Hash, PartialEq, Serialize, Deserialize, Deref)]
pub struct PassportFile { pub struct PassportFile {
/// Identifier for this file. /// Metadata of the passport file.
pub file_id: String, #[deref]
#[serde(flatten)]
/// Unique identifier for this file, which is supposed to be the same over pub file: FileMeta,
/// time and for different bots. Can't be used to download or reuse the
/// file.
pub file_unique_id: String,
/// File size in bytes.
pub file_size: u32,
/// Time when the file was uploaded. /// Time when the file was uploaded.
#[serde(with = "crate::types::serde_date_from_unix_timestamp")] #[serde(with = "crate::types::serde_date_from_unix_timestamp")]

View file

@ -1,29 +1,25 @@
use derive_more::Deref;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use crate::types::FileMeta;
/// This object represents one size of a photo or a [file]/[sticker] thumbnail. /// This object represents one size of a photo or a [file]/[sticker] thumbnail.
/// ///
/// [file]: crate::types::Document /// [file]: crate::types::Document
/// [sticker]: crate::types::Sticker /// [sticker]: crate::types::Sticker
#[serde_with_macros::skip_serializing_none] #[serde_with_macros::skip_serializing_none]
#[derive(Clone, Debug, Eq, Hash, PartialEq, Serialize, Deserialize)] #[derive(Clone, Debug, Eq, Hash, PartialEq, Serialize, Deserialize, Deref)]
pub struct PhotoSize { pub struct PhotoSize {
/// Identifier for this file. /// Metadata of the photo file.
pub file_id: String, #[deref]
#[serde(flatten)]
/// Unique identifier for this file, which is supposed to be the same over pub file: FileMeta,
/// time and for different bots. Can't be used to download or reuse the
/// file.
pub file_unique_id: String,
/// Photo width. /// Photo width.
pub width: u32, pub width: u32,
/// Photo height. /// Photo height.
pub height: u32, pub height: u32,
/// File size in bytes.
#[serde(default = "crate::types::file::file_size_fallback")]
pub file_size: u32,
} }
#[cfg(test)] #[cfg(test)]
@ -35,11 +31,13 @@ mod tests {
let json = r#"{"file_id":"id","file_unique_id":"","width":320,"height":320, let json = r#"{"file_id":"id","file_unique_id":"","width":320,"height":320,
"file_size":3452}"#; "file_size":3452}"#;
let expected = PhotoSize { let expected = PhotoSize {
file_id: "id".to_string(), file: FileMeta {
file_unique_id: "".to_string(), file_id: "id".to_owned(),
file_unique_id: "".to_owned(),
file_size: 3452,
},
width: 320, width: 320,
height: 320, height: 320,
file_size: 3452,
}; };
let actual = serde_json::from_str::<PhotoSize>(json).unwrap(); let actual = serde_json::from_str::<PhotoSize>(json).unwrap();
assert_eq!(actual, expected); assert_eq!(actual, expected);

View file

@ -1,21 +1,19 @@
use derive_more::Deref;
use mime::Mime; use mime::Mime;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use crate::types::PhotoSize; use crate::types::{FileMeta, PhotoSize};
/// This object represents a video file. /// This object represents a video file.
/// ///
/// [The official docs](https://core.telegram.org/bots/api#video). /// [The official docs](https://core.telegram.org/bots/api#video).
#[serde_with_macros::skip_serializing_none] #[serde_with_macros::skip_serializing_none]
#[derive(Clone, Debug, Eq, Hash, PartialEq, Serialize, Deserialize)] #[derive(Clone, Debug, Eq, Hash, PartialEq, Serialize, Deserialize, Deref)]
pub struct Video { pub struct Video {
/// Identifier for this file. /// Metadata of the video file.
pub file_id: String, #[deref]
#[serde(flatten)]
/// Unique identifier for this file, which is supposed to be the same over pub file: FileMeta,
/// time and for different bots. Can't be used to download or reuse the
/// file.
pub file_unique_id: String,
/// Video width as defined by sender. /// Video width as defined by sender.
pub width: u32, pub width: u32,
@ -35,8 +33,4 @@ pub struct Video {
/// Mime type of a file as defined by sender. /// Mime type of a file as defined by sender.
#[serde(with = "crate::types::non_telegram_types::mime::opt_deser")] #[serde(with = "crate::types::non_telegram_types::mime::opt_deser")]
pub mime_type: Option<Mime>, pub mime_type: Option<Mime>,
/// File size in bytes.
#[serde(default = "crate::types::file::file_size_fallback")]
pub file_size: u32,
} }

View file

@ -1,6 +1,7 @@
use derive_more::Deref;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use crate::types::PhotoSize; use crate::types::{FileMeta, PhotoSize};
/// This object represents a [video message] (available in Telegram apps as of /// This object represents a [video message] (available in Telegram apps as of
/// [v.4.0]). /// [v.4.0]).
@ -10,15 +11,12 @@ use crate::types::PhotoSize;
/// [video message]: https://telegram.org/blog/video-messages-and-telescope /// [video message]: https://telegram.org/blog/video-messages-and-telescope
/// [v4.0]: https://telegram.org/blog/video-messages-and-telescope /// [v4.0]: https://telegram.org/blog/video-messages-and-telescope
#[serde_with_macros::skip_serializing_none] #[serde_with_macros::skip_serializing_none]
#[derive(Clone, Debug, Eq, Hash, PartialEq, Serialize, Deserialize)] #[derive(Clone, Debug, Eq, Hash, PartialEq, Serialize, Deserialize, Deref)]
pub struct VideoNote { pub struct VideoNote {
/// Identifier for this file. /// Metadata of the video note file.
pub file_id: String, #[deref]
#[serde(flatten)]
/// Unique identifier for this file, which is supposed to be the same over pub file: FileMeta,
/// time and for different bots. Can't be used to download or reuse the
/// file.
pub file_unique_id: String,
/// Video width and height (diameter of the video message) as defined by /// Video width and height (diameter of the video message) as defined by
/// sender. /// sender.
@ -29,8 +27,4 @@ pub struct VideoNote {
/// Video thumbnail. /// Video thumbnail.
pub thumb: Option<PhotoSize>, pub thumb: Option<PhotoSize>,
/// File size in bytes.
#[serde(default = "crate::types::file::file_size_fallback")]
pub file_size: u32,
} }

View file

@ -1,19 +1,19 @@
use derive_more::Deref;
use mime::Mime; use mime::Mime;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use crate::types::FileMeta;
/// This object represents a voice note. /// This object represents a voice note.
/// ///
/// [The official docs](https://core.telegram.org/bots/api#voice). /// [The official docs](https://core.telegram.org/bots/api#voice).
#[serde_with_macros::skip_serializing_none] #[serde_with_macros::skip_serializing_none]
#[derive(Clone, Debug, Eq, Hash, PartialEq, Serialize, Deserialize)] #[derive(Clone, Debug, Eq, Hash, PartialEq, Serialize, Deserialize, Deref)]
pub struct Voice { pub struct Voice {
/// Identifier for this file. /// Metadata of the voice file.
pub file_id: String, #[deref]
#[serde(flatten)]
/// Unique identifier for this file, which is supposed to be the same over pub file: FileMeta,
/// time and for different bots. Can't be used to download or reuse the
/// file.
pub file_unique_id: String,
/// Duration of the audio in seconds as defined by sender. /// Duration of the audio in seconds as defined by sender.
pub duration: u32, pub duration: u32,
@ -21,8 +21,4 @@ pub struct Voice {
/// MIME type of the file as defined by sender. /// MIME type of the file as defined by sender.
#[serde(with = "crate::types::non_telegram_types::mime::opt_deser")] #[serde(with = "crate::types::non_telegram_types::mime::opt_deser")]
pub mime_type: Option<Mime>, pub mime_type: Option<Mime>,
/// File size in bytes.
#[serde(default = "crate::types::file::file_size_fallback")]
pub file_size: u32,
} }