Skip to main content

xrpl/models/requests/
ledger_data.rs

1use alloc::borrow::Cow;
2use serde::{Deserialize, Serialize};
3use serde_with::skip_serializing_none;
4
5use crate::models::{requests::RequestMethod, Model};
6
7use super::{CommonFields, LedgerIndex, LookupByLedgerRequest, Marker, Request};
8
9/// The ledger_data method retrieves contents of the specified
10/// ledger. You can iterate through several calls to retrieve
11/// the entire contents of a single ledger version.
12///
13/// See Ledger Data:
14/// `<https://xrpl.org/ledger_data.html>`
15#[skip_serializing_none]
16#[derive(Debug, Serialize, Deserialize, PartialEq, Eq, Clone)]
17pub struct LedgerData<'a> {
18    /// The common fields shared by all requests.
19    #[serde(flatten)]
20    pub common_fields: CommonFields<'a>,
21    /// If set to true, return ledger objects as hashed hex
22    /// strings instead of JSON.
23    pub binary: Option<bool>,
24    /// The unique identifier of a ledger.
25    #[serde(flatten)]
26    pub ledger_lookup: Option<LookupByLedgerRequest<'a>>,
27    /// Limit the number of ledger objects to retrieve.
28    /// The server is not required to honor this value.
29    pub limit: Option<u16>,
30    /// Value from a previous paginated response.
31    /// Resume retrieving data where that response left off.
32    pub marker: Option<Marker<'a>>,
33}
34
35impl<'a> Model for LedgerData<'a> {}
36
37impl<'a> Request<'a> for LedgerData<'a> {
38    fn get_common_fields(&self) -> &CommonFields<'a> {
39        &self.common_fields
40    }
41
42    fn get_common_fields_mut(&mut self) -> &mut CommonFields<'a> {
43        &mut self.common_fields
44    }
45}
46
47impl<'a> LedgerData<'a> {
48    pub fn new(
49        id: Option<Cow<'a, str>>,
50        binary: Option<bool>,
51        ledger_hash: Option<Cow<'a, str>>,
52        ledger_index: Option<LedgerIndex<'a>>,
53        limit: Option<u16>,
54        marker: Option<Marker<'a>>,
55    ) -> Self {
56        Self {
57            common_fields: CommonFields {
58                command: RequestMethod::LedgerData,
59                id,
60            },
61            ledger_lookup: Some(LookupByLedgerRequest {
62                ledger_hash,
63                ledger_index,
64            }),
65            binary,
66            limit,
67            marker,
68        }
69    }
70}
71
72#[cfg(test)]
73mod tests {
74    use super::*;
75
76    #[test]
77    fn test_serde_round_trip() {
78        let req = LedgerData::new(
79            Some("ld-1".into()),
80            Some(true),
81            None,
82            Some(LedgerIndex::Int(42)),
83            Some(75),
84            Some(Marker::Int(7)),
85        );
86        let serialized = serde_json::to_string(&req).unwrap();
87        let deserialized: LedgerData = serde_json::from_str(&serialized).unwrap();
88        assert_eq!(req, deserialized);
89        assert!(serialized.contains("\"command\":\"ledger_data\""));
90        assert!(serialized.contains("\"binary\":true"));
91    }
92}