diff --git a/client/src/client.rs b/client/src/client.rs index 44a35d686dc..6e3b28f1196 100644 --- a/client/src/client.rs +++ b/client/src/client.rs @@ -387,8 +387,8 @@ impl QueryRequest { match self.request { iroha_data_model::query::QueryRequest::Query(query_with_params) => builder - .params(Vec::from(query_with_params.sorting().clone())) - .params(Vec::from(*query_with_params.pagination())) + .params(query_with_params.sorting().clone().into_query_parameters()) + .params(query_with_params.pagination().into_query_parameters()) .body(query_with_params.query().clone()), iroha_data_model::query::QueryRequest::Cursor(cursor) => { builder.params(Vec::from(cursor)) @@ -969,7 +969,7 @@ impl Client { retry_in: Duration, pagination: Pagination, ) -> Result> { - let pagination: Vec<_> = pagination.into(); + let pagination = pagination.into_query_parameters(); for _ in 0..retry_count { let response = DefaultRequestBuilder::new( HttpMethod::GET, diff --git a/data_model/src/query/pagination.rs b/data_model/src/query/pagination.rs index f93a1dadc25..a1a6c4ad1b2 100644 --- a/data_model/src/query/pagination.rs +++ b/data_model/src/query/pagination.rs @@ -38,16 +38,22 @@ pub struct Pagination { pub start: Option, } -impl From for Vec<(&'static str, NonZeroU64)> { - fn from(pagination: Pagination) -> Self { - match (pagination.start, pagination.limit) { +impl Pagination { + /// Converts self to iterator of tuples to be used in queries + /// + /// The length of the output iterator is not constant and it's in (0..3) + pub fn into_query_parameters( + self, + ) -> impl IntoIterator + Clone { + let result_vec = match (self.start, self.limit) { (Some(start), Some(limit)) => { vec![(PAGINATION_LIMIT, limit.into()), (PAGINATION_START, start)] } (Some(start), None) => vec![(PAGINATION_START, start)], (None, Some(limit)) => vec![(PAGINATION_LIMIT, limit.into())], (None, None) => Vec::new(), - } + }; + result_vec.into_iter() } } diff --git a/data_model/src/query/sorting.rs b/data_model/src/query/sorting.rs index 015aeb3f34d..d6bd3a1230f 100644 --- a/data_model/src/query/sorting.rs +++ b/data_model/src/query/sorting.rs @@ -38,13 +38,14 @@ impl Sorting { } } -impl From for Vec<(&'static str, Name)> { - fn from(sorting: Sorting) -> Self { - if let Some(key) = sorting.sort_by_metadata_key { - return vec![(SORT_BY_KEY, key)]; - } - - Vec::new() +impl Sorting { + /// Converts self to iterator of tuples to be used in queries + /// + /// The length of the output iterator is not constant and has either 0 or 1 value + pub fn into_query_parameters(self) -> impl IntoIterator + Clone { + self.sort_by_metadata_key + .map(|key| (SORT_BY_KEY, key)) + .into_iter() } }