Skip to content

Commit

Permalink
Use derive macro for pagination
Browse files Browse the repository at this point in the history
  • Loading branch information
LeonardTibben authored and tluijken committed Apr 19, 2024
1 parent ecf6c9e commit b9daa81
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 90 deletions.
50 changes: 5 additions & 45 deletions stellar_rust_sdk/src/operations/operations_for_ledger_request.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
use stellar_rust_sdk_derive::Pagination;
use crate::Paginatable;

use crate::{
models::{IncludeFailed, Order, Request},
BuildQueryParametersExt,
};

#[derive(Default)]
#[derive(Default, Pagination)]
pub struct OperationsForLedgerRequest {
/// The account ID for which to retrieve operations.
ledger_sequence: Option<String>,
Expand All @@ -29,50 +32,6 @@ impl OperationsForLedgerRequest {
OperationsForLedgerRequest::default()
}

/// Sets the cursor for pagination.
///
/// # Arguments
/// * `cursor` - A `u32` value pointing to a specific location in a collection of responses.
///
pub fn set_cursor(self, cursor: u32) -> Result<OperationsForLedgerRequest, String> {
if cursor < 1 {
return Err("cursor must be greater than or equal to 1".to_string());
}

Ok(OperationsForLedgerRequest {
cursor: Some(cursor),
..self
})
}

/// Sets the maximum number of records to return.
///
/// # Arguments
/// * `limit` - A `u8` value specifying the maximum number of records. Range: 1 to 200. Defaults to 10.
///
pub fn set_limit(self, limit: u8) -> Result<OperationsForLedgerRequest, String> {
if limit < 1 || limit > 200 {
return Err("limit must be between 1 and 200".to_string());
}

Ok(OperationsForLedgerRequest {
limit: Some(limit),
..self
})
}

/// Sets the order of the returned records.
///
/// # Arguments
/// * `order` - An [`Order`] enum value specifying the order (ascending or descending).
///
pub fn set_order(self, order: Order) -> OperationsForLedgerRequest {
OperationsForLedgerRequest {
order: Some(order),
..self
}
}

/// Sets whether to include failed operations in the response.
///
/// # Arguments
Expand Down Expand Up @@ -137,6 +96,7 @@ mod tests {
.set_limit(200)
.unwrap()
.set_order(Order::Desc)
.unwrap()
.set_include_failed(IncludeFailed::True)
.set_account_id("00000000".to_string());

Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
use stellar_rust_sdk_derive::Pagination;
use crate::Paginatable;

use crate::{
models::{IncludeFailed, Order, Request},
BuildQueryParametersExt,
};

#[derive(Default)]
#[derive(Default, Pagination)]
pub struct OperationsForLiquidityPoolRequest {
/// A unique identifier for the liquidity pool of the operation(s).
liquidity_pool_id: Option<String>,
Expand All @@ -26,50 +29,6 @@ impl OperationsForLiquidityPoolRequest {
OperationsForLiquidityPoolRequest::default()
}

/// Sets the cursor for pagination.
///
/// # Arguments
/// * `cursor` - A `u32` value pointing to a specific location in a collection of responses.
///
pub fn set_cursor(self, cursor: u32) -> Result<OperationsForLiquidityPoolRequest, String> {
if cursor < 1 {
return Err("cursor must be greater than or equal to 1".to_string());
}

Ok(OperationsForLiquidityPoolRequest {
cursor: Some(cursor),
..self
})
}

/// Sets the maximum number of records to return.
///
/// # Arguments
/// * `limit` - A `u8` value specifying the maximum number of records. Range: 1 to 200. Defaults to 10.
///
pub fn set_limit(self, limit: u8) -> Result<OperationsForLiquidityPoolRequest, String> {
if limit < 1 || limit > 200 {
return Err("limit must be between 1 and 200".to_string());
}

Ok(OperationsForLiquidityPoolRequest {
limit: Some(limit),
..self
})
}

/// Sets the order of the returned records.
///
/// # Arguments
/// * `order` - An [`Order`] enum value specifying the order (ascending or descending).
///
pub fn set_order(self, order: Order) -> OperationsForLiquidityPoolRequest {
OperationsForLiquidityPoolRequest {
order: Some(order),
..self
}
}

/// Sets whether to include failed operations in the response.
///
/// # Arguments
Expand Down Expand Up @@ -136,6 +95,7 @@ mod tests {
.set_limit(10)
.unwrap()
.set_order(Order::Desc)
.unwrap()
.set_include_failed(IncludeFailed::True);

assert_eq!(
Expand Down

0 comments on commit b9daa81

Please sign in to comment.