rtdlib/types/
voice_note.rs

1
2use crate::types::*;
3use crate::errors::*;
4use uuid::Uuid;
5
6
7
8
9/// Describes a voice note. The voice note must be encoded with the Opus codec, and stored inside an OGG container. Voice notes can have only a single audio channel
10#[derive(Debug, Clone, Default, Serialize, Deserialize)]
11pub struct VoiceNote {
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 voice note, in seconds; as defined by the sender
19  duration: i64,
20  /// A waveform representation of the voice note in 5-bit format
21  waveform: String,
22  /// MIME type of the file; as defined by the sender
23  mime_type: String,
24  /// File containing the voice note
25  voice: File,
26  
27}
28
29impl RObject for VoiceNote {
30  #[doc(hidden)] fn td_name(&self) -> &'static str { "voiceNote" }
31  #[doc(hidden)] fn extra(&self) -> Option<String> { self.extra.clone() }
32  fn to_json(&self) -> RTDResult<String> { Ok(serde_json::to_string(self)?) }
33}
34
35
36
37impl VoiceNote {
38  pub fn from_json<S: AsRef<str>>(json: S) -> RTDResult<Self> { Ok(serde_json::from_str(json.as_ref())?) }
39  pub fn builder() -> RTDVoiceNoteBuilder {
40    let mut inner = VoiceNote::default();
41    inner.td_name = "voiceNote".to_string();
42    inner.extra = Some(Uuid::new_v4().to_string());
43    RTDVoiceNoteBuilder { inner }
44  }
45
46  pub fn duration(&self) -> i64 { self.duration }
47
48  pub fn waveform(&self) -> &String { &self.waveform }
49
50  pub fn mime_type(&self) -> &String { &self.mime_type }
51
52  pub fn voice(&self) -> &File { &self.voice }
53
54}
55
56#[doc(hidden)]
57pub struct RTDVoiceNoteBuilder {
58  inner: VoiceNote
59}
60
61impl RTDVoiceNoteBuilder {
62  pub fn build(&self) -> VoiceNote { self.inner.clone() }
63
64   
65  pub fn duration(&mut self, duration: i64) -> &mut Self {
66    self.inner.duration = duration;
67    self
68  }
69
70   
71  pub fn waveform<T: AsRef<str>>(&mut self, waveform: T) -> &mut Self {
72    self.inner.waveform = waveform.as_ref().to_string();
73    self
74  }
75
76   
77  pub fn mime_type<T: AsRef<str>>(&mut self, mime_type: T) -> &mut Self {
78    self.inner.mime_type = mime_type.as_ref().to_string();
79    self
80  }
81
82   
83  pub fn voice<T: AsRef<File>>(&mut self, voice: T) -> &mut Self {
84    self.inner.voice = voice.as_ref().clone();
85    self
86  }
87
88}
89
90impl AsRef<VoiceNote> for VoiceNote {
91  fn as_ref(&self) -> &VoiceNote { self }
92}
93
94impl AsRef<VoiceNote> for RTDVoiceNoteBuilder {
95  fn as_ref(&self) -> &VoiceNote { &self.inner }
96}
97
98
99