Skip to main content

qdrant_client/builders/
discover_input_builder.rs

1use crate::qdrant::*;
2
3#[must_use]
4#[derive(Clone)]
5pub struct DiscoverInputBuilder {
6    /// Use this as the primary search objective
7    pub(crate) target: Option<Option<VectorInput>>,
8    /// Search space will be constrained by these pairs of vectors
9    pub(crate) context: Option<Option<ContextInput>>,
10}
11
12impl DiscoverInputBuilder {
13    /// Use this as the primary search objective
14    pub fn target<VALUE: core::convert::Into<VectorInput>>(self, value: VALUE) -> Self {
15        let mut new = self;
16        new.target = Option::Some(Option::Some(value.into()));
17        new
18    }
19    /// Search space will be constrained by these pairs of vectors
20    pub fn context<VALUE: core::convert::Into<ContextInput>>(self, value: VALUE) -> Self {
21        let mut new = self;
22        new.context = Option::Some(Option::Some(value.into()));
23        new
24    }
25
26    fn build_inner(self) -> Result<DiscoverInput, DiscoverInputBuilderError> {
27        Ok(DiscoverInput {
28            target: self.target.unwrap_or_default(),
29            context: self.context.unwrap_or_default(),
30        })
31    }
32    /// Create an empty builder, with all fields set to `None` or `PhantomData`.
33    fn create_empty() -> Self {
34        Self {
35            target: core::default::Default::default(),
36            context: core::default::Default::default(),
37        }
38    }
39}
40
41impl From<DiscoverInputBuilder> for DiscoverInput {
42    fn from(value: DiscoverInputBuilder) -> Self {
43        value.build_inner().unwrap_or_else(|_| {
44            panic!(
45                "Failed to convert {0} to {1}",
46                "DiscoverInputBuilder", "DiscoverInput"
47            )
48        })
49    }
50}
51
52impl DiscoverInputBuilder {
53    /// Builds the desired type. Can often be omitted.
54    pub fn build(self) -> DiscoverInput {
55        self.build_inner().unwrap_or_else(|_| {
56            panic!(
57                "Failed to build {0} into {1}",
58                "DiscoverInputBuilder", "DiscoverInput"
59            )
60        })
61    }
62}
63
64impl DiscoverInputBuilder {
65    pub(crate) fn empty() -> Self {
66        Self::create_empty()
67    }
68}
69
70/// Error type for DiscoverInputBuilder
71#[non_exhaustive]
72#[derive(Debug)]
73pub enum DiscoverInputBuilderError {
74    /// Uninitialized field
75    UninitializedField(&'static str),
76    /// Custom validation error
77    ValidationError(String),
78}
79
80// Implementing the From trait for conversion from UninitializedFieldError
81impl From<derive_builder::UninitializedFieldError> for DiscoverInputBuilderError {
82    fn from(s: derive_builder::UninitializedFieldError) -> Self {
83        Self::UninitializedField(s.field_name())
84    }
85}
86
87// Implementing the From trait for conversion from String
88impl From<String> for DiscoverInputBuilderError {
89    fn from(s: String) -> Self {
90        Self::ValidationError(s)
91    }
92}
93
94// Implementing the Display trait for better error messages
95impl std::fmt::Display for DiscoverInputBuilderError {
96    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
97        match self {
98            Self::UninitializedField(ref field) => {
99                write!(f, "`{field}` must be initialized")
100            }
101            Self::ValidationError(ref error) => write!(f, "{error}"),
102        }
103    }
104}
105
106// Implementing the Error trait
107impl std::error::Error for DiscoverInputBuilderError {}