Skip to content

Commit

Permalink
Add documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
kevin-pease committed Aug 20, 2024
1 parent 56d840b commit 3124737
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 1 deletion.
1 change: 1 addition & 0 deletions stellar_rust_sdk/src/effects/all_effects_request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ use stellar_rust_sdk_derive::pagination;
#[pagination]
#[derive(Default)]
pub struct AllEffectsRequest {
// All fields are injected by the `pagination` macro.
}

impl AllEffectsRequest {
Expand Down
1 change: 1 addition & 0 deletions stellar_rust_sdk/src/ledgers/ledgers_request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ use stellar_rust_sdk_derive::pagination;
#[pagination]
#[derive(Default)]
pub struct LedgersRequest {
// All fields are injected by the `pagination` macro.
}

impl LedgersRequest {
Expand Down
1 change: 1 addition & 0 deletions stellar_rust_sdk/src/payments/all_payments_request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use stellar_rust_sdk_derive::pagination;
#[pagination]
#[derive(Default)]
pub struct AllPaymentsRequest {
// All fields are injected by the `pagination` macro.
}

impl AllPaymentsRequest {
Expand Down
31 changes: 30 additions & 1 deletion stellar_rust_sdk_derive/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,35 @@ use proc_macro::TokenStream;
use quote::quote;
use syn::{parse_macro_input, ItemStruct, Fields, Field};

/// The procedural attribute macro to add pagination functionality to request structs.
///
/// This macro automatically injects pagination-related fields and methods into a struct
/// to facilitate paginated API requests. Specifically, it adds three optional fields
/// and three methods:
///
/// - `cursor`: An `Option<u32>` field that represents the pagination cursor. The cursor
/// is used to track the current position in a paginated dataset. The `set_cursor` method
/// allows setting this field, with a validation that ensures the cursor is greater than
/// or equal to 1.
///
/// - `limit`: An `Option<u8>` field that specifies the maximum number of items to retrieve
/// in a single page. The `set_limit` method allows setting this field, ensuring that the
/// limit is within a valid range (between 1 and 200).
///
/// - `order`: An `Option<Order>` field that defines the sort order of the paginated results.
/// The `set_order` method allows setting this field without additional validation, as the
/// sort order is context-dependent.
///
/// # Usage
///
/// Apply the `#[pagination]` attribute to a struct to automatically add pagination
/// functionality.
///
#[proc_macro_attribute]
pub fn pagination(_attr: TokenStream, input: TokenStream) -> TokenStream {
let mut input = parse_macro_input!(input as ItemStruct);

// create req'd fields
// Create required fields to be added to the struct.
let cursor_field: Field = syn::parse_quote! {
pub cursor: Option<u32>
};
Expand All @@ -18,14 +42,19 @@ pub fn pagination(_attr: TokenStream, input: TokenStream) -> TokenStream {
pub order: Option<Order>
};

// Add the fields to the struct.
if let Fields::Named(ref mut fields) = input.fields {
fields.named.push(cursor_field);
fields.named.push(limit_field);
fields.named.push(order_field);
}

let struct_name = &input.ident;

// Split the generics into implementation, type, and where clause parts, so that the macro supports generic structs.
let (impl_generics, type_generics, where_clause) = input.generics.split_for_impl();

// Add methods to the struct.
let expanded = quote! {
#input
impl #impl_generics #struct_name #type_generics #where_clause {
Expand Down

0 comments on commit 3124737

Please sign in to comment.