Skip to main content

couchbase_core/searchx/
index.rs

1/*
2 *
3 *  * Copyright (c) 2025 Couchbase, Inc.
4 *  *
5 *  * Licensed under the Apache License, Version 2.0 (the "License");
6 *  * you may not use this file except in compliance with the License.
7 *  * You may obtain a copy of the License at
8 *  *
9 *  *    http://www.apache.org/licenses/LICENSE-2.0
10 *  *
11 *  * Unless required by applicable law or agreed to in writing, software
12 *  * distributed under the License is distributed on an "AS IS" BASIS,
13 *  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 *  * See the License for the specific language governing permissions and
15 *  * limitations under the License.
16 *
17 */
18
19use serde::{Deserialize, Serialize};
20use serde_json::Value;
21use std::collections::HashMap;
22
23// The serialize/deserialize implementation here is not used for serializing/deserializing
24// in requests.
25// This implementation is intended as a convenience for upserting indexes.
26#[derive(Debug, Serialize, Deserialize)]
27#[serde(rename_all = "camelCase")]
28#[non_exhaustive]
29pub struct Index {
30    pub name: String,
31    #[serde(rename = "type")]
32    pub index_type: String,
33
34    #[serde(skip_serializing_if = "Option::is_none")]
35    pub params: Option<HashMap<String, Value>>,
36    #[serde(skip_serializing_if = "Option::is_none")]
37    pub plan_params: Option<HashMap<String, Value>>,
38    #[serde(skip_serializing_if = "Option::is_none")]
39    pub prev_index_uuid: Option<String>,
40    #[serde(skip_serializing_if = "Option::is_none")]
41    pub source_name: Option<String>,
42    #[serde(skip_serializing_if = "Option::is_none")]
43    pub source_params: Option<HashMap<String, Value>>,
44    #[serde(skip_serializing_if = "Option::is_none")]
45    pub source_type: Option<String>,
46    #[serde(skip_serializing_if = "Option::is_none")]
47    pub source_uuid: Option<String>,
48    #[serde(skip_serializing_if = "Option::is_none")]
49    pub uuid: Option<String>,
50}
51
52impl Index {
53    pub fn new(name: impl Into<String>, index_type: impl Into<String>) -> Self {
54        Self {
55            name: name.into(),
56            index_type: index_type.into(),
57            params: None,
58            plan_params: None,
59            prev_index_uuid: None,
60            source_name: None,
61            source_params: None,
62            source_type: None,
63            source_uuid: None,
64            uuid: None,
65        }
66    }
67
68    pub fn params(mut self, params: impl Into<Option<HashMap<String, Value>>>) -> Self {
69        self.params = params.into();
70        self
71    }
72
73    pub fn plan_params(mut self, plan_params: impl Into<Option<HashMap<String, Value>>>) -> Self {
74        self.plan_params = plan_params.into();
75        self
76    }
77
78    pub fn prev_index_uuid(mut self, prev_index_uuid: impl Into<Option<String>>) -> Self {
79        self.prev_index_uuid = prev_index_uuid.into();
80        self
81    }
82
83    pub fn source_name(mut self, source_name: impl Into<Option<String>>) -> Self {
84        self.source_name = source_name.into();
85        self
86    }
87
88    pub fn source_params(
89        mut self,
90        source_params: impl Into<Option<HashMap<String, Value>>>,
91    ) -> Self {
92        self.source_params = source_params.into();
93        self
94    }
95
96    pub fn source_type(mut self, source_type: impl Into<Option<String>>) -> Self {
97        self.source_type = source_type.into();
98        self
99    }
100
101    pub fn source_uuid(mut self, source_uuid: impl Into<Option<String>>) -> Self {
102        self.source_uuid = source_uuid.into();
103        self
104    }
105
106    pub fn uuid(mut self, uuid: impl Into<Option<String>>) -> Self {
107        self.uuid = uuid.into();
108        self
109    }
110}