From ba57d95d66205599fef16735c772e979d6b0b1a4 Mon Sep 17 00:00:00 2001 From: Eline Jorritsma Date: Thu, 16 Nov 2023 14:13:32 +0100 Subject: [PATCH] Add builder pattern to single_ledger_request --- src/ledgers/single_ledger_request.rs | 49 ++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) diff --git a/src/ledgers/single_ledger_request.rs b/src/ledgers/single_ledger_request.rs index a2349de..10d41f1 100644 --- a/src/ledgers/single_ledger_request.rs +++ b/src/ledgers/single_ledger_request.rs @@ -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 { + sequence: S, +} + +impl SingleLedgerRequestBuilder { + pub fn new() -> Self { + SingleLedgerRequestBuilder::default() + } +} + +impl SingleLedgerRequestBuilder { + pub fn sequence( + self, + sequence: impl Into, + ) -> SingleLedgerRequestBuilder { + + SingleLedgerRequestBuilder { + sequence: Sequence(sequence.into()) + } + } +} + +impl SingleLedgerRequestBuilder { + pub fn build(self) -> Result { + Ok(SingleLedgerRequest { + sequence: self.sequence.0 + }) + } +} + + impl Request for SingleLedgerRequest { fn new() -> Self { Self { sequence: 0 } @@ -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"); + } } \ No newline at end of file