From 3a0d5a15cc79043912dca21d3dedde6212373b97 Mon Sep 17 00:00:00 2001 From: postmeback Date: Sun, 7 Jul 2024 21:19:34 +0530 Subject: [PATCH 1/4] Issues Fixed --- src/client.rs | 36 ++++++++++++++++++++++++++---------- src/errors.rs | 5 +++++ 2 files changed, 31 insertions(+), 10 deletions(-) diff --git a/src/client.rs b/src/client.rs index 247954c2..a76951a0 100644 --- a/src/client.rs +++ b/src/client.rs @@ -79,18 +79,34 @@ impl Client { &self, value: &Value, ) -> Result, Error> { - let raw_indexes = value["results"].as_array().unwrap(); - + let raw_indexes = value["results"] + .as_array() + .ok_or_else(|| Error::ParseStringError("Missing or invalid 'results' field".to_string()))?; + + let limit = value["limit"] + .as_u64() + .ok_or_else(|| Error::ParseStringError("Missing or invalid 'limit' field".to_string()))? as u32; + + let offset = value["offset"] + .as_u64() + .ok_or_else(|| Error::ParseStringError("Missing or invalid 'offset' field".to_string()))? as u32; + + let total = value["total"] + .as_u64() + .ok_or_else(|| Error::ParseStringError("Missing or invalid 'total' field".to_string()))? as u32; + + let results = raw_indexes + .iter() + .map(|raw_index| Index::from_value(raw_index.clone(), self.clone())) + .collect::>()?; + let indexes_results = IndexesResults { - limit: value["limit"].as_u64().unwrap() as u32, - offset: value["offset"].as_u64().unwrap() as u32, - total: value["total"].as_u64().unwrap() as u32, - results: raw_indexes - .iter() - .map(|raw_index| Index::from_value(raw_index.clone(), self.clone())) - .collect::>()?, + limit, + offset, + total, + results, }; - + Ok(indexes_results) } diff --git a/src/errors.rs b/src/errors.rs index 7faa5b9a..63a2da19 100644 --- a/src/errors.rs +++ b/src/errors.rs @@ -17,6 +17,11 @@ pub enum Error { /// The Meilisearch server returned an invalid JSON for a request. #[error("Error parsing response JSON: {}", .0)] ParseError(#[from] serde_json::Error), + + /// An error occurred while parsing the fields of the response JSON. + #[error("Error parsing fields: {0}")] + ParseStringError(String), + /// A timeout happened while waiting for an update to complete. #[error("A task did not succeed in time.")] Timeout, From 8182012d724d42bb446fcc49bfc3857f6efbce97 Mon Sep 17 00:00:00 2001 From: postmeback Date: Mon, 8 Jul 2024 23:37:43 +0530 Subject: [PATCH 2/4] Changes as per suggestion and cargo fmt executed --- src/client.rs | 25 +++++++++++++++---------- src/errors.rs | 10 +++------- src/indexes.rs | 3 ++- src/request.rs | 4 ++-- 4 files changed, 22 insertions(+), 20 deletions(-) diff --git a/src/client.rs b/src/client.rs index a76951a0..190c82fd 100644 --- a/src/client.rs +++ b/src/client.rs @@ -1,3 +1,4 @@ +use serde::de::Error as SerdeError; use serde::{de::DeserializeOwned, Deserialize, Serialize}; use serde_json::{json, Value}; use std::{collections::HashMap, time::Duration}; @@ -81,32 +82,36 @@ impl Client { ) -> Result, Error> { let raw_indexes = value["results"] .as_array() - .ok_or_else(|| Error::ParseStringError("Missing or invalid 'results' field".to_string()))?; - + .ok_or_else(|| serde_json::Error::custom("Missing or invalid 'results' field")) + .map_err(Error::SerdeParseError)?; + let limit = value["limit"] .as_u64() - .ok_or_else(|| Error::ParseStringError("Missing or invalid 'limit' field".to_string()))? as u32; - + .ok_or_else(|| serde_json::Error::custom("Missing or invalid 'limit' field")) + .map_err(Error::SerdeParseError)? as u32; + let offset = value["offset"] .as_u64() - .ok_or_else(|| Error::ParseStringError("Missing or invalid 'offset' field".to_string()))? as u32; - + .ok_or_else(|| serde_json::Error::custom("Missing or invalid 'offset' field")) + .map_err(Error::SerdeParseError)? as u32; + let total = value["total"] .as_u64() - .ok_or_else(|| Error::ParseStringError("Missing or invalid 'total' field".to_string()))? as u32; - + .ok_or_else(|| serde_json::Error::custom("Missing or invalid 'total' field")) + .map_err(Error::SerdeParseError)? as u32; + let results = raw_indexes .iter() .map(|raw_index| Index::from_value(raw_index.clone(), self.clone())) .collect::>()?; - + let indexes_results = IndexesResults { limit, offset, total, results, }; - + Ok(indexes_results) } diff --git a/src/errors.rs b/src/errors.rs index 63a2da19..4a0e5cf6 100644 --- a/src/errors.rs +++ b/src/errors.rs @@ -15,13 +15,9 @@ pub enum Error { #[error(transparent)] MeilisearchCommunication(#[from] MeilisearchCommunicationError), /// The Meilisearch server returned an invalid JSON for a request. - #[error("Error parsing response JSON: {}", .0)] - ParseError(#[from] serde_json::Error), + #[error(transparent)] + SerdeParseError(#[from] serde_json::Error), - /// An error occurred while parsing the fields of the response JSON. - #[error("Error parsing fields: {0}")] - ParseStringError(String), - /// A timeout happened while waiting for an update to complete. #[error("A task did not succeed in time.")] Timeout, @@ -390,7 +386,7 @@ mod test { "age": 43, }"#; - let error = Error::ParseError(serde_json::from_str::(data).unwrap_err()); + let error = Error::SerdeParseError(serde_json::from_str::(data).unwrap_err()); assert_eq!( error.to_string(), "Error parsing response JSON: invalid type: map, expected a string at line 2 column 8" diff --git a/src/indexes.rs b/src/indexes.rs index 05c728f1..c0fb84b7 100644 --- a/src/indexes.rs +++ b/src/indexes.rs @@ -102,7 +102,8 @@ impl Index { primaryKey: Option, } - let i: IndexFromSerde = serde_json::from_value(raw_index).map_err(Error::ParseError)?; + let i: IndexFromSerde = + serde_json::from_value(raw_index).map_err(Error::SerdeParseError)?; Ok(Index { uid: i.uid, diff --git a/src/request.rs b/src/request.rs index 366e51b9..6c61ab9a 100644 --- a/src/request.rs +++ b/src/request.rs @@ -117,7 +117,7 @@ pub fn parse_response( } Err(e) => { error!("Request succeeded but failed to parse response"); - Err(Error::ParseError(e)) + Err(Error::SerdeParseError(e)) } }; } @@ -139,7 +139,7 @@ pub fn parse_response( }, )); } - Err(Error::ParseError(e)) + Err(Error::SerdeParseError(e)) } } } From 2831c60ced8fa2ebb708c127a613964d5f846c37 Mon Sep 17 00:00:00 2001 From: Arka Poddar Date: Tue, 30 Jul 2024 22:25:37 +0530 Subject: [PATCH 3/4] Review Comments Included Co-authored-by: Tamo --- src/errors.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/errors.rs b/src/errors.rs index 4a0e5cf6..89ebb8dd 100644 --- a/src/errors.rs +++ b/src/errors.rs @@ -15,8 +15,8 @@ pub enum Error { #[error(transparent)] MeilisearchCommunication(#[from] MeilisearchCommunicationError), /// The Meilisearch server returned an invalid JSON for a request. - #[error(transparent)] - SerdeParseError(#[from] serde_json::Error), + #[error("Error parsing response JSON: {}", .0)] + ParseError(#[from] serde_json::Error), /// A timeout happened while waiting for an update to complete. #[error("A task did not succeed in time.")] From 6092d2caef15da4715d0292ccf1a1314872c8c97 Mon Sep 17 00:00:00 2001 From: postmeback Date: Tue, 30 Jul 2024 22:38:45 +0530 Subject: [PATCH 4/4] Second Set Changes Done --- src/client.rs | 8 ++++---- src/errors.rs | 2 +- src/indexes.rs | 3 +-- src/request.rs | 4 ++-- 4 files changed, 8 insertions(+), 9 deletions(-) diff --git a/src/client.rs b/src/client.rs index 190c82fd..c7d2ebea 100644 --- a/src/client.rs +++ b/src/client.rs @@ -83,22 +83,22 @@ impl Client { let raw_indexes = value["results"] .as_array() .ok_or_else(|| serde_json::Error::custom("Missing or invalid 'results' field")) - .map_err(Error::SerdeParseError)?; + .map_err(Error::ParseError)?; let limit = value["limit"] .as_u64() .ok_or_else(|| serde_json::Error::custom("Missing or invalid 'limit' field")) - .map_err(Error::SerdeParseError)? as u32; + .map_err(Error::ParseError)? as u32; let offset = value["offset"] .as_u64() .ok_or_else(|| serde_json::Error::custom("Missing or invalid 'offset' field")) - .map_err(Error::SerdeParseError)? as u32; + .map_err(Error::ParseError)? as u32; let total = value["total"] .as_u64() .ok_or_else(|| serde_json::Error::custom("Missing or invalid 'total' field")) - .map_err(Error::SerdeParseError)? as u32; + .map_err(Error::ParseError)? as u32; let results = raw_indexes .iter() diff --git a/src/errors.rs b/src/errors.rs index 89ebb8dd..9e9c7eae 100644 --- a/src/errors.rs +++ b/src/errors.rs @@ -386,7 +386,7 @@ mod test { "age": 43, }"#; - let error = Error::SerdeParseError(serde_json::from_str::(data).unwrap_err()); + let error = Error::ParseError(serde_json::from_str::(data).unwrap_err()); assert_eq!( error.to_string(), "Error parsing response JSON: invalid type: map, expected a string at line 2 column 8" diff --git a/src/indexes.rs b/src/indexes.rs index c0fb84b7..05c728f1 100644 --- a/src/indexes.rs +++ b/src/indexes.rs @@ -102,8 +102,7 @@ impl Index { primaryKey: Option, } - let i: IndexFromSerde = - serde_json::from_value(raw_index).map_err(Error::SerdeParseError)?; + let i: IndexFromSerde = serde_json::from_value(raw_index).map_err(Error::ParseError)?; Ok(Index { uid: i.uid, diff --git a/src/request.rs b/src/request.rs index 6c61ab9a..366e51b9 100644 --- a/src/request.rs +++ b/src/request.rs @@ -117,7 +117,7 @@ pub fn parse_response( } Err(e) => { error!("Request succeeded but failed to parse response"); - Err(Error::SerdeParseError(e)) + Err(Error::ParseError(e)) } }; } @@ -139,7 +139,7 @@ pub fn parse_response( }, )); } - Err(Error::SerdeParseError(e)) + Err(Error::ParseError(e)) } } }