qdrant_client/builders/
product_quantization_builder.rs

1use crate::qdrant::*;
2
3#[derive(Clone)]
4pub struct ProductQuantizationBuilder {
5    /// Compression ratio
6    pub(crate) compression: Option<i32>,
7    /// If true - quantized vectors always will be stored in RAM, ignoring the config of main storage
8    pub(crate) always_ram: Option<Option<bool>>,
9}
10
11impl ProductQuantizationBuilder {
12    /// Compression ratio
13    #[allow(unused_mut)]
14    pub fn compression(self, value: i32) -> Self {
15        let mut new = self;
16        new.compression = Option::Some(value);
17        new
18    }
19    /// If true - quantized vectors always will be stored in RAM, ignoring the config of main storage
20    #[allow(unused_mut)]
21    pub fn always_ram(self, value: bool) -> Self {
22        let mut new = self;
23        new.always_ram = Option::Some(Option::Some(value));
24        new
25    }
26
27    fn build_inner(self) -> Result<ProductQuantization, ProductQuantizationBuilderError> {
28        Ok(ProductQuantization {
29            compression: match self.compression {
30                Some(value) => value,
31                None => {
32                    return Result::Err(core::convert::Into::into(
33                        ::derive_builder::UninitializedFieldError::from("compression"),
34                    ));
35                }
36            },
37            always_ram: self.always_ram.unwrap_or_default(),
38        })
39    }
40    /// Create an empty builder, with all fields set to `None` or `PhantomData`.
41    fn create_empty() -> Self {
42        Self {
43            compression: core::default::Default::default(),
44            always_ram: core::default::Default::default(),
45        }
46    }
47}
48
49impl From<ProductQuantizationBuilder> for ProductQuantization {
50    fn from(value: ProductQuantizationBuilder) -> Self {
51        value.build_inner().unwrap_or_else(|_| {
52            panic!(
53                "Failed to convert {0} to {1}",
54                "ProductQuantizationBuilder", "ProductQuantization"
55            )
56        })
57    }
58}
59
60impl ProductQuantizationBuilder {
61    /// Builds the desired type. Can often be omitted.
62    pub fn build(self) -> ProductQuantization {
63        self.build_inner().unwrap_or_else(|_| {
64            panic!(
65                "Failed to build {0} into {1}",
66                "ProductQuantizationBuilder", "ProductQuantization"
67            )
68        })
69    }
70}
71
72impl ProductQuantizationBuilder {
73    pub(crate) fn empty() -> Self {
74        Self::create_empty()
75    }
76}
77
78/// Error type for ProductQuantizationBuilder
79#[non_exhaustive]
80#[derive(Debug)]
81pub enum ProductQuantizationBuilderError {
82    /// Uninitialized field
83    UninitializedField(&'static str),
84    /// Custom validation error
85    ValidationError(String),
86}
87
88// Implementing the Display trait for better error messages
89impl std::fmt::Display for ProductQuantizationBuilderError {
90    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
91        match self {
92            Self::UninitializedField(field) => {
93                write!(f, "`{field}` must be initialized")
94            }
95            Self::ValidationError(error) => write!(f, "{error}"),
96        }
97    }
98}
99
100// Implementing the Error trait
101impl std::error::Error for ProductQuantizationBuilderError {}
102
103// Implementing From trait for conversion from UninitializedFieldError
104impl From<derive_builder::UninitializedFieldError> for ProductQuantizationBuilderError {
105    fn from(error: derive_builder::UninitializedFieldError) -> Self {
106        Self::UninitializedField(error.field_name())
107    }
108}
109
110// Implementing From trait for conversion from String
111impl From<String> for ProductQuantizationBuilderError {
112    fn from(error: String) -> Self {
113        Self::ValidationError(error)
114    }
115}