Skip to main content

qdrant_client/builders/
scalar_quantization_builder.rs

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