qdrant_client/builders/
vector_params_builder.rs1use crate::grpc_macros::convert_option;
2use crate::qdrant::*;
3
4#[must_use]
5#[derive(Clone)]
6pub struct VectorParamsBuilder {
7 pub(crate) size: Option<u64>,
9 pub(crate) distance: Option<i32>,
11 pub(crate) hnsw_config: Option<Option<HnswConfigDiff>>,
13 quantization_config: Option<quantization_config::Quantization>,
15 pub(crate) on_disk: Option<Option<bool>>,
17 pub(crate) datatype: Option<Option<i32>>,
19 pub(crate) multivector_config: Option<Option<MultiVectorConfig>>,
21}
22
23impl VectorParamsBuilder {
24 pub fn size(self, value: u64) -> Self {
26 let mut new = self;
27 new.size = Option::Some(value);
28 new
29 }
30 pub fn distance<VALUE: core::convert::Into<i32>>(self, value: VALUE) -> Self {
32 let mut new = self;
33 new.distance = Option::Some(value.into());
34 new
35 }
36 pub fn hnsw_config<VALUE: core::convert::Into<HnswConfigDiff>>(self, value: VALUE) -> Self {
38 let mut new = self;
39 new.hnsw_config = Option::Some(Option::Some(value.into()));
40 new
41 }
42 pub fn quantization_config<VALUE: core::convert::Into<quantization_config::Quantization>>(
44 self,
45 value: VALUE,
46 ) -> Self {
47 let mut new = self;
48 new.quantization_config = Option::Some(value.into());
49 new
50 }
51 pub fn on_disk(self, value: bool) -> Self {
53 let mut new = self;
54 new.on_disk = Option::Some(Option::Some(value));
55 new
56 }
57 pub fn datatype<VALUE: core::convert::Into<i32>>(self, value: VALUE) -> Self {
59 let mut new = self;
60 new.datatype = Option::Some(Option::Some(value.into()));
61 new
62 }
63 pub fn multivector_config<VALUE: core::convert::Into<MultiVectorConfig>>(
65 self,
66 value: VALUE,
67 ) -> Self {
68 let mut new = self;
69 new.multivector_config = Option::Some(Option::Some(value.into()));
70 new
71 }
72
73 fn build_inner(self) -> Result<VectorParams, VectorParamsBuilderError> {
74 Ok(VectorParams {
75 size: self.size.unwrap_or_default(),
76 distance: self.distance.unwrap_or_default(),
77 hnsw_config: self.hnsw_config.unwrap_or_default(),
78 quantization_config: { convert_option(&self.quantization_config) },
79 on_disk: self.on_disk.unwrap_or_default(),
80 datatype: self.datatype.unwrap_or_default(),
81 multivector_config: self.multivector_config.unwrap_or_default(),
82 })
83 }
84 fn create_empty() -> Self {
86 Self {
87 size: core::default::Default::default(),
88 distance: core::default::Default::default(),
89 hnsw_config: core::default::Default::default(),
90 quantization_config: core::default::Default::default(),
91 on_disk: core::default::Default::default(),
92 datatype: core::default::Default::default(),
93 multivector_config: core::default::Default::default(),
94 }
95 }
96}
97
98impl From<VectorParamsBuilder> for VectorParams {
99 fn from(value: VectorParamsBuilder) -> Self {
100 value.build_inner().unwrap_or_else(|_| {
101 panic!(
102 "Failed to convert {0} to {1}",
103 "VectorParamsBuilder", "VectorParams"
104 )
105 })
106 }
107}
108
109impl VectorParamsBuilder {
110 pub fn build(self) -> VectorParams {
112 self.build_inner().unwrap_or_else(|_| {
113 panic!(
114 "Failed to build {0} into {1}",
115 "VectorParamsBuilder", "VectorParams"
116 )
117 })
118 }
119}
120
121impl VectorParamsBuilder {
122 pub(crate) fn empty() -> Self {
123 Self::create_empty()
124 }
125}
126
127#[non_exhaustive]
128#[derive(Debug)]
129pub enum VectorParamsBuilderError {
130 UninitializedField(&'static str),
132 ValidationError(String),
134}
135
136impl std::fmt::Display for VectorParamsBuilderError {
138 fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
139 match self {
140 Self::UninitializedField(field) => {
141 write!(f, "`{field}` must be initialized")
142 }
143 Self::ValidationError(error) => write!(f, "{error}"),
144 }
145 }
146}
147
148impl std::error::Error for VectorParamsBuilderError {}
150
151impl From<derive_builder::UninitializedFieldError> for VectorParamsBuilderError {
153 fn from(error: derive_builder::UninitializedFieldError) -> Self {
154 Self::UninitializedField(error.field_name())
155 }
156}
157
158impl From<String> for VectorParamsBuilderError {
160 fn from(error: String) -> Self {
161 Self::ValidationError(error)
162 }
163}