rtdlib/types/
audio.rs

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