Add auto-generated bindings of `lightning_transaction_sync`
[ldk-c-bindings] / lightning-c-bindings / src / lightning / util / scid_utils.rs
1 // This file is Copyright its original authors, visible in version control
2 // history and in the source files from which this was generated.
3 //
4 // This file is licensed under the license available in the LICENSE or LICENSE.md
5 // file in the root of this repository or, if no such file exists, the same
6 // license as that which applies to the original source files from which this
7 // source was automatically generated.
8
9 //! Utilities for creating and parsing short channel ids.
10
11 use alloc::str::FromStr;
12 use alloc::string::String;
13 use core::ffi::c_void;
14 use core::convert::Infallible;
15 use bitcoin::hashes::Hash;
16 use crate::c_types::*;
17 #[cfg(feature="no-std")]
18 use alloc::{vec::Vec, boxed::Box};
19
20 /// Maximum block height that can be used in a `short_channel_id`. This
21 /// value is based on the 3-bytes available for block height.
22
23 #[no_mangle]
24 pub static MAX_SCID_BLOCK: u64 = lightning::util::scid_utils::MAX_SCID_BLOCK;
25 /// Maximum transaction index that can be used in a `short_channel_id`.
26 /// This value is based on the 3-bytes available for tx index.
27
28 #[no_mangle]
29 pub static MAX_SCID_TX_INDEX: u64 = lightning::util::scid_utils::MAX_SCID_TX_INDEX;
30 /// Maximum vout index that can be used in a `short_channel_id`. This
31 /// value is based on the 2-bytes available for the vout index.
32
33 #[no_mangle]
34 pub static MAX_SCID_VOUT_INDEX: u64 = lightning::util::scid_utils::MAX_SCID_VOUT_INDEX;
35 /// A `short_channel_id` construction error
36 #[derive(Clone)]
37 #[must_use]
38 #[repr(C)]
39 pub enum ShortChannelIdError {
40         /// Block height too high
41         BlockOverflow,
42         /// Tx index too high
43         TxIndexOverflow,
44         /// Vout index too high
45         VoutIndexOverflow,
46 }
47 use lightning::util::scid_utils::ShortChannelIdError as ShortChannelIdErrorImport;
48 pub(crate) type nativeShortChannelIdError = ShortChannelIdErrorImport;
49
50 impl ShortChannelIdError {
51         #[allow(unused)]
52         pub(crate) fn to_native(&self) -> nativeShortChannelIdError {
53                 match self {
54                         ShortChannelIdError::BlockOverflow => nativeShortChannelIdError::BlockOverflow,
55                         ShortChannelIdError::TxIndexOverflow => nativeShortChannelIdError::TxIndexOverflow,
56                         ShortChannelIdError::VoutIndexOverflow => nativeShortChannelIdError::VoutIndexOverflow,
57                 }
58         }
59         #[allow(unused)]
60         pub(crate) fn into_native(self) -> nativeShortChannelIdError {
61                 match self {
62                         ShortChannelIdError::BlockOverflow => nativeShortChannelIdError::BlockOverflow,
63                         ShortChannelIdError::TxIndexOverflow => nativeShortChannelIdError::TxIndexOverflow,
64                         ShortChannelIdError::VoutIndexOverflow => nativeShortChannelIdError::VoutIndexOverflow,
65                 }
66         }
67         #[allow(unused)]
68         pub(crate) fn from_native(native: &ShortChannelIdErrorImport) -> Self {
69                 let native = unsafe { &*(native as *const _ as *const c_void as *const nativeShortChannelIdError) };
70                 match native {
71                         nativeShortChannelIdError::BlockOverflow => ShortChannelIdError::BlockOverflow,
72                         nativeShortChannelIdError::TxIndexOverflow => ShortChannelIdError::TxIndexOverflow,
73                         nativeShortChannelIdError::VoutIndexOverflow => ShortChannelIdError::VoutIndexOverflow,
74                 }
75         }
76         #[allow(unused)]
77         pub(crate) fn native_into(native: nativeShortChannelIdError) -> Self {
78                 match native {
79                         nativeShortChannelIdError::BlockOverflow => ShortChannelIdError::BlockOverflow,
80                         nativeShortChannelIdError::TxIndexOverflow => ShortChannelIdError::TxIndexOverflow,
81                         nativeShortChannelIdError::VoutIndexOverflow => ShortChannelIdError::VoutIndexOverflow,
82                 }
83         }
84 }
85 /// Creates a copy of the ShortChannelIdError
86 #[no_mangle]
87 pub extern "C" fn ShortChannelIdError_clone(orig: &ShortChannelIdError) -> ShortChannelIdError {
88         orig.clone()
89 }
90 #[allow(unused)]
91 /// Used only if an object of this type is returned as a trait impl by a method
92 pub(crate) extern "C" fn ShortChannelIdError_clone_void(this_ptr: *const c_void) -> *mut c_void {
93         Box::into_raw(Box::new(unsafe { (*(this_ptr as *const ShortChannelIdError)).clone() })) as *mut c_void
94 }
95 #[allow(unused)]
96 /// Used only if an object of this type is returned as a trait impl by a method
97 pub(crate) extern "C" fn ShortChannelIdError_free_void(this_ptr: *mut c_void) {
98         let _ = unsafe { Box::from_raw(this_ptr as *mut ShortChannelIdError) };
99 }
100 #[no_mangle]
101 /// Utility method to constructs a new BlockOverflow-variant ShortChannelIdError
102 pub extern "C" fn ShortChannelIdError_block_overflow() -> ShortChannelIdError {
103         ShortChannelIdError::BlockOverflow}
104 #[no_mangle]
105 /// Utility method to constructs a new TxIndexOverflow-variant ShortChannelIdError
106 pub extern "C" fn ShortChannelIdError_tx_index_overflow() -> ShortChannelIdError {
107         ShortChannelIdError::TxIndexOverflow}
108 #[no_mangle]
109 /// Utility method to constructs a new VoutIndexOverflow-variant ShortChannelIdError
110 pub extern "C" fn ShortChannelIdError_vout_index_overflow() -> ShortChannelIdError {
111         ShortChannelIdError::VoutIndexOverflow}
112 /// Get a string which allows debug introspection of a ShortChannelIdError object
113 pub extern "C" fn ShortChannelIdError_debug_str_void(o: *const c_void) -> Str {
114         alloc::format!("{:?}", unsafe { o as *const crate::lightning::util::scid_utils::ShortChannelIdError }).into()}
115 /// Checks if two ShortChannelIdErrors contain equal inner contents.
116 /// This ignores pointers and is_owned flags and looks at the values in fields.
117 #[no_mangle]
118 pub extern "C" fn ShortChannelIdError_eq(a: &ShortChannelIdError, b: &ShortChannelIdError) -> bool {
119         if &a.to_native() == &b.to_native() { true } else { false }
120 }
121 /// Extracts the block height (most significant 3-bytes) from the `short_channel_id`
122 #[no_mangle]
123 pub extern "C" fn block_from_scid(mut short_channel_id: u64) -> u32 {
124         let mut ret = lightning::util::scid_utils::block_from_scid(short_channel_id);
125         ret
126 }
127
128 /// Extracts the tx index (bytes [2..4]) from the `short_channel_id`
129 #[no_mangle]
130 pub extern "C" fn tx_index_from_scid(mut short_channel_id: u64) -> u32 {
131         let mut ret = lightning::util::scid_utils::tx_index_from_scid(short_channel_id);
132         ret
133 }
134
135 /// Extracts the vout (bytes [0..2]) from the `short_channel_id`
136 #[no_mangle]
137 pub extern "C" fn vout_from_scid(mut short_channel_id: u64) -> u16 {
138         let mut ret = lightning::util::scid_utils::vout_from_scid(short_channel_id);
139         ret
140 }
141
142 /// Constructs a `short_channel_id` using the components pieces. Results in an error
143 /// if the block height, tx index, or vout index overflow the maximum sizes.
144 #[no_mangle]
145 pub extern "C" fn scid_from_parts(mut block: u64, mut tx_index: u64, mut vout_index: u64) -> crate::c_types::derived::CResult_u64ShortChannelIdErrorZ {
146         let mut ret = lightning::util::scid_utils::scid_from_parts(block, tx_index, vout_index);
147         let mut local_ret = match ret { Ok(mut o) => crate::c_types::CResultTempl::ok( { o }).into(), Err(mut e) => crate::c_types::CResultTempl::err( { crate::lightning::util::scid_utils::ShortChannelIdError::native_into(e) }).into() };
148         local_ret
149 }
150
151 mod fake_scid {
152
153 use alloc::str::FromStr;
154 use alloc::string::String;
155 use core::ffi::c_void;
156 use core::convert::Infallible;
157 use bitcoin::hashes::Hash;
158 use crate::c_types::*;
159 #[cfg(feature="no-std")]
160 use alloc::{vec::Vec, boxed::Box};
161
162 }