Skip to content
This repository has been archived by the owner on Nov 8, 2022. It is now read-only.

Commit

Permalink
Replace file_{[unique_]id,size} fields with FileMeta
Browse files Browse the repository at this point in the history
  • Loading branch information
WaffleLapkin committed Sep 29, 2022
1 parent 00165e6 commit cd10ef0
Show file tree
Hide file tree
Showing 9 changed files with 92 additions and 115 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,15 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### 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])
- `Request` now requires `Self: IntoFuture`
- There is no need for `AutoSend` anymore
- 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

- Methods for creating `InlineQuery` ([#246][pr244])
Expand Down
38 changes: 19 additions & 19 deletions src/types/animation.rs
Original file line number Diff line number Diff line change
@@ -1,22 +1,20 @@
use derive_more::Deref;
use mime::Mime;
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
/// without sound).
///
/// [The official docs](https://core.telegram.org/bots/api#animation).
#[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 {
/// An identifier for this file.
pub file_id: String,

/// Unique identifier for this file, which is supposed to be the same over
/// time and for different bots. Can't be used to download or reuse the
/// file.
pub file_unique_id: String,
/// Metadata of the animation file.
#[deref]
#[serde(flatten)]
pub file: FileMeta,

/// A video width as defined by a sender.
pub width: u32,
Expand All @@ -36,14 +34,12 @@ pub struct Animation {
/// A MIME type of the file as defined by a sender.
#[serde(with = "crate::types::non_telegram_types::mime::opt_deser")]
pub mime_type: Option<Mime>,

/// File size in bytes.
#[serde(default = "crate::types::file::file_size_fallback")]
pub file_size: u32,
}

#[cfg(test)]
mod tests {
use crate::types::FileMeta;

use super::*;

#[test]
Expand All @@ -65,21 +61,25 @@ mod tests {
"mime_type":"video/gif",
"file_size":6500}"#;
let expected = Animation {
file_id: "id".to_string(),
file_unique_id: "".to_string(),
file: FileMeta {
file_id: "id".to_string(),
file_unique_id: "".to_string(),
file_size: 6500,
},
width: 320,
height: 320,
duration: 59,
thumb: Some(PhotoSize {
file_id: "id".to_string(),
file_unique_id: "".to_string(),
file: FileMeta {
file_id: "id".to_owned(),
file_unique_id: "".to_owned(),
file_size: 3452,
},
width: 320,
height: 320,
file_size: 3452,
}),
file_name: Some("some".to_string()),
mime_type: Some("video/gif".parse().unwrap()),
file_size: 6500,
};
let actual = serde_json::from_str::<Animation>(json).unwrap();
assert_eq!(actual, expected)
Expand Down
38 changes: 19 additions & 19 deletions src/types/audio.rs
Original file line number Diff line number Diff line change
@@ -1,22 +1,20 @@
use derive_more::Deref;
use mime::Mime;
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
/// clients.
///
/// [The official docs](https://core.telegram.org/bots/api#audio).
#[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 {
/// An identifier for this file.
pub file_id: String,

/// Unique identifier for this file, which is supposed to be the same over
/// time and for different bots. Can't be used to download or reuse the
/// file.
pub file_unique_id: String,
/// Metadata of the audio file.
#[deref]
#[serde(flatten)]
pub file: FileMeta,

/// A duration of the audio in seconds as defined by a sender.
pub duration: u32,
Expand All @@ -34,16 +32,14 @@ pub struct Audio {
#[serde(with = "crate::types::non_telegram_types::mime::opt_deser")]
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.
pub thumb: Option<PhotoSize>,
}

#[cfg(test)]
mod tests {
use crate::types::FileMeta;

use super::*;

#[test]
Expand All @@ -65,19 +61,23 @@ mod tests {
}
}"#;
let expected = Audio {
file_id: "id".to_string(),
file_unique_id: "".to_string(),
file: FileMeta {
file_id: "id".to_string(),
file_unique_id: "".to_string(),
file_size: 123_456,
},
duration: 60,
performer: Some("Performer".to_string()),
title: Some("Title".to_string()),
mime_type: Some("application/zip".parse().unwrap()),
file_size: 123_456,
thumb: Some(PhotoSize {
file_id: "id".to_string(),
file_unique_id: "".to_string(),
file: FileMeta {
file_id: "id".to_owned(),
file_unique_id: "".to_owned(),
file_size: 3452,
},
width: 320,
height: 320,
file_size: 3452,
}),
file_name: None,
};
Expand Down
20 changes: 7 additions & 13 deletions src/types/document.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
use derive_more::Deref;
use mime::Mime;
use serde::{Deserialize, Serialize};

use crate::types::PhotoSize;
use crate::types::{FileMeta, PhotoSize};

/// This object represents a general file (as opposed to [photos], [voice
/// messages] and [audio files]).
Expand All @@ -12,15 +13,12 @@ use crate::types::PhotoSize;
/// [voice messages]: https://core.telegram.org/bots/api#voice
/// [audio files]: https://core.telegram.org/bots/api#audio
#[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 {
/// An identifier for this file.
pub file_id: String,

/// Unique identifier for this file, which is supposed to be the same over
/// time and for different bots. Can't be used to download or reuse the
/// file.
pub file_unique_id: String,
/// Metadata of the document file.
#[deref]
#[serde(flatten)]
pub file: FileMeta,

/// A document thumbnail as defined by a sender.
pub thumb: Option<PhotoSize>,
Expand All @@ -31,8 +29,4 @@ pub struct Document {
/// A MIME type of the file as defined by a sender.
#[serde(default, with = "crate::types::non_telegram_types::mime::opt_deser")]
pub mime_type: Option<Mime>,

/// File size in bytes.
#[serde(default = "crate::types::file::file_size_fallback")]
pub file_size: u32,
}
19 changes: 8 additions & 11 deletions src/types/passport_file.rs
Original file line number Diff line number Diff line change
@@ -1,24 +1,21 @@
use chrono::{DateTime, Utc};
use derive_more::Deref;
use serde::{Deserialize, Serialize};

use crate::types::FileMeta;

/// This object represents a file uploaded to Telegram Passport.
///
/// Currently all Telegram Passport files are in JPEG format when decrypted and
/// don't exceed 10MB.
///
/// [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 {
/// Identifier for this file.
pub file_id: String,

/// Unique identifier for this file, which is supposed to be the same over
/// 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,
/// Metadata of the passport file.
#[deref]
#[serde(flatten)]
pub file: FileMeta,

/// Time when the file was uploaded.
#[serde(with = "crate::types::serde_date_from_unix_timestamp")]
Expand Down
28 changes: 13 additions & 15 deletions src/types/photo_size.rs
Original file line number Diff line number Diff line change
@@ -1,29 +1,25 @@
use derive_more::Deref;
use serde::{Deserialize, Serialize};

use crate::types::FileMeta;

/// This object represents one size of a photo or a [file]/[sticker] thumbnail.
///
/// [file]: crate::types::Document
/// [sticker]: crate::types::Sticker
#[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 {
/// Identifier for this file.
pub file_id: String,

/// Unique identifier for this file, which is supposed to be the same over
/// time and for different bots. Can't be used to download or reuse the
/// file.
pub file_unique_id: String,
/// Metadata of the photo file.
#[deref]
#[serde(flatten)]
pub file: FileMeta,

/// Photo width.
pub width: u32,

/// Photo height.
pub height: u32,

/// File size in bytes.
#[serde(default = "crate::types::file::file_size_fallback")]
pub file_size: u32,
}

#[cfg(test)]
Expand All @@ -35,11 +31,13 @@ mod tests {
let json = r#"{"file_id":"id","file_unique_id":"","width":320,"height":320,
"file_size":3452}"#;
let expected = PhotoSize {
file_id: "id".to_string(),
file_unique_id: "".to_string(),
file: FileMeta {
file_id: "id".to_owned(),
file_unique_id: "".to_owned(),
file_size: 3452,
},
width: 320,
height: 320,
file_size: 3452,
};
let actual = serde_json::from_str::<PhotoSize>(json).unwrap();
assert_eq!(actual, expected);
Expand Down
20 changes: 7 additions & 13 deletions src/types/video.rs
Original file line number Diff line number Diff line change
@@ -1,21 +1,19 @@
use derive_more::Deref;
use mime::Mime;
use serde::{Deserialize, Serialize};

use crate::types::PhotoSize;
use crate::types::{FileMeta, PhotoSize};

/// This object represents a video file.
///
/// [The official docs](https://core.telegram.org/bots/api#video).
#[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 {
/// Identifier for this file.
pub file_id: String,

/// Unique identifier for this file, which is supposed to be the same over
/// time and for different bots. Can't be used to download or reuse the
/// file.
pub file_unique_id: String,
/// Metadata of the video file.
#[deref]
#[serde(flatten)]
pub file: FileMeta,

/// Video width as defined by sender.
pub width: u32,
Expand All @@ -35,8 +33,4 @@ pub struct Video {
/// Mime type of a file as defined by sender.
#[serde(with = "crate::types::non_telegram_types::mime::opt_deser")]
pub mime_type: Option<Mime>,

/// File size in bytes.
#[serde(default = "crate::types::file::file_size_fallback")]
pub file_size: u32,
}
20 changes: 7 additions & 13 deletions src/types/video_note.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use derive_more::Deref;
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
/// [v.4.0]).
Expand All @@ -10,15 +11,12 @@ use crate::types::PhotoSize;
/// [video message]: https://telegram.org/blog/video-messages-and-telescope
/// [v4.0]: https://telegram.org/blog/video-messages-and-telescope
#[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 {
/// Identifier for this file.
pub file_id: String,

/// Unique identifier for this file, which is supposed to be the same over
/// time and for different bots. Can't be used to download or reuse the
/// file.
pub file_unique_id: String,
/// Metadata of the video note file.
#[deref]
#[serde(flatten)]
pub file: FileMeta,

/// Video width and height (diameter of the video message) as defined by
/// sender.
Expand All @@ -29,8 +27,4 @@ pub struct VideoNote {

/// Video thumbnail.
pub thumb: Option<PhotoSize>,

/// File size in bytes.
#[serde(default = "crate::types::file::file_size_fallback")]
pub file_size: u32,
}
Loading

0 comments on commit cd10ef0

Please sign in to comment.