Remove file_ prefix from File fields

This also removes `Deref<Target = FileMeta>` impls from
`Animation`, `Audio`, `Document`, `PhotoSize` (?), `Video`, `VideoNote`
and `Voice` -- they are not really necessary with the `file_` prefix
removed.
This commit is contained in:
Maybe Waffle 2022-10-01 17:28:54 +04:00
parent 7a31bcea91
commit 24db0e29d3
11 changed files with 58 additions and 67 deletions

View file

@ -34,17 +34,17 @@ pub trait Download<'w>
/// use teloxide_core::{
/// net::Download,
/// requests::{Request, Requester},
/// types::File as TgFile,
/// types::File,
/// Bot,
/// };
/// use tokio::fs::File;
/// use tokio::fs;
///
/// # async fn run() -> Result<(), Box<dyn std::error::Error>> {
/// let bot = Bot::new("TOKEN");
///
/// let TgFile { file_path, .. } = bot.get_file("*file_id*").await?;
/// let mut file = File::create("/tmp/test.png").await?;
/// bot.download_file(&file_path, &mut file).await?;
/// let file = bot.get_file("*file_id*").await?;
/// let mut dst = fs::File::create("/tmp/test.png").await?;
/// bot.download_file(&file.path, &mut dst).await?;
/// # Ok(()) }
/// ```
///

View file

@ -1,4 +1,3 @@
use derive_more::Deref;
use mime::Mime;
use serde::{Deserialize, Serialize};
@ -9,10 +8,9 @@ use crate::types::{FileMeta, PhotoSize};
///
/// [The official docs](https://core.telegram.org/bots/api#animation).
#[serde_with_macros::skip_serializing_none]
#[derive(Clone, Debug, Eq, Hash, PartialEq, Serialize, Deserialize, Deref)]
#[derive(Clone, Debug, Eq, Hash, PartialEq, Serialize, Deserialize)]
pub struct Animation {
/// Metadata of the animation file.
#[deref]
#[serde(flatten)]
pub file: FileMeta,
@ -62,18 +60,18 @@ mod tests {
"file_size":6500}"#;
let expected = Animation {
file: FileMeta {
file_id: "id".to_string(),
file_unique_id: "".to_string(),
file_size: 6500,
id: "id".to_string(),
unique_id: "".to_string(),
size: 6500,
},
width: 320,
height: 320,
duration: 59,
thumb: Some(PhotoSize {
file: FileMeta {
file_id: "id".to_owned(),
file_unique_id: "".to_owned(),
file_size: 3452,
id: "id".to_owned(),
unique_id: "".to_owned(),
size: 3452,
},
width: 320,
height: 320,

View file

@ -1,4 +1,3 @@
use derive_more::Deref;
use mime::Mime;
use serde::{Deserialize, Serialize};
@ -9,10 +8,9 @@ use crate::types::{FileMeta, PhotoSize};
///
/// [The official docs](https://core.telegram.org/bots/api#audio).
#[serde_with_macros::skip_serializing_none]
#[derive(Clone, Debug, Eq, Hash, PartialEq, Serialize, Deserialize, Deref)]
#[derive(Clone, Debug, Eq, Hash, PartialEq, Serialize, Deserialize)]
pub struct Audio {
/// Metadata of the audio file.
#[deref]
#[serde(flatten)]
pub file: FileMeta,
@ -62,9 +60,9 @@ mod tests {
}"#;
let expected = Audio {
file: FileMeta {
file_id: "id".to_string(),
file_unique_id: "".to_string(),
file_size: 123_456,
id: "id".to_string(),
unique_id: "".to_string(),
size: 123_456,
},
duration: 60,
performer: Some("Performer".to_string()),
@ -72,9 +70,9 @@ mod tests {
mime_type: Some("application/zip".parse().unwrap()),
thumb: Some(PhotoSize {
file: FileMeta {
file_id: "id".to_owned(),
file_unique_id: "".to_owned(),
file_size: 3452,
id: "id".to_owned(),
unique_id: "".to_owned(),
size: 3452,
},
width: 320,
height: 320,

View file

@ -1,4 +1,3 @@
use derive_more::Deref;
use mime::Mime;
use serde::{Deserialize, Serialize};
@ -13,10 +12,9 @@ use crate::types::{FileMeta, 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, Deref)]
#[derive(Clone, Debug, Eq, Hash, PartialEq, Serialize, Deserialize)]
pub struct Document {
/// Metadata of the document file.
#[deref]
#[serde(flatten)]
pub file: FileMeta,

View file

@ -25,26 +25,30 @@ pub struct File {
/// File path. Use [`Bot::download_file(file_path, dst)`] to get the file.
///
/// [`Bot::download_file(file_path, dst)`]: crate::net::Download::download_file
pub file_path: String,
#[serde(rename = "file_path")]
pub path: String,
}
/// Metadata of the [`File`].
/// Metadata of a [`File`].
#[derive(Clone, Debug, Eq, Hash, PartialEq, Serialize, Deserialize)]
pub struct FileMeta {
/// Identifier for this file.
pub file_id: String,
#[serde(rename = "file_id")]
pub 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,
#[serde(rename = "file_unique_id")]
pub unique_id: String,
/// File size in bytes.
// This should never be necessary in practice,
#[serde(rename = "file_size")]
// This fallback should never be necessary in practice,
// but just in case something goes wrong with the TBA server
// (see the test below)
#[serde(default = "file_size_fallback")]
pub file_size: u32,
pub size: u32,
}
pub(crate) const fn file_size_fallback() -> u32 {
@ -58,12 +62,12 @@ pub(crate) const fn file_size_fallback() -> u32 {
/// ```rust
/// use teloxide_core::types::File;
/// #
/// # let get_file = || File { meta: teloxide_core::types::FileMeta { file_id: String::new(), file_unique_id: String::new(), file_size: 0 }, file_path: String::new() };
/// # let get_file = || File { meta: teloxide_core::types::FileMeta { id: String::new(), unique_id: String::new(), size: 0 }, path: String::new() };
/// let file: File = get_file();
///
/// let file_id: &str = &file.file_id;
/// let file_unique_id: &str = &file.file_unique_id;
/// let file_size: u32 = file.file_size;
/// let file_id: &str = &file.id;
/// let file_unique_id: &str = &file.unique_id;
/// let file_size: u32 = file.size;
/// #
/// # let _ = (file_id, file_unique_id, file_size);
/// ```
@ -92,11 +96,11 @@ mod tests {
file,
File {
meta: FileMeta {
file_id: "FILE_ID".to_owned(),
file_unique_id: "FILE_UNIQUE_ID".to_owned(),
file_size: u32::MAX,
id: "FILE_ID".to_owned(),
unique_id: "FILE_UNIQUE_ID".to_owned(),
size: u32::MAX,
},
file_path: "FILE_PATH".to_owned(),
path: "FILE_PATH".to_owned(),
}
);
}
@ -111,9 +115,9 @@ mod tests {
assert_eq!(
file,
FileMeta {
file_id: "FILE_ID".to_owned(),
file_unique_id: "FILE_UNIQUE_ID".to_owned(),
file_size: 42,
id: "FILE_ID".to_owned(),
unique_id: "FILE_UNIQUE_ID".to_owned(),
size: 42,
}
);
}
@ -127,11 +131,11 @@ mod tests {
file,
File {
meta: FileMeta {
file_id: "FILE_ID".to_owned(),
file_unique_id: "FILE_UNIQUE_ID".to_owned(),
file_size: 42,
id: "FILE_ID".to_owned(),
unique_id: "FILE_UNIQUE_ID".to_owned(),
size: 42,
},
file_path: "FILE_PATH".to_owned(),
path: "FILE_PATH".to_owned(),
}
);
}

View file

@ -19,5 +19,6 @@ pub struct PassportFile {
/// Time when the file was uploaded.
#[serde(with = "crate::types::serde_date_from_unix_timestamp")]
pub file_date: DateTime<Utc>,
#[serde(rename = "file_date")]
pub date: DateTime<Utc>,
}

View file

@ -1,4 +1,3 @@
use derive_more::Deref;
use serde::{Deserialize, Serialize};
use crate::types::FileMeta;
@ -8,10 +7,9 @@ use crate::types::FileMeta;
/// [file]: crate::types::Document
/// [sticker]: crate::types::Sticker
#[serde_with_macros::skip_serializing_none]
#[derive(Clone, Debug, Eq, Hash, PartialEq, Serialize, Deserialize, Deref)]
#[derive(Clone, Debug, Eq, Hash, PartialEq, Serialize, Deserialize)]
pub struct PhotoSize {
/// Metadata of the photo file.
#[deref]
#[serde(flatten)]
pub file: FileMeta,
@ -32,9 +30,9 @@ mod tests {
"file_size":3452}"#;
let expected = PhotoSize {
file: FileMeta {
file_id: "id".to_owned(),
file_unique_id: "".to_owned(),
file_size: 3452,
id: "id".to_owned(),
unique_id: "".to_owned(),
size: 3452,
},
width: 320,
height: 320,

View file

@ -364,8 +364,8 @@ mod tests {
assert_eq!(sticker.mask_position().unwrap().point, MaskPoint::Forehead);
assert_eq!(sticker.is_animated(), false);
assert_eq!(sticker.is_video(), false);
assert_eq!(sticker.thumb.clone().unwrap().file_size, 11028);
assert_eq!(sticker.file.file_size, 18290);
assert_eq!(sticker.thumb.clone().unwrap().file.size, 11028);
assert_eq!(sticker.file.size, 18290);
assert_eq!(sticker.width, 512);
assert_eq!(sticker.height, 512);
@ -404,8 +404,8 @@ mod tests {
assert_eq!(sticker.premium_animation(), None);
assert_eq!(sticker.is_animated(), false);
assert_eq!(sticker.is_video(), false);
assert_eq!(sticker.thumb.clone().unwrap().file_size, 4558);
assert_eq!(sticker.file.file_size, 25734);
assert_eq!(sticker.thumb.clone().unwrap().file.size, 4558);
assert_eq!(sticker.file.size, 25734);
assert_eq!(sticker.width, 463);
assert_eq!(sticker.height, 512);
assert_eq!(sticker.set_name.as_deref(), Some("menhera2"));

View file

@ -1,4 +1,3 @@
use derive_more::Deref;
use mime::Mime;
use serde::{Deserialize, Serialize};
@ -8,10 +7,9 @@ use crate::types::{FileMeta, PhotoSize};
///
/// [The official docs](https://core.telegram.org/bots/api#video).
#[serde_with_macros::skip_serializing_none]
#[derive(Clone, Debug, Eq, Hash, PartialEq, Serialize, Deserialize, Deref)]
#[derive(Clone, Debug, Eq, Hash, PartialEq, Serialize, Deserialize)]
pub struct Video {
/// Metadata of the video file.
#[deref]
#[serde(flatten)]
pub file: FileMeta,

View file

@ -1,4 +1,3 @@
use derive_more::Deref;
use serde::{Deserialize, Serialize};
use crate::types::{FileMeta, PhotoSize};
@ -11,10 +10,9 @@ use crate::types::{FileMeta, 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, Deref)]
#[derive(Clone, Debug, Eq, Hash, PartialEq, Serialize, Deserialize)]
pub struct VideoNote {
/// Metadata of the video note file.
#[deref]
#[serde(flatten)]
pub file: FileMeta,

View file

@ -1,4 +1,3 @@
use derive_more::Deref;
use mime::Mime;
use serde::{Deserialize, Serialize};
@ -8,10 +7,9 @@ use crate::types::FileMeta;
///
/// [The official docs](https://core.telegram.org/bots/api#voice).
#[serde_with_macros::skip_serializing_none]
#[derive(Clone, Debug, Eq, Hash, PartialEq, Serialize, Deserialize, Deref)]
#[derive(Clone, Debug, Eq, Hash, PartialEq, Serialize, Deserialize)]
pub struct Voice {
/// Metadata of the voice file.
#[deref]
#[serde(flatten)]
pub file: FileMeta,