rust_tdlib/types/
audio.rs

1use crate::errors::Result;
2use crate::types::*;
3use uuid::Uuid;
4
5/// Describes an audio file. Audio is usually in MP3 or M4A format
6#[derive(Debug, Clone, Default, Serialize, Deserialize)]
7pub struct Audio {
8    #[doc(hidden)]
9    #[serde(rename(serialize = "@extra", deserialize = "@extra"))]
10    extra: Option<String>,
11    #[serde(rename(serialize = "@client_id", deserialize = "@client_id"))]
12    client_id: Option<i32>,
13    /// Duration of the audio, in seconds; as defined by the sender
14
15    #[serde(default)]
16    duration: i32,
17    /// Title of the audio; as defined by the sender
18
19    #[serde(default)]
20    title: String,
21    /// Performer of the audio; as defined by the sender
22
23    #[serde(default)]
24    performer: String,
25    /// Original name of the file; as defined by the sender
26
27    #[serde(default)]
28    file_name: String,
29    /// The MIME type of the file; as defined by the sender
30
31    #[serde(default)]
32    mime_type: String,
33    /// The minithumbnail of the album cover; may be null
34    album_cover_minithumbnail: Option<Minithumbnail>,
35    /// The thumbnail of the album cover in JPEG format; as defined by the sender. The full size thumbnail is supposed to be extracted from the downloaded file; may be null
36    album_cover_thumbnail: Option<Thumbnail>,
37    /// File containing the audio
38    audio: File,
39}
40
41impl RObject for Audio {
42    #[doc(hidden)]
43    fn extra(&self) -> Option<&str> {
44        self.extra.as_deref()
45    }
46    #[doc(hidden)]
47    fn client_id(&self) -> Option<i32> {
48        self.client_id
49    }
50}
51
52impl Audio {
53    pub fn from_json<S: AsRef<str>>(json: S) -> Result<Self> {
54        Ok(serde_json::from_str(json.as_ref())?)
55    }
56    pub fn builder() -> AudioBuilder {
57        let mut inner = Audio::default();
58        inner.extra = Some(Uuid::new_v4().to_string());
59
60        AudioBuilder { inner }
61    }
62
63    pub fn duration(&self) -> i32 {
64        self.duration
65    }
66
67    pub fn title(&self) -> &String {
68        &self.title
69    }
70
71    pub fn performer(&self) -> &String {
72        &self.performer
73    }
74
75    pub fn file_name(&self) -> &String {
76        &self.file_name
77    }
78
79    pub fn mime_type(&self) -> &String {
80        &self.mime_type
81    }
82
83    pub fn album_cover_minithumbnail(&self) -> &Option<Minithumbnail> {
84        &self.album_cover_minithumbnail
85    }
86
87    pub fn album_cover_thumbnail(&self) -> &Option<Thumbnail> {
88        &self.album_cover_thumbnail
89    }
90
91    pub fn audio(&self) -> &File {
92        &self.audio
93    }
94}
95
96#[doc(hidden)]
97pub struct AudioBuilder {
98    inner: Audio,
99}
100
101#[deprecated]
102pub type RTDAudioBuilder = AudioBuilder;
103
104impl AudioBuilder {
105    pub fn build(&self) -> Audio {
106        self.inner.clone()
107    }
108
109    pub fn duration(&mut self, duration: i32) -> &mut Self {
110        self.inner.duration = duration;
111        self
112    }
113
114    pub fn title<T: AsRef<str>>(&mut self, title: T) -> &mut Self {
115        self.inner.title = title.as_ref().to_string();
116        self
117    }
118
119    pub fn performer<T: AsRef<str>>(&mut self, performer: T) -> &mut Self {
120        self.inner.performer = performer.as_ref().to_string();
121        self
122    }
123
124    pub fn file_name<T: AsRef<str>>(&mut self, file_name: T) -> &mut Self {
125        self.inner.file_name = file_name.as_ref().to_string();
126        self
127    }
128
129    pub fn mime_type<T: AsRef<str>>(&mut self, mime_type: T) -> &mut Self {
130        self.inner.mime_type = mime_type.as_ref().to_string();
131        self
132    }
133
134    pub fn album_cover_minithumbnail<T: AsRef<Minithumbnail>>(
135        &mut self,
136        album_cover_minithumbnail: T,
137    ) -> &mut Self {
138        self.inner.album_cover_minithumbnail = Some(album_cover_minithumbnail.as_ref().clone());
139        self
140    }
141
142    pub fn album_cover_thumbnail<T: AsRef<Thumbnail>>(
143        &mut self,
144        album_cover_thumbnail: T,
145    ) -> &mut Self {
146        self.inner.album_cover_thumbnail = Some(album_cover_thumbnail.as_ref().clone());
147        self
148    }
149
150    pub fn audio<T: AsRef<File>>(&mut self, audio: T) -> &mut Self {
151        self.inner.audio = audio.as_ref().clone();
152        self
153    }
154}
155
156impl AsRef<Audio> for Audio {
157    fn as_ref(&self) -> &Audio {
158        self
159    }
160}
161
162impl AsRef<Audio> for AudioBuilder {
163    fn as_ref(&self) -> &Audio {
164        &self.inner
165    }
166}