diff --git a/pinecone_sdk/src/models/create_index_request_params.rs b/pinecone_sdk/src/models/create_index_request_params.rs index e930586..2fc5633 100644 --- a/pinecone_sdk/src/models/create_index_request_params.rs +++ b/pinecone_sdk/src/models/create_index_request_params.rs @@ -1,5 +1,7 @@ +use crate::utils::errors::PineconeError; -#[derive(Debug)] + +#[derive(Debug, PartialEq)] pub struct CreateIndexParams { pub name: String, pub dimension: u32, @@ -12,7 +14,7 @@ impl CreateIndexParams { CreateIndexParams { name: name.to_string(), dimension, - metric: metric.map(|x| x).unwrap_or(Metric::Cosine), + metric: metric.unwrap_or(Metric::Cosine), spec, } } @@ -24,8 +26,8 @@ impl CreateIndexParams { } pub struct CreateIndexParamsBuilder { - name: String, - dimension: u32, + name: Option, + dimension: Option, metric: Option, spec: Option, } @@ -33,21 +35,21 @@ pub struct CreateIndexParamsBuilder { impl CreateIndexParamsBuilder { pub fn new() -> CreateIndexParamsBuilder { CreateIndexParamsBuilder { - name: "".to_string(), - dimension: 0, + name: None, + dimension: None, metric: None, spec: None, } } pub fn with_name(mut self, name: &str) -> CreateIndexParamsBuilder { - self.name = name.to_string(); + self.name = Some(name.to_string()); self } pub fn with_dimension(mut self, dimension: u32) -> CreateIndexParamsBuilder { // want to eventually throw an error if dimension is 0? - self.dimension = dimension; + self.dimension = Some(dimension); self } @@ -62,13 +64,26 @@ impl CreateIndexParamsBuilder { } // constructs CreateIndexParams from CreateIndexParamsBuilder fields - pub fn build(self) -> CreateIndexParams { - CreateIndexParams { - name: self.name, - dimension: self.dimension, - metric: self.metric.map(|x| x).unwrap_or(Metric::Cosine), - spec: self.spec.map(|x| x).unwrap(), - } + pub fn build(self) -> Result { + let name = match self.name { + Some(name) => name, + None => Err(PineconeError::MissingNameError)?, + }; + let dimension = match self.dimension { + Some(dimension) => dimension, + None => Err(PineconeError::MissingDimensionError)?, + }; + let spec = match self.spec { + Some(spec) => spec, + None => Err(PineconeError::MissingSpecError)?, + }; + + Ok(CreateIndexParams { + name, + dimension, + metric: self.metric.unwrap_or(Metric::Cosine), + spec, + }) } } @@ -134,7 +149,29 @@ mod tests { cloud: Cloud::Aws, region: "us-west-2".to_string(), }) - .build(); + .build() + .unwrap(); + + assert_eq!(create_index_params.name, "test_index"); + assert_eq!(create_index_params.dimension, 10); + assert_eq!(create_index_params.metric, Metric::Cosine); + assert_eq!(create_index_params.spec, Spec::Serverless { + cloud: Cloud::Aws, + region: "us-west-2".to_string(), + }); + } + + #[test] + fn test_builder_missing_metric() { + let create_index_params = CreateIndexParams::builder() + .with_name("test_index") + .with_dimension(10) + .with_spec(Spec::Serverless { + cloud: Cloud::Aws, + region: "us-west-2".to_string(), + }) + .build() + .unwrap(); assert_eq!(create_index_params.name, "test_index"); assert_eq!(create_index_params.dimension, 10); @@ -144,4 +181,38 @@ mod tests { region: "us-west-2".to_string(), }); } -} \ No newline at end of file + + #[test] + fn test_missing_name() { + let create_index_params = CreateIndexParams::builder() + .with_dimension(10) + .with_metric(Metric::Cosine) + .build(); + + assert!(create_index_params.is_err()); + assert_eq!(create_index_params, Err(PineconeError::MissingNameError)); + } + + #[test] + fn test_missing_dimension() { + let create_index_params = CreateIndexParams::builder() + .with_name("test_index") + .with_metric(Metric::Cosine) + .build(); + + assert!(create_index_params.is_err()); + assert_eq!(create_index_params, Err(PineconeError::MissingDimensionError)); + } + + #[test] + fn test_missing_spec() { + let create_index_params = CreateIndexParams::builder() + .with_name("test_index") + .with_dimension(10) + .build(); + + assert!(create_index_params.is_err()); + assert_eq!(create_index_params, Err(PineconeError::MissingSpecError)); + } + +} diff --git a/pinecone_sdk/src/utils/errors.rs b/pinecone_sdk/src/utils/errors.rs index 639b3dd..c426277 100644 --- a/pinecone_sdk/src/utils/errors.rs +++ b/pinecone_sdk/src/utils/errors.rs @@ -11,6 +11,15 @@ pub enum PineconeError { openapi_error: OpenAPIError, }, + #[snafu(display("Index name missing."))] + MissingNameError, + + #[snafu(display("Dimension missing."))] + MissingDimensionError, + + #[snafu(display("Spec missing."))] + MissingSpecError, + #[snafu(display("Failed to parse headers: {}", json_error))] InvalidHeadersError { json_error: serde_json::Error }, @@ -23,3 +32,20 @@ pub enum PineconeError { #[snafu(display("Invalid region."))] InvalidRegionError, } + +impl PartialEq for PineconeError { + fn eq(&self, other: &Self) -> bool { + match (self, other) { + (PineconeError::APIKeyMissingError, PineconeError::APIKeyMissingError) => true, + (PineconeError::CreateIndexError { .. }, PineconeError::CreateIndexError { .. }) => true, + (PineconeError::MissingNameError, PineconeError::MissingNameError) => true, + (PineconeError::MissingDimensionError, PineconeError::MissingDimensionError) => true, + (PineconeError::MissingSpecError, PineconeError::MissingSpecError) => true, + (PineconeError::InvalidHeadersError { .. }, PineconeError::InvalidHeadersError { .. }) => true, + (PineconeError::InvalidCloudError { .. }, PineconeError::InvalidCloudError { .. }) => true, + (PineconeError::InvalidMetricError { .. }, PineconeError::InvalidMetricError { .. }) => true, + (PineconeError::InvalidRegionError, PineconeError::InvalidRegionError) => true, + _ => false, + } + } +} \ No newline at end of file