From 0b21b4f19584eb08f45dc303bea4d8f17f60ac6e Mon Sep 17 00:00:00 2001 From: Bodo Junglas Date: Fri, 14 Dec 2018 11:24:05 +0100 Subject: [PATCH] [refactor] cleaner bulk results --- src/elasticsearch.rs | 41 ++++++++++++++++++++++++++++------------- 1 file changed, 28 insertions(+), 13 deletions(-) diff --git a/src/elasticsearch.rs b/src/elasticsearch.rs index 43b454d..416f2e6 100644 --- a/src/elasticsearch.rs +++ b/src/elasticsearch.rs @@ -250,22 +250,39 @@ pub struct Bucket { #[derive(Clone, Debug, Deserialize)] pub struct BulkActionResponse { #[serde(rename = "_id")] - id: String, - status: u16, - result: Option, + pub id: String, + pub status: u16, + pub result: Option, } #[derive(Clone, Debug, Deserialize)] pub struct BulkResponseItem { - index: Option, - update: Option, - delete: Option, + pub index: Option, + pub update: Option, + pub delete: Option, +} + +impl BulkResponseItem { + pub fn action_response(&self) -> Option<&BulkActionResponse> { + self + .index + .as_ref() + .or_else(|| self.update.as_ref()) + .or_else(|| self.delete.as_ref()) + } + + pub fn is_success(&self) -> bool { + self + .action_response() + .map(|response| response.status < 300) + .unwrap_or(false) + } } #[derive(Clone, Debug, Deserialize)] pub struct BulkResponse { - errors: bool, - items: Vec, + pub errors: bool, + pub items: Vec, } #[derive(Clone, Debug, Default)] @@ -279,11 +296,9 @@ impl BulkResult { let mut result: BulkResult = Default::default(); for item in response.items { - match item.index.or(item.update).or(item.delete) { - Some(ref action_response) if action_response.status < 300 => { - result.successes.push(action_response.id.to_owned()) - } - Some(action_response) => result.failures.push(action_response.id), + match item.action_response() { + Some(action_response) if action_response.status < 300 => result.successes.push(action_response.id.to_owned()), + Some(action_response) => result.failures.push(action_response.id.to_owned()), None => (), } }