Skip to content

Commit

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

Signed-off-by: VAmuzing <[email protected]>
  • Loading branch information
VAmuzing committed Oct 30, 2023
1 parent 8018332 commit 41fdd5c
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 14 deletions.
8 changes: 4 additions & 4 deletions client/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -389,8 +389,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 @@ -1079,15 +1079,15 @@ 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,
self.torii_url
.join(uri::PENDING_TRANSACTIONS)
.expect("Valid URI"),
)
.params(pagination.clone())
.params(&pagination)
.headers(self.headers.clone())
.build()?
.send()?;
Expand Down
9 changes: 6 additions & 3 deletions data_model/src/query/pagination.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,12 @@ 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 Vec of tuples to be used in queries
///
/// The length of the output Vec is not constant and it's in (0..3)
pub fn into_query_parameters(self) -> Vec<(&'static str, NonZeroU64)> {
match (self.start, self.limit) {
(Some(start), Some(limit)) => {
vec![(PAGINATION_LIMIT, limit.into()), (PAGINATION_START, start)]
}
Expand Down
14 changes: 7 additions & 7 deletions data_model/src/query/sorting.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,13 @@ 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 Vec of tuples to be used in queries
///
/// The length of the output Vec is not constant and it's in (0..3)
pub fn into_query_parameters(self) -> Vec<(&'static str, Name)> {
self.sort_by_metadata_key
.map_or(Vec::new(), |key| vec![(SORT_BY_KEY, key)])
}
}

Expand Down

0 comments on commit 41fdd5c

Please sign in to comment.