elastic_responses/
index.rs

1/*!
2Response types for an [index document request](https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-index_.html).
3*/
4
5use common::Shards;
6use parsing::{HttpResponseHead, IsOk, MaybeOkResponse, ResponseBody, Unbuffered};
7use error::*;
8
9/** Response for an [index document request](https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-index_.html). */
10#[derive(Deserialize, Debug)]
11pub struct IndexResponse {
12    #[serde(rename = "_index")] index: String,
13    #[serde(rename = "_type")] ty: String,
14    #[serde(rename = "_id")] id: String,
15    #[serde(rename = "_version")] version: Option<u32>,
16    created: bool,
17    #[serde(rename = "_shards")] shards: Shards,
18}
19
20impl IndexResponse {
21    /** Shards metadata for the request. */
22    pub fn shards(&self) -> &Shards {
23        &self.shards
24    }
25
26    /** Whether or not a matching document was created. */
27    pub fn created(&self) -> bool {
28        self.created
29    }
30
31    /** The index for the document. */
32    pub fn index(&self) -> &str {
33        &self.index
34    }
35
36    /** The type of the document. */
37    pub fn ty(&self) -> &str {
38        &self.ty
39    }
40
41    /** The id of the document. */
42    pub fn id(&self) -> &str {
43        &self.id
44    }
45
46    /** The version of the document. */
47    pub fn version(&self) -> Option<u32> {
48        self.version.clone()
49    }
50}
51
52impl IsOk for IndexResponse {
53    fn is_ok<B: ResponseBody>(head: HttpResponseHead, body: Unbuffered<B>) -> Result<MaybeOkResponse<B>, ParseResponseError> {
54        match head.status() {
55            200...299 => Ok(MaybeOkResponse::ok(body)),
56            _ => Ok(MaybeOkResponse::err(body)),
57        }
58    }
59}