1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
//! Stellar Horizon SDK for Rust
//!
//! This Rust library provides a user-friendly interface to the Stellar Horizon API,
//! allowing developers to easily query and transact on the Stellar network. Centered
//! around the `HorizonClient`, the SDK abstracts the underlying HTTP request and response
//! mechanisms into a set of simple, high-level methods.
//!
//! The SDK is designed with a focus on developer experience, providing clear abstractions,
//! sensible defaults, and streamlined error handling.
//!
//! ## Status
//!
//! The SDK is under active development. It is functional but should be considered a
//! work-in-progress. Features may be added or changed, and the SDK may evolve before
//! stabilization.
//!
//! #### Supported endpoints:
//! ![80%](https://progress-bar.dev/80/?width=200)
//! * Accounts
//! * Assets
//! * Claimable balance
//! * Effects
//! * Fee stats
//! * Ledgers
//! * Liquidity pools
//! * Operations
//! * Offers
//! * Orderbook
//! * Trades
//!
//! #### Endpoints on the roadmap:
//! * Paths
//! * Payments
//! * Trade aggregations
//! * Transactions

//!
//! ## Example Usage
//!
//! The following example demonstrates how to use the `HorizonClient` to retrieve a list
//! of accounts with a specific signer:
//!
//! ```rust
//! use stellar_rs::horizon_client::HorizonClient;
//! use stellar_rs::accounts::prelude::{AccountsRequest, AccountsResponse};
//! use stellar_rs::models::{Request, Response};
//!
//! #[tokio::main]
//! async fn main() -> Result<(), Box<dyn std::error::Error>> {
//!     // Initialize the Horizon client with the testnet server
//!     let horizon_client = HorizonClient::new("https://horizon-testnet.stellar.org".to_string())?;
//!
//!     // Create a request to fetch accounts with a specific signer
//!     let accounts_request = AccountsRequest::new()
//!         .set_signer_filter("GDQJUTQYK2MQX2VGDR2FYWLIYAQIEGXTQVTFEMGH2BEWFG4BRUY4CKI7")?
//!         .set_limit(10)?;
//!
//!     // Perform the request using the Horizon client
//!     let accounts_response =
//!         horizon_client.get_account_list(&accounts_request)
//!         .await;
//!
//!     // Check for success and handle the response or error
//!     match accounts_response {
//!         Ok(response) => {
//!             // Process the response
//!         },
//!         Err(e) => {
//!             // Handle the error
//!         }
//!     }
//!
//!     Ok(())
//! }
//! ```
//!
//! This example initializes a `HorizonClient`, constructs an `AccountsRequest` to filter
//! accounts by signer, and calls `get_account_list` to retrieve the relevant data.
//! The result is then handled in a match expression, demonstrating the SDK's straightforward
//! error handling.
//!
//! Visit the documentation for `HorizonClient` and endpoint-specific request and response
//! types for more examples and detailed usage instructions.

use derive_getters::Getters;
use models::Order;
/// Provides `Request` and `Response` structs for retrieving accounts.
///
/// This module provides a set of specialized request and response structures designed for
/// interacting with the accounts-related endpoints of the Horizon server. These structures
/// facilitate the construction of requests to query account data and the interpretation of
/// the corresponding responses.
///
/// # Usage
///
/// This module is intended to be used in conjunction with the [`HorizonClient`](crate::horizon_client::HorizonClient)
/// for making specific account-related API calls to the Horizon server. The request
/// structures are designed to be passed to the client's methods, which handle the
/// communication with the server and return the corresponding response structures.
///
/// # Example
/// An example of retrieving a list of accounts, filtering by signer:
/// ```rust
/// # use stellar_rs::accounts::prelude::{AccountsRequest, AccountsResponse};
/// # use stellar_rs::models::Request;
/// # use stellar_rs::horizon_client::HorizonClient;
/// #
/// # async fn example() -> Result<(), Box<dyn std::error::Error>> {
/// # let base_url = "https://horizon-testnet.stellar.org".to_string();
/// # let horizon_client = HorizonClient::new(base_url)
/// #    .expect("Failed to create Horizon Client");
/// let request = AccountsRequest::new()
///     .set_signer_filter("GDQJUTQYK2MQX2VGDR2FYWLIYAQIEGXTQVTFEMGH2BEWFG4BRUY4CKI7").unwrap()
///     .set_limit(10).unwrap();
///
/// let response = horizon_client
///     .get_account_list(&request)
///     .await?;
/// # Ok({})
/// # }
///
pub mod accounts;

/// Provides `Request` and `Response` structs for retrieving assets.
///
/// This module provides the structures and functionalities necessary to interact with asset-related
/// endpoints of the Stellar Horizon API. It defines the request and response handlers for querying
/// information about assets on the Stellar network as described in the Stellar Horizon API documentation
/// on [Assets](https://developers.stellar.org/api/horizon/resources/assets). It is intended to be used in
/// conjunction with the is intended to be used in conjunction with the [`HorizonClient`](crate::horizon_client::HorizonClient)
/// struct.
///
/// # Example
///
/// The `assets` module simplifies the process of constructing queries about assets and interpreting the results. For example:
///
/// ```rust
/// # use stellar_rs::assets::prelude::*;
/// # use stellar_rs::models::Request;
/// # use stellar_rs::horizon_client::HorizonClient;
/// #
/// # async fn example() -> Result<(), Box<dyn std::error::Error>> {
/// # let base_url = "https://horizon-testnet.stellar.org".to_string();
/// # let horizon_client = HorizonClient::new(base_url)
/// #    .expect("Failed to create Horizon Client");
/// let request = AllAssetsRequest::new()
///     .set_asset_code("GDQJUTQYK2MQX2VGDR2FYWLIYAQIEGXTQVTFEMGH2BEWFG4BRUY4CKI7").unwrap();
///
/// let response = horizon_client
///     .get_all_assets(&request)
///     .await?;
/// # Ok({})
/// # }
/// ```
///
pub mod assets;

/// Provides `Request` and `Response` structs for retrieving claimable balances.
///
/// This module provides structures and functionalities related to claimable balances within the Stellar network.
/// Claimable balances are a feature of the Stellar network that allows for the creation of balances that are
/// claimable by a designated party. They are used to facilitate payments to accounts that may not yet exist
/// or to provide an assurance that funds can be claimed by the rightful recipient.
///
/// The module comprises request and response structs for both single and batched operations involving
/// claimable balances. These are designed to interface with the Horizon API's endpoints for creating,
/// retrieving, and claiming such balances.
///
/// # Usage
///
/// To utilize the functionalities for claimable balances, import the necessary structs from this module
/// and use them to interact with the Horizon API. The `HorizonClient` methods, such as `get_claimable_balances`
/// and `get_claimable_balance`, will typically return the response structs provided here.
///
/// # Example
/// ```rust
/// # use stellar_rs::claimable_balances::prelude::*;
/// # use stellar_rs::models::Request;
/// # use stellar_rs::horizon_client::HorizonClient;
/// #
/// # async fn example() -> Result<(), Box<dyn std::error::Error>> {
/// # let base_url = "https://horizon-testnet.stellar.org".to_string();
/// # let horizon_client = HorizonClient::new(base_url)
/// #    .expect("Failed to create Horizon Client");
/// let request = AllClaimableBalancesRequest::new();
///
/// let response = horizon_client
///     .get_all_claimable_balances(&request)
///     .await?;
///
/// // Process the response
/// # Ok({})
/// # }
/// ```
///
pub mod claimable_balances;

/// Client for calling the Stellar Horizon API
///
/// # Constructing a `HorizonClient`
/// A string containing the base URL for the Horizon API is required to contruct a client.
/// For example, to construct a client for the Horizon API testnet:
/// ```rust
/// use stellar_rs::horizon_client::HorizonClient;
///
/// let base_url = "https://horizon-testnet.stellar.org".to_string();
/// let horizon_client = HorizonClient::new(base_url)
///     .expect("Failed to create Horizon Client");;
/// ```
///
/// # Using the `HorizonClient`
/// The HorizonClient has a function that can be called for each endpoind provided
/// by the Horizon API. For example, it has a [`HorizonClient::get_account_list`](crate::horizon_client::HorizonClient::get_account_list)
/// function, which returns an async future that contains a result, as illustrated below:
/// ```rust
/// # use stellar_rs::assets::prelude::{AllAssetsRequest, AllAssetsResponse};
/// # use stellar_rs::models::Request;
/// # use stellar_rs::horizon_client::HorizonClient;
/// #
/// # async fn example() -> Result<(), Box<dyn std::error::Error>> {
/// # let base_url = "https://horizon-testnet.stellar.org".to_string();
/// # let horizon_client = HorizonClient::new(base_url)
/// #    .expect("Failed to create Horizon Client");;
/// let all_assets_request = AllAssetsRequest::new();
/// let accounts_response = horizon_client
///     .get_all_assets(&all_assets_request)
///     .await?;
/// # Ok({})
/// # }
/// ```
pub mod horizon_client;

/// Provides `Request` and `Response` structs for retrieving ledgers.
///
/// The `ledgers` module in the Stellar Horizon SDK includes structures and methods that facilitate
/// querying ledger data from the Horizon server.
///
/// # Usage
///
/// This module is used to construct requests for ledger-related data and to parse the responses
/// received from the Horizon server. It includes request and response structures for both
/// individual ledger queries and queries for a collection of ledgers.
///
/// # Example
///
/// To use this module, you can create an instance of a request struct, such as `SingleLedgerRequest`
/// or `AllLedgersRequest`, set any desired query parameters, and pass the request to the
/// `HorizonClient`. The client will then execute the request and return the corresponding
/// response struct, like `SingleLedgerResponse` or `AllLedgersResponse`.
///
/// ```rust
/// # use stellar_rs::horizon_client::HorizonClient;
/// # use stellar_rs::ledgers::prelude::*;
/// # use stellar_rs::models::Request;
/// # use stellar_rust_sdk_derive::Pagination;
/// # use stellar_rs::Paginatable;
///
/// # async fn example() -> Result<(), Box<dyn std::error::Error>> {
/// let horizon_client = HorizonClient::new("https://horizon-testnet.stellar.org".to_string())?;
///
/// // Example: Fetching a single ledger by sequence number
/// let single_ledger_request = SingleLedgerRequest::new().set_sequence(123456)?;
/// let ledger_response = horizon_client.get_single_ledger(&single_ledger_request).await?;
///
/// // Example: Fetching all ledgers
/// let all_ledgers_request = LedgersRequest::new().set_limit(10)?;
/// let ledgers_response = horizon_client.get_all_ledgers(&all_ledgers_request).await?;
///
/// // Process the responses...
/// # Ok(())
/// # }
/// ```
///
pub mod ledgers;

/// Provides `Request` and `Response` structs for retrieving effects.
///
/// The `effects` module in the Stellar Horizon SDK includes structures and methods that facilitate
/// querying effect data from the Horizon server.
///
/// # Usage
///
/// This module is used to construct requests for effect-related data and to parse the responses
/// received from the Horizon server. It includes request and response structures for both
/// individual effect queries and queries for a collection of effects.
///
/// # Example
///
/// To use this module, you can create an instance of a request struct, such as `SingleEffectRequest`
/// or `AllEffectsRequest`, set any desired query parameters, and pass the request to the
/// `HorizonClient`. The client will then execute the request and return the corresponding
/// response struct, like `SingleEffectResponse` or `AllEffectsResponse`.
///
/// ```rust
/// # use stellar_rs::horizon_client::HorizonClient;
/// # use stellar_rs::effects::prelude::*;
/// # use stellar_rs::models::Request;
/// # use stellar_rust_sdk_derive::Pagination;
/// # use crate::stellar_rs::Paginatable;
///
/// # async fn example() -> Result<(), Box<dyn std::error::Error>> {
/// let horizon_client = HorizonClient::new("https://horizon-testnet.stellar.org".to_string())?;
///
/// // Example: Fetching all effects
/// let all_effects_request = AllEffectsRequest::new().set_limit(10)?;
/// let effects_response = horizon_client.get_all_effects(&all_effects_request).await?;
///
/// // Process the responses...
/// # Ok(())
/// # }
/// ```
///
pub mod effects;

/// Provides `Request` and `Response` structs for retrieving fee stats.
///
/// The `fee_stats` module in the Stellar Horizon SDK includes structures and methods that facilitate
/// querying fee stats data from the Horizon server.
///
/// # Usage
///
/// This module is used to construct requests for fee stats-related data and to parse the responses
/// received from the Horizon server. It includes request and response structures for querying
/// fee stats data.
///
/// # Example
///
/// To use this module, you can create an instance of a request struct, such as `FeeStatsRequest`,
/// and pass the request to the `HorizonClient`. The client will then execute the request and
/// return the corresponding response struct, like `FeeStatsResponse`.
///
/// ```rust
/// use stellar_rs::horizon_client::HorizonClient;
/// use stellar_rs::fee_stats::prelude::*;
/// use stellar_rs::models::Request;
///
/// # async fn example() -> Result<(), Box<dyn std::error::Error>> {
/// let horizon_client = HorizonClient::new("https://horizon-testnet.stellar.org".to_string())?;
///
/// // Example: Fetching fee stats
/// let fee_stats_request = FeeStatsRequest::new();
/// let fee_stats_response = horizon_client.get_fee_stats(&fee_stats_request).await?;
///
/// // Process the response...
/// # Ok(())
/// # }
/// ```
///
pub mod fee_stats;

/// Provides `Request` and `Response` structs for retrieving liquidity pools.
///
/// The `liquidity_pools` module in the Stellar Horizon SDK includes structures and methods that facilitate
/// querying liquidity pool data from the Horizon server.
///
/// # Usage
///
/// This module is used to construct requests for liquidity pool related data and to parse the responses
/// received from the Horizon server. It includes request and response structures for querying
/// liquidity pool data.
///
/// # Example
///
/// To use this module, you can create an instance of a request struct, such as `SingleLiquidityPoolRequest`,
/// and pass the request to the `HorizonClient`. The client will then execute the request and
/// return the corresponding response struct, like `LiquidityPool`.
///
/// ```rust
/// # use stellar_rs::horizon_client::HorizonClient;
/// # use stellar_rs::liquidity_pools::prelude::*;
/// # use stellar_rs::models::Request;
///
/// # async fn example() -> Result<(), Box<dyn std::error::Error>> {
/// let horizon_client = HorizonClient::new("https://horizon-testnet.stellar.org".to_string())?;
///
/// // Example: Fetching fee stats
/// let single_lp_request = SingleLiquidityPoolRequest::new()
///     .set_liquidity_pool_id("000000006520216af66d20d63a58534d6cbdf28ba9f2a9c1e03f8d9a756bb7d988b29bca".to_string())
///     .unwrap();
/// let lp_response = horizon_client.get_single_liquidity_pool(&single_lp_request).await?;
///
/// // Process the response...
/// # Ok(())
/// # }
/// ```
///
pub mod liquidity_pools;

/// Provides `Request` and `Response` structs for retrieving offers.
///
/// This module provides a set of specialized request and response structures designed for
/// interacting with the offer-related endpoints of the Horizon server. These structures
/// facilitate the construction of requests to query offer data and the interpretation of
/// the corresponding responses.
///
/// # Usage
///
/// This module is intended to be used in conjunction with the [`HorizonClient`](crate::horizon_client::HorizonClient)
/// for making specific offer-related API calls to the Horizon server. The request
/// structures are designed to be passed to the client's methods, which handle the
/// communication with the server and return the corresponding response structures.
///
/// # Example
///
/// /// To use this module, you can create an instance of a request struct, such as
/// `SingleOfferRequest`, set any desired query parameters, and pass the request to the
/// `HorizonClient`. The client will then execute the request and return the corresponding
/// response struct, like `SingleOfferResponse`.
///
/// ```rust
/// use stellar_rs::horizon_client::HorizonClient;
/// use stellar_rs::offers::prelude::*;
/// use stellar_rs::models::Request;
///
/// # async fn example() -> Result<(), Box<dyn std::error::Error>> {
/// let horizon_client = HorizonClient::new("https://horizon-testnet.stellar.org".to_string())?;
///
/// // Example: Fetching all effects
/// let single_offer_request = SingleOfferRequest::new()
///     .set_offer_id("1".to_string())
///     .unwrap();
/// let single_offer_response = horizon_client.get_single_offer(&single_offer_request).await?;
///
/// // Process the responses...
/// # Ok(())
/// # }
/// ```
///
pub mod offers;

/// Provides `Request` and `Response` structs for retrieving operations.
///
/// The `operations` module in the Stellar Horizon SDK includes structures and methods that facilitate
/// querying operation data from the Horizon server.
///
/// # Usage
///
/// This module is used to construct requests for operation-related data and to parse the responses
/// received from the Horizon server. It includes request and response structures for both
/// individual operation queries and queries for a collection of operations.
///
/// # Example
///
/// To use this module, you can create an instance of a request struct, such as `SingleOperationRequest`
/// or `AllOperationsRequest`, set any desired query parameters, and pass the request to the
/// `HorizonClient`. The client will then execute the request and return the corresponding
/// response struct, like `SingleOperationResponse` or `AllOperationsResponse`.
///
/// ```rust
/// # use stellar_rs::horizon_client::HorizonClient;
/// # use stellar_rs::operations::prelude::*;
/// # use stellar_rs::models::Request;
/// # use stellar_rust_sdk_derive::Pagination;
/// # use stellar_rs::Paginatable;
///
/// # async fn example() -> Result<(), Box<dyn std::error::Error>> {
/// let horizon_client = HorizonClient::new("https://horizon-testnet.stellar.org".to_string())?;
///
/// // Example: Fetching all operations
/// let all_operations_request = AllOperationsRequest::new().set_limit(10)?;
/// let operations_response = horizon_client.get_all_operations(&all_operations_request).await?;
///
/// // Process the responses...
/// # Ok(())
/// # }
/// ```
///
pub mod operations;

/// Provides `Request` and `Response` structs for retrieving order book details.
/// 
/// The `order_book` module in the Stellar Horizon SDK includes structures and methods that facilitate
/// querying order book data from the Horizon server.
/// 
/// # Usage
/// 
/// This module is used to construct requests for order book-related data and to parse the responses
/// received from the Horizon server. It includes request and response structures for querying
/// order book details.
/// 
/// # Example
/// 
/// To use this module, you can create an instance of a request struct, such as `DetailsRequest`,
/// set any desired query parameters, and pass the request to the `HorizonClient`. The client will
/// then execute the request and return the corresponding response struct, like `DetailsResponse`.
/// 
/// ```rust
/// use stellar_rs::horizon_client::HorizonClient;
/// use stellar_rs::order_book::prelude::*;
/// use stellar_rs::models::Request;
/// 
/// # async fn example() -> Result<(), Box<dyn std::error::Error>> {
/// let horizon_client = HorizonClient::new("https://horizon-testnet.stellar.org".to_string())?;
/// 
/// // Example: Fetching order book details
/// let details_request = DetailsRequest::new()
///    .set_buying_asset(AssetType::Native)?
///   .set_selling_asset(AssetType::Alphanumeric4(Asset {
///      asset_code: "USDC".to_string(),
///     asset_issuer: "GBBD47IF6LWK7P7MDEVSCWR7DPUWV3NY3DTQEVFL4NAT4AQH3ZLLFLA5".to_string(),
/// }))?;
/// let details_response = horizon_client.get_order_book_details(&details_request).await?;
/// 
/// // Process the response...
/// # Ok(())
/// # }
/// ```
/// 
pub mod order_book;

/// Provides `Request` and `Response` structs for retrieving transactions.
///
/// This module provides a set of specialized request and response structures designed for
/// interacting with the transaction-related endpoints of the Horizon server. These structures
/// facilitate the construction of requests to query transaction data and the interpretation of
/// the corresponding responses.
///
/// # Usage
///
/// This module is intended to be used in conjunction with the [`HorizonClient`](crate::horizon_client::HorizonClient)
/// for making specific transaction-related API calls to the Horizon server. The request
/// structures are designed to be passed to the client's methods, which handle the
/// communication with the server and return the corresponding response structures.
///
/// # Example
///
/// /// To use this module, you can create an instance of a request struct, such as
/// `SingleTransactionRequest`, set any desired query parameters, and pass the request to the
/// `HorizonClient`. The client will then execute the request and return the corresponding
/// response struct, like `TransactionResponse`.
///
/// ```rust
/// use stellar_rs::horizon_client::HorizonClient;
/// use stellar_rs::transactions::prelude::*;
/// use stellar_rs::models::Request;
///
/// # async fn example() -> Result<(), Box<dyn std::error::Error>> {
/// let horizon_client = HorizonClient::new("https://horizon-testnet.stellar.org".to_string())?;
///
/// // Example: Fetching a transaction
/// let single_transaction_request = SingleTransactionRequest::new()
///     .set_transaction_hash("be0d59c8706e8fd525d2ab10910a55ec57323663858c65b330a3f93afb13ab0f".to_string())
///     .unwrap();
/// let single_transaction_response = horizon_client.get_single_transaction(&single_transaction_request).await?;
///
/// // Process the responses...
/// # Ok(())
/// # }
/// ```
///
pub mod transactions;

/// Provides `Request` and `Response` structs for retrieving trades.
///
/// This module provides a set of specialized request and response structures designed for
/// interacting with the trade-related endpoints of the Horizon server. These structures
/// facilitate the construction of requests to query trade data and the interpretation of
/// the corresponding responses.
///
/// # Usage
///
/// This module is intended to be used in conjunction with the [`HorizonClient`](crate::horizon_client::HorizonClient)
/// for making specific trade-related API calls to the Horizon server. The request
/// structures are designed to be passed to the client's methods, which handle the
/// communication with the server and return the corresponding response structures.
///
/// # Example
///
/// /// To use this module, you can create an instance of a request struct, such as
/// `AllTradesRequest`, set any desired query parameters, and pass the request to the
/// `HorizonClient`. The client will then execute the request and return the corresponding
/// response struct, like `AllTradesResponse`.
///
/// ```rust
/// use stellar_rs::horizon_client::HorizonClient;
/// use stellar_rs::trades::prelude::*;
/// use stellar_rs::models::Request;
///
/// # async fn example() -> Result<(), Box<dyn std::error::Error>> {
/// let horizon_client = HorizonClient::new("https://horizon-testnet.stellar.org".to_string())?;
///
/// // Example: Fetching all trades
/// let all_trades_request = AllTradesRequest::new();
///
/// let all_trades_response = horizon_client.get_all_trades(&all_trades_request).await?;
///
/// // Process the responses...
/// # Ok(())
/// # }
/// ```
///
pub mod trades;

/// Contains core data structures and traits.
///
/// This module is used by the Stellar Rust SDK to interact with the Horizon API.
/// It defines enums, traits, and functions that encapsulate the logic for
/// creating and processing HTTP requests and responses, as well as handling the
/// data involved in these operations.
///
/// The `models` module plays a critical role in abstracting the complexities
/// of the Horizon API, allowing developers to work with high-level Rust constructs
/// instead of raw HTTP requests and JSON responses.
pub mod models;

/// Extension trait for building query parameter strings from a vector of optional values.
///
/// This trait provides a method to construct a query string from a vector of optional
/// values (`Option<T>`). It is designed to be used for generating query parameters in
/// URL construction, where each parameter is only included if it has a value (`Some`).
///
/// # Usage
/// This trait is typically used internally in constructing URLs with query parameters
/// by implementors of the [`Request::get_query_parameters`](crate::models::Request::get_query_parameters)
/// method. It enables a convenient and efficient way to handle optional parameters in
/// a URL query string.
///
trait BuildQueryParametersExt<T> {
    /// Constructs a query string for an HTTP request from the object's properties.
    ///
    /// This method transforms the properties of the implementing object into a URL-encoded query
    /// string.
    ///
    fn build_query_parameters(self) -> String;
}

impl<T: ToString> BuildQueryParametersExt<Option<T>> for Vec<Option<T>> {
    /// # Implementation for `Vec<Option<T>>`
    /// Converts each property to a key-value pair, and concatenates pairs with '&'.
    /// Properties that are `None` are omitted from the string.
    ///
    /// ## Returns
    /// A `String` representing the query parameters of the HTTP request. If there
    /// are no parameters, or all properties are `None`, an empty string is returned.
    ///
    fn build_query_parameters(self) -> String {
        let params = self
            .into_iter()
            // Iterate over each element in the vector.
            .filter_map(|x|
                // Use filter_map to process each Option<T>.
                // If the element is Some, it's transformed to its string representation.
                // If the element is None, it's filtered out.
                x.map(|val| val.to_string()))
            // Collect the transformed values into a Vec<String>.
            .collect::<Vec<String>>()
            // Join the Vec<String> elements with '&' to create the query string.
            .join("&");

        // Check if the resulting params string is empty.
        match params.is_empty() {
            // If params is empty, return an empty string.
            true => "".to_string(),
            // If params is not empty, prepend a '?' to the params string.
            false => format!("?{}", params),
        }
    }
}

pub trait Paginatable {
    fn set_cursor(self, cursor: u32) -> Result<Self, String>
    where
        Self: Sized;
    fn set_limit(self, limit: u8) -> Result<Self, String>
    where
        Self: Sized;
    fn set_order(self, order: Order) -> Result<Self, String>
    where
        Self: Sized;
}