1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
use azure_sdk_core::errors::AzureError;
use azure_sdk_core::prelude::IfMatchCondition;
use http::HeaderMap;

#[derive(Default, Serialize, Deserialize, Debug, Clone, PartialEq)]
pub struct DocumentAttributes {
    pub id: String,
    #[serde(rename = "_rid")]
    pub rid: String,
    #[serde(rename = "_ts")]
    pub ts: u64,
    #[serde(rename = "_self")]
    pub _self: String,
    #[serde(rename = "_etag")]
    pub etag: String,
    #[serde(rename = "_attachments")]
    pub attachments: String,
}

impl DocumentAttributes {
    pub fn id(&self) -> &str {
        &self.id
    }

    pub fn rid(&self) -> &str {
        &self.rid
    }

    pub fn ts(&self) -> u64 {
        self.ts
    }

    pub fn _self(&self) -> &str {
        &self._self
    }

    pub fn etag(&self) -> &str {
        &self.etag
    }

    pub fn attachments(&self) -> &str {
        &self.attachments
    }

    pub fn set_id<T>(&mut self, value: T)
    where
        T: Into<String>,
    {
        self.id = value.into();
    }

    pub fn set_rid<T>(&mut self, value: T)
    where
        T: Into<String>,
    {
        self.rid = value.into();
    }

    pub fn set_ts(&mut self, value: u64) {
        self.ts = value;
    }

    pub fn set_self<T>(&mut self, value: T)
    where
        T: Into<String>,
    {
        self._self = value.into();
    }

    pub fn set_etag<T>(&mut self, value: T)
    where
        T: Into<String>,
    {
        self.etag = value.into();
    }

    pub fn set_attachments<T>(&mut self, value: T)
    where
        T: Into<String>,
    {
        self.attachments = value.into();
    }
}

impl std::convert::TryFrom<(&HeaderMap, &[u8])> for DocumentAttributes {
    type Error = AzureError;
    fn try_from(value: (&HeaderMap, &[u8])) -> Result<Self, Self::Error> {
        let body = value.1;
        Ok(serde_json::from_slice(body)?)
    }
}

impl<'a> std::convert::From<&'a DocumentAttributes> for IfMatchCondition<'a> {
    fn from(document_attributes: &'a DocumentAttributes) -> Self {
        IfMatchCondition::Match(&document_attributes.etag)
    }
}

#[cfg(test)]
mod tests {
    #[test]
    fn test_mutate() {
        use super::*;

        let mut a = DocumentAttributes {
            id: "id".to_owned(),
            rid: "rid".to_owned(),
            ts: 100,
            _self: "_self".to_owned(),
            etag: "etag".to_owned(),
            attachments: "attachments".to_owned(),
        };

        a.set_id("new_id");
        a.set_attachments("new_attachments".to_owned());
    }
}