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