rs_es/operations/
common.rs

1/*
2 * Copyright 2015-2016 Ben Ashford
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *     http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17//! Features common to all operations
18
19use std::fmt;
20
21use serde::ser::{Serialize, Serializer};
22
23use crate::util::StrJoin;
24
25/// A newtype for the value of a URI option, this is to allow conversion traits
26/// to be implemented for it
27#[derive(Debug)]
28pub struct OptionVal(pub String);
29
30/// Conversion from `&str` to `OptionVal`
31impl<'a> From<&'a str> for OptionVal {
32    fn from(from: &'a str) -> OptionVal {
33        OptionVal(from.to_owned())
34    }
35}
36
37// Basic types have conversions to `OptionVal`
38from_exp!(String, OptionVal, from, OptionVal(from));
39from_exp!(i32, OptionVal, from, OptionVal(from.to_string()));
40from_exp!(i64, OptionVal, from, OptionVal(from.to_string()));
41from_exp!(u32, OptionVal, from, OptionVal(from.to_string()));
42from_exp!(u64, OptionVal, from, OptionVal(from.to_string()));
43from_exp!(bool, OptionVal, from, OptionVal(from.to_string()));
44
45/// Every ES operation has a set of options
46#[derive(Default, Debug)]
47pub struct Options<'a>(pub Vec<(&'a str, OptionVal)>);
48
49impl<'a> Options<'a> {
50    pub fn new() -> Options<'a> {
51        Options(Vec::new())
52    }
53
54    pub fn is_empty(&self) -> bool {
55        self.0.is_empty()
56    }
57
58    /// Add a value
59    ///
60    /// ```
61    /// use rs_es::operations::common::Options;
62    /// let mut options = Options::new();
63    /// options.push("a", 1);
64    /// options.push("b", "2");
65    /// ```
66    pub fn push<O: Into<OptionVal>>(&mut self, key: &'a str, val: O) {
67        self.0.push((key, val.into()));
68    }
69}
70
71impl<'a> fmt::Display for Options<'a> {
72    fn fmt(&self, formatter: &mut fmt::Formatter) -> Result<(), fmt::Error> {
73        if !self.is_empty() {
74            formatter.write_str("?")?;
75            formatter.write_str(
76                &self
77                    .0
78                    .iter()
79                    .map(|&(ref k, ref v)| format!("{}={}", k, v.0))
80                    .join("&"),
81            )?;
82        }
83        Ok(())
84    }
85}
86
87/// Adds a function to an operation to add specific query-string options to that
88/// operations builder interface.
89macro_rules! add_option {
90    ($n:ident, $e:expr) => (
91        pub fn $n<T: Into<OptionVal>>(&'a mut self, val: T) -> &'a mut Self {
92            self.options.push($e, val);
93            self
94        }
95    )
96}
97
98/// The [`version_type` field](https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-index_.html#index-versioning)
99#[derive(Debug)]
100pub enum VersionType {
101    Internal,
102    External,
103    ExternalGt,
104    ExternalGte,
105    Force,
106}
107
108impl Serialize for VersionType {
109    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
110    where
111        S: Serializer,
112    {
113        self.to_string().serialize(serializer)
114    }
115}
116
117impl ToString for VersionType {
118    fn to_string(&self) -> String {
119        match *self {
120            VersionType::Internal => "internal",
121            VersionType::External => "external",
122            VersionType::ExternalGt => "external_gt",
123            VersionType::ExternalGte => "external_gte",
124            VersionType::Force => "force",
125        }
126        .to_owned()
127    }
128}
129
130from_exp!(VersionType, OptionVal, from, OptionVal(from.to_string()));
131
132/// The consistency query parameter
133#[derive(Debug)]
134pub enum Consistency {
135    One,
136    Quorum,
137    All,
138}
139
140impl From<Consistency> for OptionVal {
141    fn from(from: Consistency) -> OptionVal {
142        OptionVal(
143            match from {
144                Consistency::One => "one",
145                Consistency::Quorum => "quorum",
146                Consistency::All => "all",
147            }
148            .to_owned(),
149        )
150    }
151}
152
153/// Values for `default_operator` query parameters
154#[derive(Debug)]
155pub enum DefaultOperator {
156    And,
157    Or,
158}
159
160impl From<DefaultOperator> for OptionVal {
161    fn from(from: DefaultOperator) -> OptionVal {
162        OptionVal(
163            match from {
164                DefaultOperator::And => "and",
165                DefaultOperator::Or => "or",
166            }
167            .to_owned(),
168        )
169    }
170}