Skip to content

Commit

Permalink
Replace macro generated builder types with unrolled ones (#199)
Browse files Browse the repository at this point in the history
* Replace macro generated builder types with unrolled ones

Fixes: #195

* fix clippy and implement missing defaults

* unexpose builder module and remove comment

---------

Co-authored-by: generall <[email protected]>
  • Loading branch information
palash25 and generall authored Nov 26, 2024
1 parent b203fd7 commit 0088e91
Show file tree
Hide file tree
Showing 79 changed files with 10,624 additions and 2,629 deletions.
141 changes: 141 additions & 0 deletions src/builders/abort_shard_transfer_builder.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
use crate::qdrant::*;

pub struct AbortShardTransferBuilder {
/// Local shard id
pub(crate) shard_id: Option<u32>,
pub(crate) to_shard_id: Option<Option<u32>>,
pub(crate) from_peer_id: Option<u64>,
pub(crate) to_peer_id: Option<u64>,
}

impl AbortShardTransferBuilder {
/// Local shard id
#[allow(unused_mut)]
pub fn shard_id(self, value: u32) -> Self {
let mut new = self;
new.shard_id = Option::Some(value);
new
}
#[allow(unused_mut)]
pub fn to_shard_id(self, value: u32) -> Self {
let mut new = self;
new.to_shard_id = Option::Some(Option::Some(value));
new
}
#[allow(unused_mut)]
pub fn from_peer_id(self, value: u64) -> Self {
let mut new = self;
new.from_peer_id = Option::Some(value);
new
}
#[allow(unused_mut)]
pub fn to_peer_id(self, value: u64) -> Self {
let mut new = self;
new.to_peer_id = Option::Some(value);
new
}
fn build_inner(self) -> Result<AbortShardTransfer, AbortShardTransferBuilderError> {
Ok(AbortShardTransfer {
shard_id: match self.shard_id {
Some(value) => value,
None => {
return Result::Err(core::convert::Into::into(
::derive_builder::UninitializedFieldError::from("shard_id"),
));
}
},
to_shard_id: self.to_shard_id.unwrap_or_default(),
from_peer_id: match self.from_peer_id {
Some(value) => value,
None => {
return Result::Err(core::convert::Into::into(
::derive_builder::UninitializedFieldError::from("from_peer_id"),
));
}
},
to_peer_id: match self.to_peer_id {
Some(value) => value,
None => {
return Result::Err(core::convert::Into::into(
::derive_builder::UninitializedFieldError::from("to_peer_id"),
));
}
},
})
}
/// Create an empty builder, with all fields set to `None` or `PhantomData`.
fn create_empty() -> Self {
Self {
shard_id: core::default::Default::default(),
to_shard_id: core::default::Default::default(),
from_peer_id: core::default::Default::default(),
to_peer_id: core::default::Default::default(),
}
}
}

impl From<AbortShardTransferBuilder> for AbortShardTransfer {
fn from(value: AbortShardTransferBuilder) -> Self {
value.build_inner().unwrap_or_else(|_| {
panic!(
"Failed to convert {0} to {1}",
"AbortShardTransferBuilder", "AbortShardTransfer"
)
})
}
}

impl AbortShardTransferBuilder {
/// Builds the desired type. Can often be omitted.
pub fn build(self) -> AbortShardTransfer {
self.build_inner().unwrap_or_else(|_| {
panic!(
"Failed to build {0} into {1}",
"AbortShardTransferBuilder", "AbortShardTransfer"
)
})
}
}

impl AbortShardTransferBuilder {
pub(crate) fn empty() -> Self {
Self::create_empty()
}
}

// src/builders/abort_shard_transfer_builder.rs

#[non_exhaustive]
#[derive(Debug)]
pub enum AbortShardTransferBuilderError {
/// Uninitialized field
UninitializedField(&'static str),
/// Custom validation error
ValidationError(String),
}

// Implementing the Display trait for better error messages
impl std::fmt::Display for AbortShardTransferBuilderError {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
match self {
Self::UninitializedField(field) => {
write!(f, "`{}` must be initialized", field)
}
Self::ValidationError(error) => write!(f, "{}", error),
}
}
}

impl std::error::Error for AbortShardTransferBuilderError {}

impl From<derive_builder::UninitializedFieldError> for AbortShardTransferBuilderError {
fn from(error: derive_builder::UninitializedFieldError) -> Self {
Self::UninitializedField(error.field_name())
}
}

impl From<String> for AbortShardTransferBuilderError {
fn from(error: String) -> Self {
Self::ValidationError(error)
}
}
97 changes: 97 additions & 0 deletions src/builders/binary_quantization_builder.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
use crate::qdrant::*;

pub struct BinaryQuantizationBuilder {
/// If true - quantized vectors always will be stored in RAM, ignoring the config of main storage
pub(crate) always_ram: Option<Option<bool>>,
}

impl BinaryQuantizationBuilder {
/// If true - quantized vectors always will be stored in RAM, ignoring the config of main storage
#[allow(unused_mut)]
pub fn always_ram(self, value: bool) -> Self {
let mut new = self;
new.always_ram = Option::Some(Option::Some(value));
new
}

fn build_inner(self) -> Result<BinaryQuantization, BinaryQuantizationBuilderError> {
Ok(BinaryQuantization {
always_ram: self.always_ram.unwrap_or_default(),
})
}
/// Create an empty builder, with all fields set to `None` or `PhantomData`.
fn create_empty() -> Self {
Self {
always_ram: core::default::Default::default(),
}
}
}

impl From<BinaryQuantizationBuilder> for BinaryQuantization {
fn from(value: BinaryQuantizationBuilder) -> Self {
value.build_inner().unwrap_or_else(|_| {
panic!(
"Failed to convert {0} to {1}",
"BinaryQuantizationBuilder", "BinaryQuantization"
)
})
}
}

impl BinaryQuantizationBuilder {
/// Builds the desired type. Can often be omitted.
pub fn build(self) -> BinaryQuantization {
self.build_inner().unwrap_or_else(|_| {
panic!(
"Failed to build {0} into {1}",
"BinaryQuantizationBuilder", "BinaryQuantization"
)
})
}
}

impl BinaryQuantizationBuilder {
pub(crate) fn empty() -> Self {
Self::create_empty()
}
}

// src/builders/binary_quantization_builder.rs

#[non_exhaustive]
#[derive(Debug)]
pub enum BinaryQuantizationBuilderError {
/// Uninitialized field
UninitializedField(&'static str),
/// Custom validation error
ValidationError(String),
}

// Implementing the Display trait for better error messages
impl std::fmt::Display for BinaryQuantizationBuilderError {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
match self {
Self::UninitializedField(field) => {
write!(f, "`{}` must be initialized", field)
}
Self::ValidationError(error) => write!(f, "{}", error),
}
}
}

// Implementing the Error trait
impl std::error::Error for BinaryQuantizationBuilderError {}

// Implementing From trait for conversion from UninitializedFieldError
impl From<derive_builder::UninitializedFieldError> for BinaryQuantizationBuilderError {
fn from(error: derive_builder::UninitializedFieldError) -> Self {
Self::UninitializedField(error.field_name())
}
}

// Implementing From trait for conversion from String
impl From<String> for BinaryQuantizationBuilderError {
fn from(error: String) -> Self {
Self::ValidationError(error)
}
}
149 changes: 149 additions & 0 deletions src/builders/clear_payload_points_builder.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
use crate::grpc_macros::convert_option;
use crate::qdrant::*;

pub struct ClearPayloadPointsBuilder {
/// name of the collection
pub(crate) collection_name: Option<String>,
/// Wait until the changes have been applied?
pub(crate) wait: Option<Option<bool>>,
/// Affected points
points: Option<points_selector::PointsSelectorOneOf>,
/// Write ordering guarantees
pub(crate) ordering: Option<Option<WriteOrdering>>,
/// Option for custom sharding to specify used shard keys
pub(crate) shard_key_selector: Option<Option<ShardKeySelector>>,
}

impl ClearPayloadPointsBuilder {
/// name of the collection
#[allow(unused_mut)]
pub fn collection_name(self, value: String) -> Self {
let mut new = self;
new.collection_name = Option::Some(value);
new
}
/// Wait until the changes have been applied?
#[allow(unused_mut)]
pub fn wait(self, value: bool) -> Self {
let mut new = self;
new.wait = Option::Some(Option::Some(value));
new
}
/// Affected points
#[allow(unused_mut)]
pub fn points<VALUE: core::convert::Into<points_selector::PointsSelectorOneOf>>(
self,
value: VALUE,
) -> Self {
let mut new = self;
new.points = Option::Some(value.into());
new
}
/// Write ordering guarantees
#[allow(unused_mut)]
pub fn ordering<VALUE: core::convert::Into<WriteOrdering>>(self, value: VALUE) -> Self {
let mut new = self;
new.ordering = Option::Some(Option::Some(value.into()));
new
}
/// Option for custom sharding to specify used shard keys
#[allow(unused_mut)]
pub fn shard_key_selector<VALUE: core::convert::Into<ShardKeySelector>>(
self,
value: VALUE,
) -> Self {
let mut new = self;
new.shard_key_selector = Option::Some(Option::Some(value.into()));
new
}

fn build_inner(self) -> Result<ClearPayloadPoints, ClearPayloadPointsBuilderError> {
Ok(ClearPayloadPoints {
collection_name: match self.collection_name {
Some(value) => value,
None => {
return Result::Err(core::convert::Into::into(
::derive_builder::UninitializedFieldError::from("collection_name"),
));
}
},
wait: self.wait.unwrap_or_default(),
points: { convert_option(&self.points) },
ordering: self.ordering.unwrap_or_default(),
shard_key_selector: self.shard_key_selector.unwrap_or_default(),
})
}
/// Create an empty builder, with all fields set to `None` or `PhantomData`.
fn create_empty() -> Self {
Self {
collection_name: core::default::Default::default(),
wait: core::default::Default::default(),
points: core::default::Default::default(),
ordering: core::default::Default::default(),
shard_key_selector: core::default::Default::default(),
}
}
}

impl From<ClearPayloadPointsBuilder> for ClearPayloadPoints {
fn from(value: ClearPayloadPointsBuilder) -> Self {
value.build_inner().unwrap_or_else(|_| {
panic!(
"Failed to convert {0} to {1}",
"ClearPayloadPointsBuilder", "ClearPayloadPoints"
)
})
}
}

impl ClearPayloadPointsBuilder {
/// Builds the desired type. Can often be omitted.
pub fn build(self) -> ClearPayloadPoints {
self.build_inner().unwrap_or_else(|_| {
panic!(
"Failed to build {0} into {1}",
"ClearPayloadPointsBuilder", "ClearPayloadPoints"
)
})
}
}

impl ClearPayloadPointsBuilder {
pub(crate) fn empty() -> Self {
Self::create_empty()
}
}
/// Error type for ClearPayloadPointsBuilder
#[non_exhaustive]
#[derive(Debug)]
pub enum ClearPayloadPointsBuilderError {
/// Uninitialized field
UninitializedField(&'static str),
/// Custom validation error
ValidationError(String),
}

impl std::fmt::Display for ClearPayloadPointsBuilderError {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
match self {
Self::UninitializedField(field) => {
write!(f, "`{}` must be initialized", field)
}
Self::ValidationError(error) => write!(f, "{}", error),
}
}
}

impl std::error::Error for ClearPayloadPointsBuilderError {}

impl From<derive_builder::UninitializedFieldError> for ClearPayloadPointsBuilderError {
fn from(error: derive_builder::UninitializedFieldError) -> Self {
Self::UninitializedField(error.field_name())
}
}

impl From<String> for ClearPayloadPointsBuilderError {
fn from(error: String) -> Self {
Self::ValidationError(error)
}
}
Loading

0 comments on commit 0088e91

Please sign in to comment.