Skip to main content

qdrant_client/builders/
turbo_quantization_builder.rs

1use crate::qdrant::*;
2
3#[must_use]
4#[derive(Clone)]
5pub struct TurboQuantizationBuilder {
6    /// If true - quantized vectors always will be stored in RAM, ignoring the config of main storage
7    pub(crate) always_ram: Option<Option<bool>>,
8    pub(crate) bits: Option<Option<i32>>,
9}
10
11impl TurboQuantizationBuilder {
12    /// If true - quantized vectors always will be stored in RAM, ignoring the config of main storage
13    pub fn always_ram(self, value: bool) -> Self {
14        let mut new = self;
15        new.always_ram = Some(Some(value));
16        new
17    }
18
19    /// Number of bits used to encode each component of the quantized vector.
20    pub fn bits(self, value: impl Into<TurboQuantBitSize>) -> Self {
21        let mut new = self;
22        let bits: TurboQuantBitSize = value.into();
23        new.bits = Some(Some(bits.into()));
24        new
25    }
26
27    fn build_inner(self) -> Result<TurboQuantization, TurboQuantizationBuilderError> {
28        Ok(TurboQuantization {
29            always_ram: self.always_ram.unwrap_or_default(),
30            bits: self.bits.unwrap_or_default(),
31        })
32    }
33
34    /// Create an empty builder, with all fields set to `None` or `PhantomData`.
35    fn create_empty() -> Self {
36        Self {
37            always_ram: Default::default(),
38            bits: Default::default(),
39        }
40    }
41}
42
43impl From<TurboQuantizationBuilder> for TurboQuantization {
44    fn from(value: TurboQuantizationBuilder) -> Self {
45        value.build_inner().unwrap_or_else(|_| {
46            panic!(
47                "Failed to convert {0} to {1}",
48                "TurboQuantizationBuilder", "TurboQuantization"
49            )
50        })
51    }
52}
53
54impl TurboQuantizationBuilder {
55    /// Builds the desired type. Can often be omitted.
56    pub fn build(self) -> TurboQuantization {
57        self.build_inner().unwrap_or_else(|_| {
58            panic!(
59                "Failed to build {0} into {1}",
60                "TurboQuantizationBuilder", "TurboQuantization"
61            )
62        })
63    }
64}
65
66impl TurboQuantizationBuilder {
67    pub(crate) fn empty() -> Self {
68        Self::create_empty()
69    }
70}
71
72impl Default for TurboQuantizationBuilder {
73    fn default() -> Self {
74        Self::create_empty()
75    }
76}
77
78#[non_exhaustive]
79#[derive(Debug)]
80pub enum TurboQuantizationBuilderError {
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 TurboQuantizationBuilderError {
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 TurboQuantizationBuilderError {}
101
102// Implementing From trait for conversion from UninitializedFieldError
103impl From<derive_builder::UninitializedFieldError> for TurboQuantizationBuilderError {
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 TurboQuantizationBuilderError {
111    fn from(error: String) -> Self {
112        Self::ValidationError(error)
113    }
114}