Skip to main content

xrpl_common_stdlib/objects/generated/
directory_node.rs

1// GENERATED -- do not hand-edit. Run scripts/generate-ledger-objects.sh to regenerate.
2
3/// Placeholder buffer size for fields whose XRPL wire type has no genuine Rust
4/// mapping yet (VECTOR256, XCHAIN_BRIDGE, PATHSET, ...). Such getters return
5/// raw, unparsed bytes; see the summary at the top of `generated/mod.rs`.
6const RAW_UNMAPPED_FIELD_SIZE: usize = 512;
7
8use crate::host::Result;
9use crate::host::error_codes::match_result_code;
10use crate::host::home_le_field;
11use crate::host::le_field;
12use crate::objects::traits::CurrentLedgerObjectCommonFields;
13use crate::objects::traits::LedgerObjectCommonFields;
14use crate::objects::{current_ledger_object, ledger_object};
15use crate::sfield;
16use crate::types::account_id::AccountID;
17use crate::types::uint::{Hash160, Hash192, Hash256};
18
19/// Trait providing access to fields specific to DirectoryNode objects in any ledger.
20pub trait DirectoryNodeFields: LedgerObjectCommonFields {
21    /// (Owner directories only) The address of the account that owns the objects in this directory.
22    fn owner(&self) -> Result<Option<AccountID>> {
23        ledger_object::get_field_optional(self.get_slot_num(), sfield::Owner)
24    }
25
26    /// (Offer directories only) The currency code of the `TakerPays` amount from the offers in this
27    /// directory.
28    fn taker_pays_currency(&self) -> Result<Option<Hash160>> {
29        ledger_object::get_field_optional(self.get_slot_num(), sfield::TakerPaysCurrency)
30    }
31
32    /// (Offer directories only) The issuer of the `TakerPays` amount from the offers in this
33    /// directory.
34    fn taker_pays_issuer(&self) -> Result<Option<Hash160>> {
35        ledger_object::get_field_optional(self.get_slot_num(), sfield::TakerPaysIssuer)
36    }
37
38    /// The TakerPaysMPT field (Optional).
39    fn taker_pays_mpt(&self) -> Result<Option<Hash192>> {
40        ledger_object::get_field_optional(self.get_slot_num(), sfield::TakerPaysMPT)
41    }
42
43    /// (Offer directories only) The currency code of the `TakerGets` amount from the offers in this
44    /// directory.
45    fn taker_gets_currency(&self) -> Result<Option<Hash160>> {
46        ledger_object::get_field_optional(self.get_slot_num(), sfield::TakerGetsCurrency)
47    }
48
49    /// (Offer directories only) The issuer of the `TakerGets` amount from the offers in this
50    /// directory.
51    fn taker_gets_issuer(&self) -> Result<Option<Hash160>> {
52        ledger_object::get_field_optional(self.get_slot_num(), sfield::TakerGetsIssuer)
53    }
54
55    /// The TakerGetsMPT field (Optional).
56    fn taker_gets_mpt(&self) -> Result<Option<Hash192>> {
57        ledger_object::get_field_optional(self.get_slot_num(), sfield::TakerGetsMPT)
58    }
59
60    /// (Offer directories only) DEPRECATED. Do not use.
61    fn exchange_rate(&self) -> Result<Option<u64>> {
62        ledger_object::get_field_optional(self.get_slot_num(), sfield::ExchangeRate)
63    }
64
65    /// The contents of this directory: an array of IDs of other objects.
66    /// Raw bytes; VECTOR256 is not yet typed in Rust.
67    fn indexes(&self) -> Result<[u8; RAW_UNMAPPED_FIELD_SIZE]> {
68        let mut buffer = [0u8; RAW_UNMAPPED_FIELD_SIZE];
69        let result_code = unsafe {
70            le_field(
71                self.get_slot_num(),
72                sfield::Indexes.into(),
73                buffer.as_mut_ptr(),
74                buffer.len(),
75            )
76        };
77        match_result_code(result_code, || buffer)
78    }
79
80    /// The ID of root object for this directory.
81    fn root_index(&self) -> Result<Hash256> {
82        ledger_object::get_field(self.get_slot_num(), sfield::RootIndex)
83    }
84
85    /// If this directory consists of multiple pages, this ID links to the next object in the chain,
86    /// wrapping around at the end.
87    fn index_next(&self) -> Result<Option<u64>> {
88        ledger_object::get_field_optional(self.get_slot_num(), sfield::IndexNext)
89    }
90
91    /// If this directory consists of multiple pages, this ID links to the previous object in the
92    /// chain, wrapping around at the beginning.
93    fn index_previous(&self) -> Result<Option<u64>> {
94        ledger_object::get_field_optional(self.get_slot_num(), sfield::IndexPrevious)
95    }
96
97    /// (NFT offer directories only) ID of the NFT in a buy or sell offer.
98    fn nftoken_id(&self) -> Result<Option<Hash256>> {
99        ledger_object::get_field_optional(self.get_slot_num(), sfield::NFTokenID)
100    }
101
102    /// The identifying hash of the transaction that most recently modified this entry.
103    fn previous_txn_id(&self) -> Result<Option<Hash256>> {
104        ledger_object::get_field_optional(self.get_slot_num(), sfield::PreviousTxnID)
105    }
106
107    /// The index of the ledger that contains the transaction that most recently modified this
108    /// entry.
109    fn previous_txn_lgr_seq(&self) -> Result<Option<u32>> {
110        ledger_object::get_field_optional(self.get_slot_num(), sfield::PreviousTxnLgrSeq)
111    }
112
113    /// (Offer directories only) The ledger entry ID of a permissioned domain. If present, this
114    /// order book belongs to the corresponding Permissioned DEX. Otherwise, this order book is part
115    /// of the open DEX.
116    fn domain_id(&self) -> Result<Option<Hash256>> {
117        ledger_object::get_field_optional(self.get_slot_num(), sfield::DomainID)
118    }
119}
120
121/// Trait providing access to fields specific to the current DirectoryNode object.
122pub trait CurrentDirectoryNodeFields: CurrentLedgerObjectCommonFields {
123    /// (Owner directories only) The address of the account that owns the objects in this directory.
124    fn owner(&self) -> Result<Option<AccountID>> {
125        current_ledger_object::get_field_optional(sfield::Owner)
126    }
127
128    /// (Offer directories only) The currency code of the `TakerPays` amount from the offers in this
129    /// directory.
130    fn taker_pays_currency(&self) -> Result<Option<Hash160>> {
131        current_ledger_object::get_field_optional(sfield::TakerPaysCurrency)
132    }
133
134    /// (Offer directories only) The issuer of the `TakerPays` amount from the offers in this
135    /// directory.
136    fn taker_pays_issuer(&self) -> Result<Option<Hash160>> {
137        current_ledger_object::get_field_optional(sfield::TakerPaysIssuer)
138    }
139
140    /// The TakerPaysMPT field (Optional).
141    fn taker_pays_mpt(&self) -> Result<Option<Hash192>> {
142        current_ledger_object::get_field_optional(sfield::TakerPaysMPT)
143    }
144
145    /// (Offer directories only) The currency code of the `TakerGets` amount from the offers in this
146    /// directory.
147    fn taker_gets_currency(&self) -> Result<Option<Hash160>> {
148        current_ledger_object::get_field_optional(sfield::TakerGetsCurrency)
149    }
150
151    /// (Offer directories only) The issuer of the `TakerGets` amount from the offers in this
152    /// directory.
153    fn taker_gets_issuer(&self) -> Result<Option<Hash160>> {
154        current_ledger_object::get_field_optional(sfield::TakerGetsIssuer)
155    }
156
157    /// The TakerGetsMPT field (Optional).
158    fn taker_gets_mpt(&self) -> Result<Option<Hash192>> {
159        current_ledger_object::get_field_optional(sfield::TakerGetsMPT)
160    }
161
162    /// (Offer directories only) DEPRECATED. Do not use.
163    fn exchange_rate(&self) -> Result<Option<u64>> {
164        current_ledger_object::get_field_optional(sfield::ExchangeRate)
165    }
166
167    /// The contents of this directory: an array of IDs of other objects.
168    /// Raw bytes; VECTOR256 is not yet typed in Rust.
169    fn indexes(&self) -> Result<[u8; RAW_UNMAPPED_FIELD_SIZE]> {
170        let mut buffer = [0u8; RAW_UNMAPPED_FIELD_SIZE];
171        let result_code =
172            unsafe { home_le_field(sfield::Indexes.into(), buffer.as_mut_ptr(), buffer.len()) };
173        match_result_code(result_code, || buffer)
174    }
175
176    /// The ID of root object for this directory.
177    fn root_index(&self) -> Result<Hash256> {
178        current_ledger_object::get_field(sfield::RootIndex)
179    }
180
181    /// If this directory consists of multiple pages, this ID links to the next object in the chain,
182    /// wrapping around at the end.
183    fn index_next(&self) -> Result<Option<u64>> {
184        current_ledger_object::get_field_optional(sfield::IndexNext)
185    }
186
187    /// If this directory consists of multiple pages, this ID links to the previous object in the
188    /// chain, wrapping around at the beginning.
189    fn index_previous(&self) -> Result<Option<u64>> {
190        current_ledger_object::get_field_optional(sfield::IndexPrevious)
191    }
192
193    /// (NFT offer directories only) ID of the NFT in a buy or sell offer.
194    fn nftoken_id(&self) -> Result<Option<Hash256>> {
195        current_ledger_object::get_field_optional(sfield::NFTokenID)
196    }
197
198    /// The identifying hash of the transaction that most recently modified this entry.
199    fn previous_txn_id(&self) -> Result<Option<Hash256>> {
200        current_ledger_object::get_field_optional(sfield::PreviousTxnID)
201    }
202
203    /// The index of the ledger that contains the transaction that most recently modified this
204    /// entry.
205    fn previous_txn_lgr_seq(&self) -> Result<Option<u32>> {
206        current_ledger_object::get_field_optional(sfield::PreviousTxnLgrSeq)
207    }
208
209    /// (Offer directories only) The ledger entry ID of a permissioned domain. If present, this
210    /// order book belongs to the corresponding Permissioned DEX. Otherwise, this order book is part
211    /// of the open DEX.
212    fn domain_id(&self) -> Result<Option<Hash256>> {
213        current_ledger_object::get_field_optional(sfield::DomainID)
214    }
215}
216
217#[derive(Debug, Clone, Copy, Eq, PartialEq)]
218pub struct DirectoryNode {
219    pub(crate) slot_num: i32,
220}
221
222impl DirectoryNode {
223    /// Binds this handle to a host-managed slot holding a DirectoryNode ledger object.
224    pub fn new(slot_num: i32) -> Self {
225        Self { slot_num }
226    }
227}
228
229impl LedgerObjectCommonFields for DirectoryNode {
230    fn get_slot_num(&self) -> i32 {
231        self.slot_num
232    }
233}
234
235impl DirectoryNodeFields for DirectoryNode {}
236
237#[cfg(test)]
238mod tests {
239    use super::*;
240    use crate::host::host_bindings_trait::MockHostBindings;
241    use crate::host::setup_mock;
242    use crate::objects::test_utils::*;
243
244    #[test]
245    fn read_all_fields() {
246        let mut mock = MockHostBindings::new();
247        mock_all_fields_present(&mut mock);
248        let _guard = setup_mock(mock);
249
250        let obj = DirectoryNode::new(0);
251
252        assert!(obj.indexes().is_ok());
253        assert!(obj.root_index().is_ok());
254        assert!(obj.owner().is_ok());
255        assert!(obj.taker_pays_currency().is_ok());
256        assert!(obj.taker_pays_issuer().is_ok());
257        assert!(obj.taker_pays_mpt().is_ok());
258        assert!(obj.taker_gets_currency().is_ok());
259        assert!(obj.taker_gets_issuer().is_ok());
260        assert!(obj.taker_gets_mpt().is_ok());
261        assert!(obj.exchange_rate().is_ok());
262        assert!(obj.index_next().is_ok());
263        assert!(obj.index_previous().is_ok());
264        assert!(obj.nftoken_id().is_ok());
265        assert!(obj.previous_txn_id().is_ok());
266        assert!(obj.previous_txn_lgr_seq().is_ok());
267        assert!(obj.domain_id().is_ok());
268    }
269
270    #[test]
271    fn optional_fields_none() {
272        let mut mock = MockHostBindings::new();
273        mock_all_fields_not_found(&mut mock);
274        let _guard = setup_mock(mock);
275
276        let obj = DirectoryNode::new(0);
277
278        assert!(obj.owner().unwrap().is_none());
279        assert!(obj.taker_pays_currency().unwrap().is_none());
280        assert!(obj.taker_pays_issuer().unwrap().is_none());
281        assert!(obj.taker_pays_mpt().unwrap().is_none());
282        assert!(obj.taker_gets_currency().unwrap().is_none());
283        assert!(obj.taker_gets_issuer().unwrap().is_none());
284        assert!(obj.taker_gets_mpt().unwrap().is_none());
285        assert!(obj.exchange_rate().unwrap().is_none());
286        assert!(obj.index_next().unwrap().is_none());
287        assert!(obj.index_previous().unwrap().is_none());
288        assert!(obj.nftoken_id().unwrap().is_none());
289        assert!(obj.previous_txn_id().unwrap().is_none());
290        assert!(obj.previous_txn_lgr_seq().unwrap().is_none());
291        assert!(obj.domain_id().unwrap().is_none());
292    }
293}