Skip to main content

qdrant_client/builders/
facet_counts_builder.rs

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