Skip to main content

xrpl_common_stdlib/objects/generated/
amendments.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_optional;
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::uint::Hash256;
17
18/// Trait providing access to fields specific to Amendments objects in any ledger.
19pub trait AmendmentsFields: LedgerObjectCommonFields {
20    /// Array of 256-bit amendment IDs for all currently enabled amendments. If omitted, there are
21    /// no enabled amendments.
22    /// Raw bytes; VECTOR256 is not yet typed in Rust.
23    fn amendments(&self) -> Result<Option<[u8; RAW_UNMAPPED_FIELD_SIZE]>> {
24        let mut buffer = [0u8; RAW_UNMAPPED_FIELD_SIZE];
25        let result_code = unsafe {
26            le_field(
27                self.get_slot_num(),
28                sfield::Amendments.into(),
29                buffer.as_mut_ptr(),
30                buffer.len(),
31            )
32        };
33        match_result_code_optional(result_code, || (result_code > 0).then_some(buffer))
34    }
35
36    /// The identifying hash of the transaction that most recently modified this entry.
37    fn previous_txn_id(&self) -> Result<Option<Hash256>> {
38        ledger_object::get_field_optional(self.get_slot_num(), sfield::PreviousTxnID)
39    }
40
41    /// The index of the ledger that contains the transaction that most recently modified this
42    /// entry.
43    fn previous_txn_lgr_seq(&self) -> Result<Option<u32>> {
44        ledger_object::get_field_optional(self.get_slot_num(), sfield::PreviousTxnLgrSeq)
45    }
46}
47
48/// Trait providing access to fields specific to the current Amendments object.
49pub trait CurrentAmendmentsFields: CurrentLedgerObjectCommonFields {
50    /// Array of 256-bit amendment IDs for all currently enabled amendments. If omitted, there are
51    /// no enabled amendments.
52    /// Raw bytes; VECTOR256 is not yet typed in Rust.
53    fn amendments(&self) -> Result<Option<[u8; RAW_UNMAPPED_FIELD_SIZE]>> {
54        let mut buffer = [0u8; RAW_UNMAPPED_FIELD_SIZE];
55        let result_code =
56            unsafe { home_le_field(sfield::Amendments.into(), buffer.as_mut_ptr(), buffer.len()) };
57        match_result_code_optional(result_code, || (result_code > 0).then_some(buffer))
58    }
59
60    /// The identifying hash of the transaction that most recently modified this entry.
61    fn previous_txn_id(&self) -> Result<Option<Hash256>> {
62        current_ledger_object::get_field_optional(sfield::PreviousTxnID)
63    }
64
65    /// The index of the ledger that contains the transaction that most recently modified this
66    /// entry.
67    fn previous_txn_lgr_seq(&self) -> Result<Option<u32>> {
68        current_ledger_object::get_field_optional(sfield::PreviousTxnLgrSeq)
69    }
70}
71
72#[derive(Debug, Clone, Copy, Eq, PartialEq)]
73pub struct Amendments {
74    pub(crate) slot_num: i32,
75}
76
77impl Amendments {
78    /// Binds this handle to a host-managed slot holding an Amendments ledger object.
79    pub fn new(slot_num: i32) -> Self {
80        Self { slot_num }
81    }
82}
83
84impl LedgerObjectCommonFields for Amendments {
85    fn get_slot_num(&self) -> i32 {
86        self.slot_num
87    }
88}
89
90impl AmendmentsFields for Amendments {}
91
92#[cfg(test)]
93mod tests {
94    use super::*;
95    use crate::host::host_bindings_trait::MockHostBindings;
96    use crate::host::setup_mock;
97    use crate::objects::test_utils::*;
98
99    #[test]
100    fn read_all_fields() {
101        let mut mock = MockHostBindings::new();
102        mock_all_fields_present(&mut mock);
103        let _guard = setup_mock(mock);
104
105        let obj = Amendments::new(0);
106
107        assert!(obj.amendments().is_ok());
108        assert!(obj.previous_txn_id().is_ok());
109        assert!(obj.previous_txn_lgr_seq().is_ok());
110    }
111
112    #[test]
113    fn optional_fields_none() {
114        let mut mock = MockHostBindings::new();
115        mock_all_fields_not_found(&mut mock);
116        let _guard = setup_mock(mock);
117
118        let obj = Amendments::new(0);
119
120        assert!(obj.previous_txn_id().unwrap().is_none());
121        assert!(obj.previous_txn_lgr_seq().unwrap().is_none());
122    }
123}