Skip to main content

qdrant_client/builders/
product_quantization_builder.rs

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