Skip to main content

qdrant_client/builders/
facet_counts_builder.rs

1use crate::qdrant::*;
2
3#[must_use]
4#[derive(Clone)]
5pub struct FacetCountsBuilder {
6    /// Name of the collection
7    pub(crate) collection_name: Option<String>,
8    /// Payload key of the facet
9    pub(crate) key: Option<String>,
10    /// Filter conditions - return only those points that satisfy the specified conditions.
11    pub(crate) filter: Option<Option<Filter>>,
12    /// Max number of facets. Default is 10.
13    pub(crate) limit: Option<Option<u64>>,
14    /// If true, return exact counts, slower but useful for debugging purposes. Default is false.
15    pub(crate) exact: Option<Option<bool>>,
16    /// If set, overrides global timeout setting for this request. Unit is seconds.
17    pub(crate) timeout: Option<Option<u64>>,
18    /// Options for specifying read consistency guarantees
19    pub(crate) read_consistency: Option<Option<ReadConsistency>>,
20    /// Specify in which shards to look for the points, if not specified - look in all shards
21    pub(crate) shard_key_selector: Option<Option<ShardKeySelector>>,
22}
23
24impl FacetCountsBuilder {
25    /// Name of the collection
26    pub fn collection_name(self, value: String) -> Self {
27        let mut new = self;
28        new.collection_name = Option::Some(value);
29        new
30    }
31    /// Payload key of the facet
32    pub fn key(self, value: String) -> Self {
33        let mut new = self;
34        new.key = Option::Some(value);
35        new
36    }
37    /// Filter conditions - return only those points that satisfy the specified conditions.
38    pub fn filter<VALUE: core::convert::Into<Filter>>(self, value: VALUE) -> Self {
39        let mut new = self;
40        new.filter = Option::Some(Option::Some(value.into()));
41        new
42    }
43    /// Max number of facets. Default is 10.
44    pub fn limit(self, value: u64) -> Self {
45        let mut new = self;
46        new.limit = Option::Some(Option::Some(value));
47        new
48    }
49    /// If true, return exact counts, slower but useful for debugging purposes. Default is false.
50    pub fn exact(self, value: bool) -> Self {
51        let mut new = self;
52        new.exact = Option::Some(Option::Some(value));
53        new
54    }
55    /// If set, overrides global timeout setting for this request. Unit is seconds.
56    pub fn timeout(self, value: u64) -> Self {
57        let mut new = self;
58        new.timeout = Option::Some(Option::Some(value));
59        new
60    }
61    /// Options for specifying read consistency guarantees
62    pub fn read_consistency<VALUE: core::convert::Into<ReadConsistency>>(
63        self,
64        value: VALUE,
65    ) -> Self {
66        let mut new = self;
67        new.read_consistency = Option::Some(Option::Some(value.into()));
68        new
69    }
70    /// Specify in which shards to look for the points, if not specified - look in all shards
71    pub fn shard_key_selector<VALUE: core::convert::Into<ShardKeySelector>>(
72        self,
73        value: VALUE,
74    ) -> Self {
75        let mut new = self;
76        new.shard_key_selector = Option::Some(Option::Some(value.into()));
77        new
78    }
79
80    fn build_inner(self) -> Result<FacetCounts, FacetCountsBuilderError> {
81        Ok(FacetCounts {
82            collection_name: match self.collection_name {
83                Some(value) => value,
84                None => {
85                    return Result::Err(core::convert::Into::into(
86                        ::derive_builder::UninitializedFieldError::from("collection_name"),
87                    ));
88                }
89            },
90            key: match self.key {
91                Some(value) => value,
92                None => {
93                    return Result::Err(core::convert::Into::into(
94                        ::derive_builder::UninitializedFieldError::from("key"),
95                    ));
96                }
97            },
98            filter: self.filter.unwrap_or_default(),
99            limit: self.limit.unwrap_or_default(),
100            exact: self.exact.unwrap_or_default(),
101            timeout: self.timeout.unwrap_or_default(),
102            read_consistency: self.read_consistency.unwrap_or_default(),
103            shard_key_selector: self.shard_key_selector.unwrap_or_default(),
104        })
105    }
106    /// Create an empty builder, with all fields set to `None` or `PhantomData`.
107    fn create_empty() -> Self {
108        Self {
109            collection_name: core::default::Default::default(),
110            key: core::default::Default::default(),
111            filter: core::default::Default::default(),
112            limit: core::default::Default::default(),
113            exact: core::default::Default::default(),
114            timeout: core::default::Default::default(),
115            read_consistency: core::default::Default::default(),
116            shard_key_selector: core::default::Default::default(),
117        }
118    }
119}
120
121impl From<FacetCountsBuilder> for FacetCounts {
122    fn from(value: FacetCountsBuilder) -> Self {
123        value.build_inner().unwrap_or_else(|_| {
124            panic!(
125                "Failed to convert {0} to {1}",
126                "FacetCountsBuilder", "FacetCounts"
127            )
128        })
129    }
130}
131
132impl FacetCountsBuilder {
133    /// Builds the desired type. Can often be omitted.
134    pub fn build(self) -> FacetCounts {
135        self.build_inner().unwrap_or_else(|_| {
136            panic!(
137                "Failed to build {0} into {1}",
138                "FacetCountsBuilder", "FacetCounts"
139            )
140        })
141    }
142}
143
144impl FacetCountsBuilder {
145    pub(crate) fn empty() -> Self {
146        Self::create_empty()
147    }
148}
149
150/// Error type for FacetCountsBuilder
151#[non_exhaustive]
152#[derive(Debug)]
153pub enum FacetCountsBuilderError {
154    /// Uninitialized field
155    UninitializedField(&'static str),
156    /// Custom validation error
157    ValidationError(String),
158}
159
160// Implementing the From trait for conversion from UninitializedFieldError
161impl From<derive_builder::UninitializedFieldError> for FacetCountsBuilderError {
162    fn from(s: derive_builder::UninitializedFieldError) -> Self {
163        Self::UninitializedField(s.field_name())
164    }
165}
166
167// Implementing the From trait for conversion from String
168impl From<String> for FacetCountsBuilderError {
169    fn from(s: String) -> Self {
170        Self::ValidationError(s)
171    }
172}
173
174// Implementing the Display trait for better error messages
175impl std::fmt::Display for FacetCountsBuilderError {
176    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
177        match self {
178            Self::UninitializedField(ref field) => {
179                write!(f, "`{field}` must be initialized")
180            }
181            Self::ValidationError(ref error) => write!(f, "{error}"),
182        }
183    }
184}
185
186// Implementing the Error trait
187impl std::error::Error for FacetCountsBuilderError {}