rust_tdlib/types/
document.rs

1use crate::errors::Result;
2use crate::types::*;
3use uuid::Uuid;
4
5/// Describes a document of any type
6#[derive(Debug, Clone, Default, Serialize, Deserialize)]
7pub struct Document {
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    /// Original name of the file; as defined by the sender
14
15    #[serde(default)]
16    file_name: String,
17    /// MIME type of the file; as defined by the sender
18
19    #[serde(default)]
20    mime_type: String,
21    /// Document minithumbnail; may be null
22    minithumbnail: Option<Minithumbnail>,
23    /// Document thumbnail in JPEG or PNG format (PNG will be used only for background patterns); as defined by the sender; may be null
24    thumbnail: Option<Thumbnail>,
25    /// File containing the document
26    document: File,
27}
28
29impl RObject for Document {
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 Document {
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() -> DocumentBuilder {
45        let mut inner = Document::default();
46        inner.extra = Some(Uuid::new_v4().to_string());
47
48        DocumentBuilder { inner }
49    }
50
51    pub fn file_name(&self) -> &String {
52        &self.file_name
53    }
54
55    pub fn mime_type(&self) -> &String {
56        &self.mime_type
57    }
58
59    pub fn minithumbnail(&self) -> &Option<Minithumbnail> {
60        &self.minithumbnail
61    }
62
63    pub fn thumbnail(&self) -> &Option<Thumbnail> {
64        &self.thumbnail
65    }
66
67    pub fn document(&self) -> &File {
68        &self.document
69    }
70}
71
72#[doc(hidden)]
73pub struct DocumentBuilder {
74    inner: Document,
75}
76
77#[deprecated]
78pub type RTDDocumentBuilder = DocumentBuilder;
79
80impl DocumentBuilder {
81    pub fn build(&self) -> Document {
82        self.inner.clone()
83    }
84
85    pub fn file_name<T: AsRef<str>>(&mut self, file_name: T) -> &mut Self {
86        self.inner.file_name = file_name.as_ref().to_string();
87        self
88    }
89
90    pub fn mime_type<T: AsRef<str>>(&mut self, mime_type: T) -> &mut Self {
91        self.inner.mime_type = mime_type.as_ref().to_string();
92        self
93    }
94
95    pub fn minithumbnail<T: AsRef<Minithumbnail>>(&mut self, minithumbnail: T) -> &mut Self {
96        self.inner.minithumbnail = Some(minithumbnail.as_ref().clone());
97        self
98    }
99
100    pub fn thumbnail<T: AsRef<Thumbnail>>(&mut self, thumbnail: T) -> &mut Self {
101        self.inner.thumbnail = Some(thumbnail.as_ref().clone());
102        self
103    }
104
105    pub fn document<T: AsRef<File>>(&mut self, document: T) -> &mut Self {
106        self.inner.document = document.as_ref().clone();
107        self
108    }
109}
110
111impl AsRef<Document> for Document {
112    fn as_ref(&self) -> &Document {
113        self
114    }
115}
116
117impl AsRef<Document> for DocumentBuilder {
118    fn as_ref(&self) -> &Document {
119        &self.inner
120    }
121}