Skip to content

Commit

Permalink
Add builder pattern to single_ledger_request
Browse files Browse the repository at this point in the history
  • Loading branch information
Eline Jorritsma committed Nov 16, 2023
1 parent 2b43519 commit ba57d95
Showing 1 changed file with 49 additions and 0 deletions.
49 changes: 49 additions & 0 deletions src/ledgers/single_ledger_request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,45 @@ pub struct SingleLedgerRequest {
sequence: u32,
}

// region: --- States
#[derive(Default, Clone)]
pub struct Sequence(u32);
#[derive(Default, Clone)]
pub struct NoSequence;
// endregion: --- States

#[derive(Default, Clone)]
pub struct SingleLedgerRequestBuilder<S> {
sequence: S,
}

impl SingleLedgerRequestBuilder<NoSequence> {
pub fn new() -> Self {
SingleLedgerRequestBuilder::default()
}
}

impl<S> SingleLedgerRequestBuilder<S> {
pub fn sequence(
self,
sequence: impl Into<u32>,
) -> SingleLedgerRequestBuilder<Sequence> {

SingleLedgerRequestBuilder {
sequence: Sequence(sequence.into())
}
}
}

impl SingleLedgerRequestBuilder<Sequence> {
pub fn build(self) -> Result<SingleLedgerRequest, String> {
Ok(SingleLedgerRequest {
sequence: self.sequence.0
})
}
}


impl Request for SingleLedgerRequest {
fn new() -> Self {
Self { sequence: 0 }
Expand Down Expand Up @@ -62,4 +101,14 @@ mod tests {

assert_eq!(request.get_path(), "/ledgers");
}

#[test]
fn test_ledgers_request_builder() {
let req_builder = SingleLedgerRequestBuilder::new()
.sequence(2 as u32);

let req = req_builder.clone().build().unwrap();

assert_eq!(req.get_path(), "/ledgers");
}
}

0 comments on commit ba57d95

Please sign in to comment.