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
use serde::{Deserialize, Serialize};

pub mod sync {

    use super::{UpdatePayload, UpdateResponse};
    use crate::inner_client::sync::InnerClient;
    use crate::Error;
    use serde::Serialize;

    pub struct UpdateRequest<'a, T>
    where
        T: Serialize,
    {
        client: InnerClient,
        _id: String,
        payload: UpdatePayload<'a, T>,
    }

    impl<'a, T> UpdateRequest<'a, T>
    where
        T: Serialize,
    {
        pub(crate) fn new(
            client: &InnerClient,
            document: &'a T,
            id: impl Into<String>,
            rev: impl Into<String>,
        ) -> Self {
            UpdateRequest {
                client: client.duplicate(),
                _id: id.into(),
                payload: UpdatePayload {
                    _rev: rev.into(),
                    payload: document,
                },
            }
        }

        pub fn send(self) -> Result<UpdateResponse, Error> {
            let response = self
                .client
                .join(self._id)?
                .put()
                .json(&self.payload)
                .send()?
                .json()?;
            Ok(response)
        }
    }

}

pub mod r#async {

    use super::{UpdatePayload, UpdateResponse};
    use crate::inner_client::r#async::InnerClient;
    use crate::Error;
    use serde::Serialize;
    use tokio::prelude::{future::result, Future};

    /// A request to update an existing document.
    pub struct UpdateRequest<'a, T>
    where
        T: Serialize,
    {
        client: InnerClient,
        _id: String,
        payload: UpdatePayload<'a, T>,
    }

    impl<'a, T> UpdateRequest<'a, T>
    where
        T: Serialize,
    {
        pub(crate) fn new(
            client: &InnerClient,
            document: &'a T,
            id: impl Into<String>,
            rev: impl Into<String>,
        ) -> Self {
            UpdateRequest {
                client: client.duplicate(),
                _id: id.into(),
                payload: UpdatePayload {
                    _rev: rev.into(),
                    payload: document,
                },
            }
        }

        pub fn send(self) -> impl Future<Item = UpdateResponse, Error = Error> + 'a {
            result(self.client.join(&self._id).map_err(Error::from))
                .and_then(move |client| {
                    client.put().json(&self.payload).send().map_err(Error::from)
                })
                .and_then(|mut response| response.json().map_err(Error::from))
        }
    }

}

#[derive(Serialize)]
pub struct UpdatePayload<'a, T> {
    _rev: String,

    #[serde(flatten)]
    payload: &'a T,
}

#[derive(Deserialize)]
pub struct UpdateResponse {
    pub id: String,
    pub ok: bool,
    pub rev: String,
}