aditjind_crate/
params.rs

1/*
2 * Licensed to Elasticsearch B.V. under one or more contributor
3 * license agreements. See the NOTICE file distributed with
4 * this work for additional information regarding copyright
5 * ownership. Elasticsearch B.V. licenses this file to you under
6 * the Apache License, Version 2.0 (the "License"); you may
7 * not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
9 *
10 *	http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied.  See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
19
20/*
21 * SPDX-License-Identifier: Apache-2.0
22 *
23 * The OpenSearch Contributors require contributions made to
24 * this file be licensed under the Apache-2.0 license or a
25 * compatible open source license.
26 *
27 * Modifications Copyright OpenSearch Contributors. See
28 * GitHub history for details.
29 */
30
31//! API parameters
32
33use core::fmt;
34use serde::{de, de::Visitor, Deserializer, Serializer};
35
36// GENERATED-BEGIN:spec-params
37// Generated code - do not edit until the next GENERATED-END marker
38
39use serde::{Deserialize, Serialize};
40#[doc = "The unit in which to display byte values"]
41#[derive(Debug, PartialEq, Deserialize, Serialize, Clone, Copy)]
42pub enum Bytes {
43    #[serde(rename = "b")]
44    B,
45    #[serde(rename = "k")]
46    K,
47    #[serde(rename = "kb")]
48    Kb,
49    #[serde(rename = "m")]
50    M,
51    #[serde(rename = "mb")]
52    Mb,
53    #[serde(rename = "g")]
54    G,
55    #[serde(rename = "gb")]
56    Gb,
57    #[serde(rename = "t")]
58    T,
59    #[serde(rename = "tb")]
60    Tb,
61    #[serde(rename = "p")]
62    P,
63    #[serde(rename = "pb")]
64    Pb,
65}
66#[doc = "What to do when the delete by query hits version conflicts?"]
67#[derive(Debug, PartialEq, Deserialize, Serialize, Clone, Copy)]
68pub enum Conflicts {
69    #[serde(rename = "abort")]
70    Abort,
71    #[serde(rename = "proceed")]
72    Proceed,
73}
74#[doc = "The default operator for query string query (AND or OR)"]
75#[derive(Debug, PartialEq, Deserialize, Serialize, Clone, Copy)]
76pub enum DefaultOperator {
77    #[serde(rename = "AND")]
78    And,
79    #[serde(rename = "OR")]
80    Or,
81}
82#[doc = "Whether to expand wildcard expression to concrete indices that are open, closed or both."]
83#[derive(Debug, PartialEq, Deserialize, Serialize, Clone, Copy)]
84pub enum ExpandWildcards {
85    #[serde(rename = "open")]
86    Open,
87    #[serde(rename = "closed")]
88    Closed,
89    #[serde(rename = "hidden")]
90    Hidden,
91    #[serde(rename = "none")]
92    None,
93    #[serde(rename = "all")]
94    All,
95}
96#[doc = "Optional parameter to specify the high level file format"]
97#[doc = " \n# Optional, experimental\nThis requires the `experimental-apis` feature. Can have breaking changes in future\nversions or might even be removed entirely.\n        "]
98#[cfg(feature = "experimental-apis")]
99#[derive(Debug, PartialEq, Deserialize, Serialize, Clone, Copy)]
100pub enum Format {
101    #[serde(rename = "ndjson")]
102    Ndjson,
103    #[serde(rename = "xml")]
104    Xml,
105    #[serde(rename = "delimited")]
106    Delimited,
107    #[serde(rename = "semi_structured_text")]
108    SemiStructuredText,
109}
110#[doc = "Group tasks by nodes or parent/child relationships"]
111#[doc = " \n# Optional, experimental\nThis requires the `experimental-apis` feature. Can have breaking changes in future\nversions or might even be removed entirely.\n        "]
112#[cfg(feature = "experimental-apis")]
113#[derive(Debug, PartialEq, Deserialize, Serialize, Clone, Copy)]
114pub enum GroupBy {
115    #[serde(rename = "nodes")]
116    Nodes,
117    #[serde(rename = "parents")]
118    Parents,
119    #[serde(rename = "none")]
120    None,
121}
122#[doc = "A health status (\"green\", \"yellow\", or \"red\" to filter only indices matching the specified health status"]
123#[derive(Debug, PartialEq, Deserialize, Serialize, Clone, Copy)]
124pub enum Health {
125    #[serde(rename = "green")]
126    Green,
127    #[serde(rename = "yellow")]
128    Yellow,
129    #[serde(rename = "red")]
130    Red,
131}
132#[doc = "Specify the level of detail for returned information"]
133#[derive(Debug, PartialEq, Deserialize, Serialize, Clone, Copy)]
134pub enum Level {
135    #[serde(rename = "cluster")]
136    Cluster,
137    #[serde(rename = "indices")]
138    Indices,
139    #[serde(rename = "shards")]
140    Shards,
141}
142#[doc = "Explicit operation type. Defaults to `index` for requests with an explicit document ID, and to `create`for requests without an explicit document ID"]
143#[derive(Debug, PartialEq, Deserialize, Serialize, Clone, Copy)]
144pub enum OpType {
145    #[serde(rename = "index")]
146    Index,
147    #[serde(rename = "create")]
148    Create,
149}
150#[doc = "If `true` then refresh the affected shards to make this operation visible to search, if `wait_for` then wait for a refresh to make this operation visible to search, if `false` (the default) then do nothing with refreshes."]
151#[derive(Debug, PartialEq, Deserialize, Serialize, Clone, Copy)]
152pub enum Refresh {
153    #[serde(rename = "true")]
154    True,
155    #[serde(rename = "false")]
156    False,
157    #[serde(rename = "wait_for")]
158    WaitFor,
159}
160#[doc = "Search operation type"]
161#[derive(Debug, PartialEq, Deserialize, Serialize, Clone, Copy)]
162pub enum SearchType {
163    #[serde(rename = "query_then_fetch")]
164    QueryThenFetch,
165    #[serde(rename = "dfs_query_then_fetch")]
166    DfsQueryThenFetch,
167}
168#[doc = "The multiplier in which to display values"]
169#[derive(Debug, PartialEq, Deserialize, Serialize, Clone, Copy)]
170pub enum Size {
171    #[serde(rename = "")]
172    Unspecified,
173    #[serde(rename = "k")]
174    K,
175    #[serde(rename = "m")]
176    M,
177    #[serde(rename = "g")]
178    G,
179    #[serde(rename = "t")]
180    T,
181    #[serde(rename = "p")]
182    P,
183}
184#[doc = "Specify suggest mode"]
185#[derive(Debug, PartialEq, Deserialize, Serialize, Clone, Copy)]
186pub enum SuggestMode {
187    #[serde(rename = "missing")]
188    Missing,
189    #[serde(rename = "popular")]
190    Popular,
191    #[serde(rename = "always")]
192    Always,
193}
194#[doc = "The unit in which to display time values"]
195#[derive(Debug, PartialEq, Deserialize, Serialize, Clone, Copy)]
196pub enum Time {
197    #[serde(rename = "d")]
198    D,
199    #[serde(rename = "h")]
200    H,
201    #[serde(rename = "m")]
202    M,
203    #[serde(rename = "s")]
204    S,
205    #[serde(rename = "ms")]
206    Ms,
207    #[serde(rename = "micros")]
208    Micros,
209    #[serde(rename = "nanos")]
210    Nanos,
211}
212#[doc = "The type to sample (default: cpu)"]
213#[derive(Debug, PartialEq, Deserialize, Serialize, Clone, Copy)]
214pub enum Type {
215    #[serde(rename = "cpu")]
216    Cpu,
217    #[serde(rename = "wait")]
218    Wait,
219    #[serde(rename = "block")]
220    Block,
221}
222#[doc = "Specific version type"]
223#[derive(Debug, PartialEq, Deserialize, Serialize, Clone, Copy)]
224pub enum VersionType {
225    #[serde(rename = "internal")]
226    Internal,
227    #[serde(rename = "external")]
228    External,
229    #[serde(rename = "external_gte")]
230    ExternalGte,
231    #[serde(rename = "force")]
232    Force,
233}
234#[doc = "Wait until all currently queued events with the given priority are processed"]
235#[derive(Debug, PartialEq, Deserialize, Serialize, Clone, Copy)]
236pub enum WaitForEvents {
237    #[serde(rename = "immediate")]
238    Immediate,
239    #[serde(rename = "urgent")]
240    Urgent,
241    #[serde(rename = "high")]
242    High,
243    #[serde(rename = "normal")]
244    Normal,
245    #[serde(rename = "low")]
246    Low,
247    #[serde(rename = "languid")]
248    Languid,
249}
250#[doc = "Wait until cluster is in a specific state"]
251#[derive(Debug, PartialEq, Deserialize, Serialize, Clone, Copy)]
252pub enum WaitForStatus {
253    #[serde(rename = "green")]
254    Green,
255    #[serde(rename = "yellow")]
256    Yellow,
257    #[serde(rename = "red")]
258    Red,
259}
260// GENERATED-END
261
262/// Control how the total number of hits should be tracked.
263///
264/// When set to `Track` with a value `true`, the response will always track the number of hits that
265/// match the query accurately.
266///
267/// When set to `Count` with an integer value `n`, the response accurately tracks the total
268/// hit count that match the query up to `n` documents.
269#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
270#[serde(untagged)]
271pub enum TrackTotalHits {
272    /// Whether to accurately track the number of hits that match the query accurately
273    Track(bool),
274    /// Accurately track the number of hits up to the specified value
275    Count(i64),
276}
277
278impl From<bool> for TrackTotalHits {
279    fn from(b: bool) -> Self {
280        TrackTotalHits::Track(b)
281    }
282}
283
284impl From<i64> for TrackTotalHits {
285    fn from(i: i64) -> Self {
286        TrackTotalHits::Count(i)
287    }
288}
289
290/// Control how the `_source` field is returned with every hit.
291///
292/// By default operations return the contents of the `_source` field
293/// unless you have used the `stored_fields` parameter or if the `_source` field is disabled.
294#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
295#[serde(untagged)]
296pub enum SourceFilter {
297    /// Whether `_source` retrieval should be enabled (`true`) or disabled (`false`)
298    Enable(bool),
299
300    /// A wildcard pattern to control what parts of `_source` should be returned
301    Include(String),
302
303    /// A collection of wildcard patterns to control what parts of `_source` should be returned
304    Includes(Vec<String>),
305
306    /// A collection of wildcard patterns to control what parts of `_source` should
307    /// and should not be returned
308    IncludesExcludes {
309        includes: Vec<String>,
310        excludes: Vec<String>,
311    },
312}
313
314impl From<bool> for SourceFilter {
315    fn from(b: bool) -> Self {
316        SourceFilter::Enable(b)
317    }
318}
319
320impl From<String> for SourceFilter {
321    fn from(include: String) -> Self {
322        SourceFilter::Include(include)
323    }
324}
325
326impl<'a> From<&'a str> for SourceFilter {
327    fn from(include: &'a str) -> Self {
328        SourceFilter::Include(include.to_owned())
329    }
330}
331
332impl From<Vec<String>> for SourceFilter {
333    fn from(includes: Vec<String>) -> Self {
334        SourceFilter::Includes(includes)
335    }
336}
337
338impl<'a> From<Vec<&'a str>> for SourceFilter {
339    fn from(includes: Vec<&'a str>) -> Self {
340        SourceFilter::Includes(includes.iter().map(|s| (*s).to_string()).collect())
341    }
342}
343
344impl From<(Vec<String>, Vec<String>)> for SourceFilter {
345    fn from(includes_excludes: (Vec<String>, Vec<String>)) -> Self {
346        SourceFilter::IncludesExcludes {
347            includes: includes_excludes.0,
348            excludes: includes_excludes.1,
349        }
350    }
351}
352
353impl<'a> From<(Vec<&'a str>, Vec<&'a str>)> for SourceFilter {
354    fn from(includes_excludes: (Vec<&'a str>, Vec<&'a str>)) -> Self {
355        SourceFilter::IncludesExcludes {
356            includes: includes_excludes
357                .0
358                .iter()
359                .map(|s| (*s).to_string())
360                .collect(),
361            excludes: includes_excludes
362                .1
363                .iter()
364                .map(|s| (*s).to_string())
365                .collect(),
366        }
367    }
368}
369
370/// Control the number of slices a task should be divided into. Defaults to `Slices::Count(1)`,
371/// meaning the task is not sliced.
372///
373/// When set to `Auto`, a task is automatically divided into a reasonable number of slices
374///
375/// When set to `Count` with an integer value `n`, divides the task into that number of slices
376#[derive(Debug, Clone, PartialEq)]
377pub enum Slices {
378    /// Automatically divide the task into a reasonable number of slices
379    Auto,
380    /// Number of slices to divide a task into
381    Count(i32),
382}
383
384impl Serialize for Slices {
385    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
386    where
387        S: Serializer,
388    {
389        match *self {
390            Slices::Auto => serializer.serialize_str("auto"),
391            Slices::Count(i) => serializer.serialize_i32(i),
392        }
393    }
394}
395
396impl<'de> Deserialize<'de> for Slices {
397    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
398    where
399        D: Deserializer<'de>,
400    {
401        struct SlicesVisitor;
402
403        impl<'de> Visitor<'de> for SlicesVisitor {
404            type Value = Slices;
405
406            fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
407                write!(formatter, "expected integer or string")
408            }
409
410            fn visit_i64<E>(self, value: i64) -> Result<Self::Value, E>
411            where
412                E: de::Error,
413            {
414                if value <= i32::max_value() as i64 {
415                    Ok(Slices::Count(value as i32))
416                } else {
417                    Err(E::custom(format!("i32 out of range: {}", value)))
418                }
419            }
420
421            fn visit_u64<E>(self, value: u64) -> Result<Self::Value, E>
422            where
423                E: de::Error,
424            {
425                if value <= i32::max_value() as u64 {
426                    Ok(Slices::Count(value as i32))
427                } else {
428                    Err(E::custom(format!("i32 out of range: {}", value)))
429                }
430            }
431
432            fn visit_str<E>(self, value: &str) -> Result<Self::Value, E>
433            where
434                E: de::Error,
435            {
436                match value {
437                    "auto" => Ok(Slices::Auto),
438                    n => match n.parse::<i32>() {
439                        Ok(i) => Ok(Slices::Count(i)),
440                        Err(_) => Err(E::custom(format!(
441                            "expected 'auto' or i32 but received: {}",
442                            n
443                        ))),
444                    },
445                }
446            }
447
448            fn visit_string<E>(self, value: String) -> Result<Self::Value, E>
449            where
450                E: de::Error,
451            {
452                self.visit_str(&value)
453            }
454        }
455
456        deserializer.deserialize_any(SlicesVisitor)
457    }
458}
459
460impl Default for Slices {
461    fn default() -> Self {
462        Slices::Count(1)
463    }
464}
465
466impl From<i32> for Slices {
467    fn from(i: i32) -> Self {
468        Slices::Count(i)
469    }
470}
471
472#[cfg(test)]
473mod tests {
474    use crate::params::Slices;
475
476    #[test]
477    fn serialize_slices_auto() {
478        let json = serde_json::to_string(&Slices::Auto).unwrap();
479        assert_eq!("\"auto\"", &json);
480        let slices: Slices = serde_json::from_str(&json).unwrap();
481        assert_eq!(Slices::Auto, slices);
482    }
483
484    #[test]
485    fn serialize_slices_count() {
486        let json = serde_json::to_string(&Slices::Count(100)).unwrap();
487        assert_eq!("100", &json);
488        let slices: Slices = serde_json::from_str(&json).unwrap();
489        assert_eq!(Slices::Count(100), slices);
490    }
491}