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