Skip to content

Commit

Permalink
[refactor] #3740: Replace impl From<Pagination> for Vec<...> with a m…
Browse files Browse the repository at this point in the history
…ethod (#4023)

Signed-off-by: VAmuzing <[email protected]>
  • Loading branch information
VAmuzing authored Nov 8, 2023
1 parent 5578aaa commit 3fdddf4
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 14 deletions.
6 changes: 3 additions & 3 deletions client/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down Expand Up @@ -969,7 +969,7 @@ impl Client {
retry_in: Duration,
pagination: Pagination,
) -> Result<Option<SignedTransaction>> {
let pagination: Vec<_> = pagination.into();
let pagination = pagination.into_query_parameters();
for _ in 0..retry_count {
let response = DefaultRequestBuilder::new(
HttpMethod::GET,
Expand Down
14 changes: 10 additions & 4 deletions data_model/src/query/pagination.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,16 +38,22 @@ pub struct Pagination {
pub start: Option<NonZeroU64>,
}

impl From<Pagination> 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<Item = (&'static str, NonZeroU64)> + 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()
}
}

Expand Down
15 changes: 8 additions & 7 deletions data_model/src/query/sorting.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,14 @@ impl Sorting {
}
}

impl From<Sorting> 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<Item = (&'static str, Name)> + Clone {
self.sort_by_metadata_key
.map(|key| (SORT_BY_KEY, key))
.into_iter()
}
}

Expand Down

0 comments on commit 3fdddf4

Please sign in to comment.