Skip to main content

qdrant_client/builders/
upsert_points_builder.rs

1use crate::qdrant::*;
2
3#[must_use]
4#[derive(Clone)]
5pub struct UpsertPointsBuilder {
6    /// name of the collection
7    pub(crate) collection_name: Option<String>,
8    /// Wait until the changes have been applied?
9    pub(crate) wait: Option<Option<bool>>,
10    pub(crate) points: Option<Vec<PointStruct>>,
11    /// Write ordering guarantees
12    pub(crate) ordering: Option<Option<WriteOrdering>>,
13    /// Option for custom sharding to specify used shard keys
14    pub(crate) shard_key_selector: Option<Option<ShardKeySelector>>,
15    /// Optional filter to apply to the upsert operation. If set, only points matching the filter will be updated, others will be inserted.
16    pub(crate) update_filter: Option<Option<Filter>>,
17    /// Timeout for the request in seconds
18    pub(crate) timeout: Option<Option<u64>>,
19    /// Mode of the upsert operation: insert_only, upsert (default), update_only
20    pub(crate) update_mode: Option<Option<i32>>,
21}
22
23impl UpsertPointsBuilder {
24    /// name of the collection
25    pub fn collection_name(self, value: String) -> Self {
26        let mut new = self;
27        new.collection_name = Option::Some(value);
28        new
29    }
30    /// Wait until the changes have been applied?
31    pub fn wait(self, value: bool) -> Self {
32        let mut new = self;
33        new.wait = Option::Some(Option::Some(value));
34        new
35    }
36    pub fn points(self, value: Vec<PointStruct>) -> Self {
37        let mut new = self;
38        new.points = Option::Some(value);
39        new
40    }
41    /// Write ordering guarantees
42    pub fn ordering<VALUE: core::convert::Into<WriteOrdering>>(self, value: VALUE) -> Self {
43        let mut new = self;
44        new.ordering = Option::Some(Option::Some(value.into()));
45        new
46    }
47    /// Option for custom sharding to specify used shard keys
48    pub fn shard_key_selector<VALUE: core::convert::Into<ShardKeySelector>>(
49        self,
50        value: VALUE,
51    ) -> Self {
52        let mut new = self;
53        new.shard_key_selector = Option::Some(Option::Some(value.into()));
54        new
55    }
56    /// Optional filter to apply to the upsert operation. If set, only points matching the filter will be updated, others will be inserted.
57    pub fn update_filter<VALUE: core::convert::Into<Filter>>(self, value: VALUE) -> Self {
58        let mut new = self;
59        new.update_filter = Option::Some(Option::Some(value.into()));
60        new
61    }
62    /// Timeout for the request in seconds
63    pub fn timeout(self, value: u64) -> Self {
64        let mut new = self;
65        new.timeout = Option::Some(Option::Some(value));
66        new
67    }
68    /// Mode of the upsert operation: insert_only, upsert (default), update_only
69    pub fn update_mode(self, value: UpdateMode) -> Self {
70        let mut new = self;
71        new.update_mode = Option::Some(Option::Some(value.into()));
72        new
73    }
74
75    fn build_inner(self) -> Result<UpsertPoints, UpsertPointsBuilderError> {
76        Ok(UpsertPoints {
77            collection_name: match self.collection_name {
78                Some(value) => value,
79                None => {
80                    return Result::Err(core::convert::Into::into(
81                        ::derive_builder::UninitializedFieldError::from("collection_name"),
82                    ));
83                }
84            },
85            wait: self.wait.unwrap_or_default(),
86            points: match self.points {
87                Some(value) => value,
88                None => {
89                    return Result::Err(core::convert::Into::into(
90                        ::derive_builder::UninitializedFieldError::from("points"),
91                    ));
92                }
93            },
94            ordering: self.ordering.unwrap_or_default(),
95            shard_key_selector: self.shard_key_selector.unwrap_or_default(),
96            update_filter: self.update_filter.unwrap_or_default(),
97            timeout: self.timeout.unwrap_or_default(),
98            update_mode: self.update_mode.unwrap_or_default(),
99        })
100    }
101    /// Create an empty builder, with all fields set to `None` or `PhantomData`.
102    fn create_empty() -> Self {
103        Self {
104            collection_name: core::default::Default::default(),
105            wait: core::default::Default::default(),
106            points: core::default::Default::default(),
107            ordering: core::default::Default::default(),
108            shard_key_selector: core::default::Default::default(),
109            update_filter: core::default::Default::default(),
110            timeout: core::default::Default::default(),
111            update_mode: core::default::Default::default(),
112        }
113    }
114}
115
116impl From<UpsertPointsBuilder> for UpsertPoints {
117    fn from(value: UpsertPointsBuilder) -> Self {
118        value.build_inner().unwrap_or_else(|_| {
119            panic!(
120                "Failed to convert {0} to {1}",
121                "UpsertPointsBuilder", "UpsertPoints"
122            )
123        })
124    }
125}
126
127impl UpsertPointsBuilder {
128    /// Builds the desired type. Can often be omitted.
129    pub fn build(self) -> UpsertPoints {
130        self.build_inner().unwrap_or_else(|_| {
131            panic!(
132                "Failed to build {0} into {1}",
133                "UpsertPointsBuilder", "UpsertPoints"
134            )
135        })
136    }
137}
138
139impl UpsertPointsBuilder {
140    pub(crate) fn empty() -> Self {
141        Self::create_empty()
142    }
143}
144
145#[non_exhaustive]
146#[derive(Debug)]
147pub enum UpsertPointsBuilderError {
148    /// Uninitialized field
149    UninitializedField(&'static str),
150    /// Custom validation error
151    ValidationError(String),
152}
153
154// Implementing the Display trait for better error messages
155impl std::fmt::Display for UpsertPointsBuilderError {
156    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
157        match self {
158            Self::UninitializedField(field) => {
159                write!(f, "`{field}` must be initialized")
160            }
161            Self::ValidationError(error) => write!(f, "{error}"),
162        }
163    }
164}
165
166// Implementing the Error trait
167impl std::error::Error for UpsertPointsBuilderError {}
168
169// Implementing From trait for conversion from UninitializedFieldError
170impl From<derive_builder::UninitializedFieldError> for UpsertPointsBuilderError {
171    fn from(error: derive_builder::UninitializedFieldError) -> Self {
172        Self::UninitializedField(error.field_name())
173    }
174}
175
176// Implementing From trait for conversion from String
177impl From<String> for UpsertPointsBuilderError {
178    fn from(error: String) -> Self {
179        Self::ValidationError(error)
180    }
181}