Skip to main content

qdrant_client/builders/
strict_mode_sparse_config_builder.rs

1use std::collections::HashMap;
2
3use crate::qdrant::{StrictModeSparse, StrictModeSparseConfig};
4
5/// Builder for StrictModeSparseConfig, which defines sparse vector configuration for strict mode.
6#[must_use]
7#[derive(Clone)]
8pub struct StrictModeSparseConfigBuilder {
9    /// The sparse vectors configuration map, where keys are vector names and values are their configurations.
10    pub(crate) sparse_config: HashMap<String, StrictModeSparse>,
11}
12
13impl StrictModeSparseConfigBuilder {
14    /// Create a new builder with an empty sparse vectors configuration map.
15    pub fn new() -> Self {
16        Self {
17            sparse_config: HashMap::new(),
18        }
19    }
20
21    /// Add a configuration for a named vector, specifying its maximum number of vectors.
22    pub fn add_vector_config<S: Into<String>>(
23        self,
24        name: S,
25        strict_mode_multivector: StrictModeSparse,
26    ) -> Self {
27        let mut new = self;
28        new.sparse_config
29            .insert(name.into(), strict_mode_multivector);
30
31        new
32    }
33
34    /// Set the entire sparse vectors configuration map at once.
35    pub fn sparse_config<M: Into<HashMap<String, StrictModeSparse>>>(self, config: M) -> Self {
36        let mut new = self;
37        new.sparse_config = config.into();
38        new
39    }
40
41    fn build_inner(self) -> Result<StrictModeSparseConfig, std::convert::Infallible> {
42        Ok(StrictModeSparseConfig {
43            sparse_config: self.sparse_config,
44        })
45    }
46
47    /// Create an empty builder, with all fields set to `None` or default values.
48    fn create_empty() -> Self {
49        Self {
50            sparse_config: core::default::Default::default(),
51        }
52    }
53}
54
55impl From<StrictModeSparseConfigBuilder> for StrictModeSparseConfig {
56    fn from(value: StrictModeSparseConfigBuilder) -> Self {
57        value.build_inner().unwrap_or_else(|_| {
58            panic!(
59                "Failed to convert {0} to {1}",
60                "StrictModeSparseConfigBuilder", "StrictModeSparseConfig"
61            )
62        })
63    }
64}
65
66impl StrictModeSparseConfigBuilder {
67    /// Builds the desired StrictModeSparseConfig type.
68    pub fn build(self) -> StrictModeSparseConfig {
69        self.build_inner().unwrap_or_else(|_| {
70            panic!(
71                "Failed to build {0} into {1}",
72                "StrictModeSparseConfigBuilder", "StrictModeSparseConfig"
73            )
74        })
75    }
76}
77
78impl Default for StrictModeSparseConfigBuilder {
79    fn default() -> Self {
80        Self::create_empty()
81    }
82}