rust_tdlib/types/
voice_note.rs

1use crate::errors::Result;
2use crate::types::*;
3use uuid::Uuid;
4
5/// 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
6#[derive(Debug, Clone, Default, Serialize, Deserialize)]
7pub struct VoiceNote {
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 voice note, in seconds; as defined by the sender
14
15    #[serde(default)]
16    duration: i32,
17    /// A waveform representation of the voice note in 5-bit format
18
19    #[serde(default)]
20    waveform: String,
21    /// MIME type of the file; as defined by the sender
22
23    #[serde(default)]
24    mime_type: String,
25    /// File containing the voice note
26    voice: File,
27}
28
29impl RObject for VoiceNote {
30    #[doc(hidden)]
31    fn extra(&self) -> Option<&str> {
32        self.extra.as_deref()
33    }
34    #[doc(hidden)]
35    fn client_id(&self) -> Option<i32> {
36        self.client_id
37    }
38}
39
40impl VoiceNote {
41    pub fn from_json<S: AsRef<str>>(json: S) -> Result<Self> {
42        Ok(serde_json::from_str(json.as_ref())?)
43    }
44    pub fn builder() -> VoiceNoteBuilder {
45        let mut inner = VoiceNote::default();
46        inner.extra = Some(Uuid::new_v4().to_string());
47
48        VoiceNoteBuilder { inner }
49    }
50
51    pub fn duration(&self) -> i32 {
52        self.duration
53    }
54
55    pub fn waveform(&self) -> &String {
56        &self.waveform
57    }
58
59    pub fn mime_type(&self) -> &String {
60        &self.mime_type
61    }
62
63    pub fn voice(&self) -> &File {
64        &self.voice
65    }
66}
67
68#[doc(hidden)]
69pub struct VoiceNoteBuilder {
70    inner: VoiceNote,
71}
72
73#[deprecated]
74pub type RTDVoiceNoteBuilder = VoiceNoteBuilder;
75
76impl VoiceNoteBuilder {
77    pub fn build(&self) -> VoiceNote {
78        self.inner.clone()
79    }
80
81    pub fn duration(&mut self, duration: i32) -> &mut Self {
82        self.inner.duration = duration;
83        self
84    }
85
86    pub fn waveform<T: AsRef<str>>(&mut self, waveform: T) -> &mut Self {
87        self.inner.waveform = waveform.as_ref().to_string();
88        self
89    }
90
91    pub fn mime_type<T: AsRef<str>>(&mut self, mime_type: T) -> &mut Self {
92        self.inner.mime_type = mime_type.as_ref().to_string();
93        self
94    }
95
96    pub fn voice<T: AsRef<File>>(&mut self, voice: T) -> &mut Self {
97        self.inner.voice = voice.as_ref().clone();
98        self
99    }
100}
101
102impl AsRef<VoiceNote> for VoiceNote {
103    fn as_ref(&self) -> &VoiceNote {
104        self
105    }
106}
107
108impl AsRef<VoiceNote> for VoiceNoteBuilder {
109    fn as_ref(&self) -> &VoiceNote {
110        &self.inner
111    }
112}