azure_sdk_cosmos/
document_attributes.rs

1use azure_sdk_core::errors::AzureError;
2use azure_sdk_core::prelude::IfMatchCondition;
3use http::HeaderMap;
4
5#[derive(Default, Serialize, Deserialize, Debug, Clone, PartialEq)]
6pub struct DocumentAttributes {
7    #[serde(rename = "_rid")]
8    pub rid: String,
9    #[serde(rename = "_ts")]
10    pub ts: u64,
11    #[serde(rename = "_self")]
12    pub _self: String,
13    #[serde(rename = "_etag")]
14    pub etag: String,
15    #[serde(rename = "_attachments")]
16    pub attachments: String,
17}
18
19impl DocumentAttributes {
20    pub fn rid(&self) -> &str {
21        &self.rid
22    }
23
24    pub fn ts(&self) -> u64 {
25        self.ts
26    }
27
28    pub fn _self(&self) -> &str {
29        &self._self
30    }
31
32    pub fn etag(&self) -> &str {
33        &self.etag
34    }
35
36    pub fn attachments(&self) -> &str {
37        &self.attachments
38    }
39
40    pub fn set_rid<T>(&mut self, value: T)
41    where
42        T: Into<String>,
43    {
44        self.rid = value.into();
45    }
46
47    pub fn set_ts(&mut self, value: u64) {
48        self.ts = value;
49    }
50
51    pub fn set_self<T>(&mut self, value: T)
52    where
53        T: Into<String>,
54    {
55        self._self = value.into();
56    }
57
58    pub fn set_etag<T>(&mut self, value: T)
59    where
60        T: Into<String>,
61    {
62        self.etag = value.into();
63    }
64
65    pub fn set_attachments<T>(&mut self, value: T)
66    where
67        T: Into<String>,
68    {
69        self.attachments = value.into();
70    }
71}
72
73impl std::convert::TryFrom<(&HeaderMap, &[u8])> for DocumentAttributes {
74    type Error = AzureError;
75    fn try_from(value: (&HeaderMap, &[u8])) -> Result<Self, Self::Error> {
76        let body = value.1;
77        Ok(serde_json::from_slice(body)?)
78    }
79}
80
81impl<'a> std::convert::From<&'a DocumentAttributes> for IfMatchCondition<'a> {
82    fn from(document_attributes: &'a DocumentAttributes) -> Self {
83        IfMatchCondition::Match(&document_attributes.etag)
84    }
85}
86
87#[cfg(test)]
88mod tests {
89    #[test]
90    fn test_mutate() {
91        use super::*;
92
93        let mut a = DocumentAttributes {
94            rid: "rid".to_owned(),
95            ts: 100,
96            _self: "_self".to_owned(),
97            etag: "etag".to_owned(),
98            attachments: "attachments".to_owned(),
99        };
100
101        a.set_attachments("new_attachments".to_owned());
102    }
103}