qdrant_client/builders/
scalar_quantization_builder.rs

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