xrpl_common_stdlib/objects/generated/
directory_node.rs1const 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
19pub trait DirectoryNodeFields: LedgerObjectCommonFields {
21 fn owner(&self) -> Result<Option<AccountID>> {
23 ledger_object::get_field_optional(self.get_slot_num(), sfield::Owner)
24 }
25
26 fn taker_pays_currency(&self) -> Result<Option<Hash160>> {
29 ledger_object::get_field_optional(self.get_slot_num(), sfield::TakerPaysCurrency)
30 }
31
32 fn taker_pays_issuer(&self) -> Result<Option<Hash160>> {
35 ledger_object::get_field_optional(self.get_slot_num(), sfield::TakerPaysIssuer)
36 }
37
38 fn taker_pays_mpt(&self) -> Result<Option<Hash192>> {
40 ledger_object::get_field_optional(self.get_slot_num(), sfield::TakerPaysMPT)
41 }
42
43 fn taker_gets_currency(&self) -> Result<Option<Hash160>> {
46 ledger_object::get_field_optional(self.get_slot_num(), sfield::TakerGetsCurrency)
47 }
48
49 fn taker_gets_issuer(&self) -> Result<Option<Hash160>> {
52 ledger_object::get_field_optional(self.get_slot_num(), sfield::TakerGetsIssuer)
53 }
54
55 fn taker_gets_mpt(&self) -> Result<Option<Hash192>> {
57 ledger_object::get_field_optional(self.get_slot_num(), sfield::TakerGetsMPT)
58 }
59
60 fn exchange_rate(&self) -> Result<Option<u64>> {
62 ledger_object::get_field_optional(self.get_slot_num(), sfield::ExchangeRate)
63 }
64
65 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 fn root_index(&self) -> Result<Hash256> {
82 ledger_object::get_field(self.get_slot_num(), sfield::RootIndex)
83 }
84
85 fn index_next(&self) -> Result<Option<u64>> {
88 ledger_object::get_field_optional(self.get_slot_num(), sfield::IndexNext)
89 }
90
91 fn index_previous(&self) -> Result<Option<u64>> {
94 ledger_object::get_field_optional(self.get_slot_num(), sfield::IndexPrevious)
95 }
96
97 fn nftoken_id(&self) -> Result<Option<Hash256>> {
99 ledger_object::get_field_optional(self.get_slot_num(), sfield::NFTokenID)
100 }
101
102 fn previous_txn_id(&self) -> Result<Option<Hash256>> {
104 ledger_object::get_field_optional(self.get_slot_num(), sfield::PreviousTxnID)
105 }
106
107 fn previous_txn_lgr_seq(&self) -> Result<Option<u32>> {
110 ledger_object::get_field_optional(self.get_slot_num(), sfield::PreviousTxnLgrSeq)
111 }
112
113 fn domain_id(&self) -> Result<Option<Hash256>> {
117 ledger_object::get_field_optional(self.get_slot_num(), sfield::DomainID)
118 }
119}
120
121pub trait CurrentDirectoryNodeFields: CurrentLedgerObjectCommonFields {
123 fn owner(&self) -> Result<Option<AccountID>> {
125 current_ledger_object::get_field_optional(sfield::Owner)
126 }
127
128 fn taker_pays_currency(&self) -> Result<Option<Hash160>> {
131 current_ledger_object::get_field_optional(sfield::TakerPaysCurrency)
132 }
133
134 fn taker_pays_issuer(&self) -> Result<Option<Hash160>> {
137 current_ledger_object::get_field_optional(sfield::TakerPaysIssuer)
138 }
139
140 fn taker_pays_mpt(&self) -> Result<Option<Hash192>> {
142 current_ledger_object::get_field_optional(sfield::TakerPaysMPT)
143 }
144
145 fn taker_gets_currency(&self) -> Result<Option<Hash160>> {
148 current_ledger_object::get_field_optional(sfield::TakerGetsCurrency)
149 }
150
151 fn taker_gets_issuer(&self) -> Result<Option<Hash160>> {
154 current_ledger_object::get_field_optional(sfield::TakerGetsIssuer)
155 }
156
157 fn taker_gets_mpt(&self) -> Result<Option<Hash192>> {
159 current_ledger_object::get_field_optional(sfield::TakerGetsMPT)
160 }
161
162 fn exchange_rate(&self) -> Result<Option<u64>> {
164 current_ledger_object::get_field_optional(sfield::ExchangeRate)
165 }
166
167 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 fn root_index(&self) -> Result<Hash256> {
178 current_ledger_object::get_field(sfield::RootIndex)
179 }
180
181 fn index_next(&self) -> Result<Option<u64>> {
184 current_ledger_object::get_field_optional(sfield::IndexNext)
185 }
186
187 fn index_previous(&self) -> Result<Option<u64>> {
190 current_ledger_object::get_field_optional(sfield::IndexPrevious)
191 }
192
193 fn nftoken_id(&self) -> Result<Option<Hash256>> {
195 current_ledger_object::get_field_optional(sfield::NFTokenID)
196 }
197
198 fn previous_txn_id(&self) -> Result<Option<Hash256>> {
200 current_ledger_object::get_field_optional(sfield::PreviousTxnID)
201 }
202
203 fn previous_txn_lgr_seq(&self) -> Result<Option<u32>> {
206 current_ledger_object::get_field_optional(sfield::PreviousTxnLgrSeq)
207 }
208
209 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 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}