a39f35e761ba878853bef49e26a4026e0e4815cb
[ldk-c-bindings] / lightning-c-bindings / src / lightning / ln / wire.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 //! Wire encoding/decoding for Lightning messages according to [BOLT #1], and for
10 //! custom message through the [`CustomMessageReader`] trait.
11 //!
12 //! [BOLT #1]: https://github.com/lightning/bolts/blob/master/01-messaging.md
13
14 use alloc::str::FromStr;
15 use core::ffi::c_void;
16 use core::convert::Infallible;
17 use bitcoin::hashes::Hash;
18 use crate::c_types::*;
19 #[cfg(feature="no-std")]
20 use alloc::{vec::Vec, boxed::Box};
21
22 /// Trait to be implemented by custom message (unrelated to the channel/gossip LN layers)
23 /// decoders.
24 #[repr(C)]
25 pub struct CustomMessageReader {
26         /// An opaque pointer which is passed to your function implementations as an argument.
27         /// This has no meaning in the LDK, and can be NULL or any other value.
28         pub this_arg: *mut c_void,
29         /// Decodes a custom message to `CustomMessageType`. If the given message type is known to the
30         /// implementation and the message could be decoded, must return `Ok(Some(message))`. If the
31         /// message type is unknown to the implementation, must return `Ok(None)`. If a decoding error
32         /// occur, must return `Err(DecodeError::X)` where `X` details the encountered error.
33         pub read: extern "C" fn (this_arg: *const c_void, message_type: u16, buffer: crate::c_types::u8slice) -> crate::c_types::derived::CResult_COption_TypeZDecodeErrorZ,
34         /// Frees any resources associated with this object given its this_arg pointer.
35         /// Does not need to free the outer struct containing function pointers and may be NULL is no resources need to be freed.
36         pub free: Option<extern "C" fn(this_arg: *mut c_void)>,
37 }
38 unsafe impl Send for CustomMessageReader {}
39 unsafe impl Sync for CustomMessageReader {}
40 #[no_mangle]
41 pub(crate) extern "C" fn CustomMessageReader_clone_fields(orig: &CustomMessageReader) -> CustomMessageReader {
42         CustomMessageReader {
43                 this_arg: orig.this_arg,
44                 read: Clone::clone(&orig.read),
45                 free: Clone::clone(&orig.free),
46         }
47 }
48
49 use lightning::ln::wire::CustomMessageReader as rustCustomMessageReader;
50 impl rustCustomMessageReader for CustomMessageReader {
51         type CustomMessage = crate::lightning::ln::wire::Type;
52         fn read<R:crate::c_types::io::Read>(&self, mut message_type: u16, mut buffer: &mut R) -> Result<Option<crate::lightning::ln::wire::Type>, lightning::ln::msgs::DecodeError> {
53                 let mut ret = (self.read)(self.this_arg, message_type, crate::c_types::u8slice::from_vec(&crate::c_types::reader_to_vec(buffer)));
54                 let mut local_ret = match ret.result_ok { true => Ok( { let mut local_ret_0 = { /*(*unsafe { Box::from_raw(<*mut _>::take_ptr(&mut ret.contents.result)) })*/ let ret_0_opt = (*unsafe { Box::from_raw(<*mut _>::take_ptr(&mut ret.contents.result)) }); if ret_0_opt.is_none() { None } else { Some({ { { ret_0_opt.take() } }})} }; local_ret_0 }), false => Err( { (*unsafe { Box::from_raw(<*mut _>::take_ptr(&mut ret.contents.err)) }).into_native() })};
55                 local_ret
56         }
57 }
58
59 // We're essentially a pointer already, or at least a set of pointers, so allow us to be used
60 // directly as a Deref trait in higher-level structs:
61 impl core::ops::Deref for CustomMessageReader {
62         type Target = Self;
63         fn deref(&self) -> &Self {
64                 self
65         }
66 }
67 /// Calls the free function if one is set
68 #[no_mangle]
69 pub extern "C" fn CustomMessageReader_free(this_ptr: CustomMessageReader) { }
70 impl Drop for CustomMessageReader {
71         fn drop(&mut self) {
72                 if let Some(f) = self.free {
73                         f(self.this_arg);
74                 }
75         }
76 }
77 mod encode {
78
79 use alloc::str::FromStr;
80 use core::ffi::c_void;
81 use core::convert::Infallible;
82 use bitcoin::hashes::Hash;
83 use crate::c_types::*;
84 #[cfg(feature="no-std")]
85 use alloc::{vec::Vec, boxed::Box};
86
87 }
88 /// Defines a type identifier for sending messages over the wire.
89 ///
90 /// Messages implementing this trait specify a type and must be [`Writeable`].
91 #[repr(C)]
92 pub struct Type {
93         /// An opaque pointer which is passed to your function implementations as an argument.
94         /// This has no meaning in the LDK, and can be NULL or any other value.
95         pub this_arg: *mut c_void,
96         /// Returns the type identifying the message payload.
97         pub type_id: extern "C" fn (this_arg: *const c_void) -> u16,
98         /// Return a human-readable "debug" string describing this object
99         pub debug_str: extern "C" fn (this_arg: *const c_void) -> crate::c_types::Str,
100         /// Serialize the object into a byte array
101         pub write: extern "C" fn (this_arg: *const c_void) -> crate::c_types::derived::CVec_u8Z,
102         /// Frees any resources associated with this object given its this_arg pointer.
103         /// Does not need to free the outer struct containing function pointers and may be NULL is no resources need to be freed.
104         pub free: Option<extern "C" fn(this_arg: *mut c_void)>,
105 }
106 unsafe impl Send for Type {}
107 unsafe impl Sync for Type {}
108 #[no_mangle]
109 pub(crate) extern "C" fn Type_clone_fields(orig: &Type) -> Type {
110         Type {
111                 this_arg: orig.this_arg,
112                 type_id: Clone::clone(&orig.type_id),
113                 debug_str: Clone::clone(&orig.debug_str),
114                 write: Clone::clone(&orig.write),
115                 free: Clone::clone(&orig.free),
116         }
117 }
118 impl core::fmt::Debug for Type {
119         fn fmt(&self, f: &mut core::fmt::Formatter) -> Result<(), core::fmt::Error> {
120                 f.write_str((self.debug_str)(self.this_arg).into_str())
121         }
122 }
123 impl lightning::util::ser::Writeable for Type {
124         fn write<W: lightning::util::ser::Writer>(&self, w: &mut W) -> Result<(), crate::c_types::io::Error> {
125                 let vec = (self.write)(self.this_arg);
126                 w.write_all(vec.as_slice())
127         }
128 }
129
130 use lightning::ln::wire::Type as rustType;
131 impl rustType for Type {
132         fn type_id(&self) -> u16 {
133                 let mut ret = (self.type_id)(self.this_arg);
134                 ret
135         }
136 }
137
138 // We're essentially a pointer already, or at least a set of pointers, so allow us to be used
139 // directly as a Deref trait in higher-level structs:
140 impl core::ops::Deref for Type {
141         type Target = Self;
142         fn deref(&self) -> &Self {
143                 self
144         }
145 }
146 /// Calls the free function if one is set
147 #[no_mangle]
148 pub extern "C" fn Type_free(this_ptr: Type) { }
149 impl Drop for Type {
150         fn drop(&mut self) {
151                 if let Some(f) = self.free {
152                         f(self.this_arg);
153                 }
154         }
155 }