xrpl_common_stdlib/objects/generated/
bridge.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::amount::Amount;
18use crate::types::uint::Hash256;
19
20pub trait BridgeFields: LedgerObjectCommonFields {
22 fn account(&self) -> Result<AccountID> {
24 ledger_object::get_field(self.get_slot_num(), sfield::Account)
25 }
26
27 fn signature_reward(&self) -> Result<Amount> {
30 ledger_object::get_field(self.get_slot_num(), sfield::SignatureReward)
31 }
32
33 fn min_account_create_amount(&self) -> Result<Option<Amount>> {
37 ledger_object::get_field_optional(self.get_slot_num(), sfield::MinAccountCreateAmount)
38 }
39
40 fn xchain_bridge(&self) -> Result<[u8; RAW_UNMAPPED_FIELD_SIZE]> {
43 let mut buffer = [0u8; RAW_UNMAPPED_FIELD_SIZE];
44 let result_code = unsafe {
45 le_field(
46 self.get_slot_num(),
47 sfield::XChainBridge.into(),
48 buffer.as_mut_ptr(),
49 buffer.len(),
50 )
51 };
52 match_result_code(result_code, || buffer)
53 }
54
55 fn xchain_claim_id(&self) -> Result<u64> {
57 ledger_object::get_field(self.get_slot_num(), sfield::XChainClaimID)
58 }
59
60 fn xchain_account_create_count(&self) -> Result<u64> {
63 ledger_object::get_field(self.get_slot_num(), sfield::XChainAccountCreateCount)
64 }
65
66 fn xchain_account_claim_count(&self) -> Result<u64> {
74 ledger_object::get_field(self.get_slot_num(), sfield::XChainAccountClaimCount)
75 }
76
77 fn owner_node(&self) -> Result<u64> {
79 ledger_object::get_field(self.get_slot_num(), sfield::OwnerNode)
80 }
81
82 fn previous_txn_id(&self) -> Result<Hash256> {
84 ledger_object::get_field(self.get_slot_num(), sfield::PreviousTxnID)
85 }
86
87 fn previous_txn_lgr_seq(&self) -> Result<u32> {
89 ledger_object::get_field(self.get_slot_num(), sfield::PreviousTxnLgrSeq)
90 }
91}
92
93pub trait CurrentBridgeFields: CurrentLedgerObjectCommonFields {
95 fn account(&self) -> Result<AccountID> {
97 current_ledger_object::get_field(sfield::Account)
98 }
99
100 fn signature_reward(&self) -> Result<Amount> {
103 current_ledger_object::get_field(sfield::SignatureReward)
104 }
105
106 fn min_account_create_amount(&self) -> Result<Option<Amount>> {
110 current_ledger_object::get_field_optional(sfield::MinAccountCreateAmount)
111 }
112
113 fn xchain_bridge(&self) -> Result<[u8; RAW_UNMAPPED_FIELD_SIZE]> {
116 let mut buffer = [0u8; RAW_UNMAPPED_FIELD_SIZE];
117 let result_code = unsafe {
118 home_le_field(
119 sfield::XChainBridge.into(),
120 buffer.as_mut_ptr(),
121 buffer.len(),
122 )
123 };
124 match_result_code(result_code, || buffer)
125 }
126
127 fn xchain_claim_id(&self) -> Result<u64> {
129 current_ledger_object::get_field(sfield::XChainClaimID)
130 }
131
132 fn xchain_account_create_count(&self) -> Result<u64> {
135 current_ledger_object::get_field(sfield::XChainAccountCreateCount)
136 }
137
138 fn xchain_account_claim_count(&self) -> Result<u64> {
146 current_ledger_object::get_field(sfield::XChainAccountClaimCount)
147 }
148
149 fn owner_node(&self) -> Result<u64> {
151 current_ledger_object::get_field(sfield::OwnerNode)
152 }
153
154 fn previous_txn_id(&self) -> Result<Hash256> {
156 current_ledger_object::get_field(sfield::PreviousTxnID)
157 }
158
159 fn previous_txn_lgr_seq(&self) -> Result<u32> {
161 current_ledger_object::get_field(sfield::PreviousTxnLgrSeq)
162 }
163}
164
165#[derive(Debug, Clone, Copy, Eq, PartialEq)]
166pub struct Bridge {
167 pub(crate) slot_num: i32,
168}
169
170impl Bridge {
171 pub fn new(slot_num: i32) -> Self {
173 Self { slot_num }
174 }
175}
176
177impl LedgerObjectCommonFields for Bridge {
178 fn get_slot_num(&self) -> i32 {
179 self.slot_num
180 }
181}
182
183impl BridgeFields for Bridge {}
184
185#[cfg(test)]
186mod tests {
187 use super::*;
188 use crate::host::host_bindings_trait::MockHostBindings;
189 use crate::host::setup_mock;
190 use crate::objects::test_utils::*;
191
192 #[test]
193 fn read_all_fields() {
194 let mut mock = MockHostBindings::new();
195 mock_all_fields_present(&mut mock);
196 let _guard = setup_mock(mock);
197
198 let obj = Bridge::new(0);
199
200 assert!(obj.account().is_ok());
201 assert!(obj.signature_reward().is_ok());
202 assert!(obj.xchain_bridge().is_ok());
203 assert!(obj.xchain_claim_id().is_ok());
204 assert!(obj.xchain_account_create_count().is_ok());
205 assert!(obj.xchain_account_claim_count().is_ok());
206 assert!(obj.owner_node().is_ok());
207 assert!(obj.previous_txn_id().is_ok());
208 assert!(obj.previous_txn_lgr_seq().is_ok());
209 assert!(obj.min_account_create_amount().is_ok());
210 }
211}