Skip to main content

qdrant_client/builders/
replicate_shard_builder.rs

1use crate::qdrant::*;
2
3#[must_use]
4#[derive(Clone)]
5pub struct ReplicateShardBuilder {
6    /// Local shard id
7    pub(crate) shard_id: Option<u32>,
8    pub(crate) to_shard_id: Option<Option<u32>>,
9    pub(crate) from_peer_id: Option<u64>,
10    pub(crate) to_peer_id: Option<u64>,
11    pub(crate) method: Option<Option<i32>>,
12}
13
14impl ReplicateShardBuilder {
15    /// Local shard id
16    pub fn shard_id(self, value: u32) -> Self {
17        let mut new = self;
18        new.shard_id = Option::Some(value);
19        new
20    }
21    pub fn to_shard_id(self, value: u32) -> Self {
22        let mut new = self;
23        new.to_shard_id = Option::Some(Option::Some(value));
24        new
25    }
26    pub fn from_peer_id(self, value: u64) -> Self {
27        let mut new = self;
28        new.from_peer_id = Option::Some(value);
29        new
30    }
31    pub fn to_peer_id(self, value: u64) -> Self {
32        let mut new = self;
33        new.to_peer_id = Option::Some(value);
34        new
35    }
36    pub fn method<VALUE: core::convert::Into<i32>>(self, value: VALUE) -> Self {
37        let mut new = self;
38        new.method = Option::Some(Option::Some(value.into()));
39        new
40    }
41
42    fn build_inner(self) -> Result<ReplicateShard, ReplicateShardBuilderError> {
43        Ok(ReplicateShard {
44            shard_id: match self.shard_id {
45                Some(value) => value,
46                None => {
47                    return Result::Err(core::convert::Into::into(
48                        ::derive_builder::UninitializedFieldError::from("shard_id"),
49                    ));
50                }
51            },
52            to_shard_id: self.to_shard_id.unwrap_or_default(),
53            from_peer_id: match self.from_peer_id {
54                Some(value) => value,
55                None => {
56                    return Result::Err(core::convert::Into::into(
57                        ::derive_builder::UninitializedFieldError::from("from_peer_id"),
58                    ));
59                }
60            },
61            to_peer_id: match self.to_peer_id {
62                Some(value) => value,
63                None => {
64                    return Result::Err(core::convert::Into::into(
65                        ::derive_builder::UninitializedFieldError::from("to_peer_id"),
66                    ));
67                }
68            },
69            method: self.method.unwrap_or_default(),
70        })
71    }
72    /// Create an empty builder, with all fields set to `None` or `PhantomData`.
73    fn create_empty() -> Self {
74        Self {
75            shard_id: core::default::Default::default(),
76            to_shard_id: core::default::Default::default(),
77            from_peer_id: core::default::Default::default(),
78            to_peer_id: core::default::Default::default(),
79            method: core::default::Default::default(),
80        }
81    }
82}
83
84impl From<ReplicateShardBuilder> for ReplicateShard {
85    fn from(value: ReplicateShardBuilder) -> Self {
86        value.build_inner().unwrap_or_else(|_| {
87            panic!(
88                "Failed to convert {0} to {1}",
89                "ReplicateShardBuilder", "ReplicateShard"
90            )
91        })
92    }
93}
94
95impl ReplicateShardBuilder {
96    /// Builds the desired type. Can often be omitted.
97    pub fn build(self) -> ReplicateShard {
98        self.build_inner().unwrap_or_else(|_| {
99            panic!(
100                "Failed to build {0} into {1}",
101                "ReplicateShardBuilder", "ReplicateShard"
102            )
103        })
104    }
105}
106
107impl ReplicateShardBuilder {
108    pub(crate) fn empty() -> Self {
109        Self::create_empty()
110    }
111}
112
113///Error type for ReplicateShardBuilder
114#[non_exhaustive]
115#[derive(Debug)]
116pub enum ReplicateShardBuilderError {
117    /// Uninitialized field
118    UninitializedField(&'static str),
119    /// Custom validation error
120    ValidationError(String),
121}
122impl From<derive_builder::UninitializedFieldError> for ReplicateShardBuilderError {
123    fn from(error: derive_builder::UninitializedFieldError) -> Self {
124        Self::UninitializedField(error.field_name())
125    }
126}
127impl From<String> for ReplicateShardBuilderError {
128    fn from(error: String) -> Self {
129        Self::ValidationError(error)
130    }
131}
132impl std::fmt::Display for ReplicateShardBuilderError {
133    fn fmt(&self, f: &mut std::fmt::Formatter) -> ::derive_builder::export::core::fmt::Result {
134        match self {
135            Self::UninitializedField(field) => {
136                write!(f, "`{field}` must be initialized")
137            }
138            Self::ValidationError(error) => write!(f, "{error}"),
139        }
140    }
141}
142impl std::error::Error for ReplicateShardBuilderError {}