Skip to main content

qdrant_client/builders/
count_points_builder.rs

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