8b253aad30d36acc559c755cf3bfa21823f8163e
[ldk-c-bindings] / lightning-c-bindings / src / lightning / onion_message / packet.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 //! Structs and enums useful for constructing and reading an onion message packet.
10
11 use alloc::str::FromStr;
12 use core::ffi::c_void;
13 use core::convert::Infallible;
14 use bitcoin::hashes::Hash;
15 use crate::c_types::*;
16 #[cfg(feature="no-std")]
17 use alloc::{vec::Vec, boxed::Box};
18
19 /// The contents of an onion message. In the context of offers, this would be the invoice, invoice
20 /// request, or invoice error.
21 #[must_use]
22 #[repr(C)]
23 pub enum OnionMessageContents {
24         /// A message related to BOLT 12 Offers.
25         Offers(
26                 crate::lightning::onion_message::offers::OffersMessage),
27         /// A custom onion message specified by the user.
28         Custom(
29                 crate::lightning::onion_message::packet::CustomOnionMessageContents),
30 }
31 use lightning::onion_message::packet::OnionMessageContents as OnionMessageContentsImport;
32 pub(crate) type nativeOnionMessageContents = OnionMessageContentsImport<crate::lightning::onion_message::packet::CustomOnionMessageContents>;
33
34 impl OnionMessageContents {
35         #[allow(unused)]
36         pub(crate) fn into_native(self) -> nativeOnionMessageContents {
37                 match self {
38                         OnionMessageContents::Offers (mut a, ) => {
39                                 nativeOnionMessageContents::Offers (
40                                         a.into_native(),
41                                 )
42                         },
43                         OnionMessageContents::Custom (mut a, ) => {
44                                 nativeOnionMessageContents::Custom (
45                                         a,
46                                 )
47                         },
48                 }
49         }
50         #[allow(unused)]
51         pub(crate) fn native_into(native: nativeOnionMessageContents) -> Self {
52                 match native {
53                         nativeOnionMessageContents::Offers (mut a, ) => {
54                                 OnionMessageContents::Offers (
55                                         crate::lightning::onion_message::offers::OffersMessage::native_into(a),
56                                 )
57                         },
58                         nativeOnionMessageContents::Custom (mut a, ) => {
59                                 OnionMessageContents::Custom (
60                                         Into::into(a),
61                                 )
62                         },
63                 }
64         }
65 }
66 /// Frees any resources used by the OnionMessageContents
67 #[no_mangle]
68 pub extern "C" fn OnionMessageContents_free(this_ptr: OnionMessageContents) { }
69 #[no_mangle]
70 /// Utility method to constructs a new Offers-variant OnionMessageContents
71 pub extern "C" fn OnionMessageContents_offers(a: crate::lightning::onion_message::offers::OffersMessage) -> OnionMessageContents {
72         OnionMessageContents::Offers(a, )
73 }
74 #[no_mangle]
75 /// Utility method to constructs a new Custom-variant OnionMessageContents
76 pub extern "C" fn OnionMessageContents_custom(a: crate::lightning::onion_message::packet::CustomOnionMessageContents) -> OnionMessageContents {
77         OnionMessageContents::Custom(a, )
78 }
79 /// The contents of a custom onion message.
80 #[repr(C)]
81 pub struct CustomOnionMessageContents {
82         /// An opaque pointer which is passed to your function implementations as an argument.
83         /// This has no meaning in the LDK, and can be NULL or any other value.
84         pub this_arg: *mut c_void,
85         /// Returns the TLV type identifying the message contents. MUST be >= 64.
86         pub tlv_type: extern "C" fn (this_arg: *const c_void) -> u64,
87         /// Serialize the object into a byte array
88         pub write: extern "C" fn (this_arg: *const c_void) -> crate::c_types::derived::CVec_u8Z,
89         /// Frees any resources associated with this object given its this_arg pointer.
90         /// Does not need to free the outer struct containing function pointers and may be NULL is no resources need to be freed.
91         pub free: Option<extern "C" fn(this_arg: *mut c_void)>,
92 }
93 unsafe impl Send for CustomOnionMessageContents {}
94 unsafe impl Sync for CustomOnionMessageContents {}
95 #[no_mangle]
96 pub(crate) extern "C" fn CustomOnionMessageContents_clone_fields(orig: &CustomOnionMessageContents) -> CustomOnionMessageContents {
97         CustomOnionMessageContents {
98                 this_arg: orig.this_arg,
99                 tlv_type: Clone::clone(&orig.tlv_type),
100                 write: Clone::clone(&orig.write),
101                 free: Clone::clone(&orig.free),
102         }
103 }
104 impl lightning::util::ser::Writeable for CustomOnionMessageContents {
105         fn write<W: lightning::util::ser::Writer>(&self, w: &mut W) -> Result<(), crate::c_types::io::Error> {
106                 let vec = (self.write)(self.this_arg);
107                 w.write_all(vec.as_slice())
108         }
109 }
110
111 use lightning::onion_message::packet::CustomOnionMessageContents as rustCustomOnionMessageContents;
112 impl rustCustomOnionMessageContents for CustomOnionMessageContents {
113         fn tlv_type(&self) -> u64 {
114                 let mut ret = (self.tlv_type)(self.this_arg);
115                 ret
116         }
117 }
118
119 // We're essentially a pointer already, or at least a set of pointers, so allow us to be used
120 // directly as a Deref trait in higher-level structs:
121 impl core::ops::Deref for CustomOnionMessageContents {
122         type Target = Self;
123         fn deref(&self) -> &Self {
124                 self
125         }
126 }
127 /// Calls the free function if one is set
128 #[no_mangle]
129 pub extern "C" fn CustomOnionMessageContents_free(this_ptr: CustomOnionMessageContents) { }
130 impl Drop for CustomOnionMessageContents {
131         fn drop(&mut self) {
132                 if let Some(f) = self.free {
133                         f(self.this_arg);
134                 }
135         }
136 }