Skip to main content

qdrant_client/builders/
context_input_pair_builder.rs

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