374c5e1e163c2d502cddf0da0bdce16ad08ea07b
[ldk-c-bindings] / lightning-c-bindings / src / lightning / offers / invoice_error.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 //! Data structures and encoding for `invoice_error` messages.
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
20 use lightning::offers::invoice_error::InvoiceError as nativeInvoiceErrorImport;
21 pub(crate) type nativeInvoiceError = nativeInvoiceErrorImport;
22
23 /// An error in response to an [`InvoiceRequest`] or an [`Bolt12Invoice`].
24 ///
25 /// [`InvoiceRequest`]: crate::offers::invoice_request::InvoiceRequest
26 /// [`Bolt12Invoice`]: crate::offers::invoice::Bolt12Invoice
27 #[must_use]
28 #[repr(C)]
29 pub struct InvoiceError {
30         /// A pointer to the opaque Rust object.
31
32         /// Nearly everywhere, inner must be non-null, however in places where
33         /// the Rust equivalent takes an Option, it may be set to null to indicate None.
34         pub inner: *mut nativeInvoiceError,
35         /// Indicates that this is the only struct which contains the same pointer.
36
37         /// Rust functions which take ownership of an object provided via an argument require
38         /// this to be true and invalidate the object pointed to by inner.
39         pub is_owned: bool,
40 }
41
42 impl Drop for InvoiceError {
43         fn drop(&mut self) {
44                 if self.is_owned && !<*mut nativeInvoiceError>::is_null(self.inner) {
45                         let _ = unsafe { Box::from_raw(ObjOps::untweak_ptr(self.inner)) };
46                 }
47         }
48 }
49 /// Frees any resources used by the InvoiceError, if is_owned is set and inner is non-NULL.
50 #[no_mangle]
51 pub extern "C" fn InvoiceError_free(this_obj: InvoiceError) { }
52 #[allow(unused)]
53 /// Used only if an object of this type is returned as a trait impl by a method
54 pub(crate) extern "C" fn InvoiceError_free_void(this_ptr: *mut c_void) {
55         let _ = unsafe { Box::from_raw(this_ptr as *mut nativeInvoiceError) };
56 }
57 #[allow(unused)]
58 impl InvoiceError {
59         pub(crate) fn get_native_ref(&self) -> &'static nativeInvoiceError {
60                 unsafe { &*ObjOps::untweak_ptr(self.inner) }
61         }
62         pub(crate) fn get_native_mut_ref(&self) -> &'static mut nativeInvoiceError {
63                 unsafe { &mut *ObjOps::untweak_ptr(self.inner) }
64         }
65         /// When moving out of the pointer, we have to ensure we aren't a reference, this makes that easy
66         pub(crate) fn take_inner(mut self) -> *mut nativeInvoiceError {
67                 assert!(self.is_owned);
68                 let ret = ObjOps::untweak_ptr(self.inner);
69                 self.inner = core::ptr::null_mut();
70                 ret
71         }
72 }
73 /// The field in the [`InvoiceRequest`] or the [`Bolt12Invoice`] that contained an error.
74 ///
75 /// [`InvoiceRequest`]: crate::offers::invoice_request::InvoiceRequest
76 /// [`Bolt12Invoice`]: crate::offers::invoice::Bolt12Invoice
77 ///
78 /// Note that the return value (or a relevant inner pointer) may be NULL or all-0s to represent None
79 #[no_mangle]
80 pub extern "C" fn InvoiceError_get_erroneous_field(this_ptr: &InvoiceError) -> crate::lightning::offers::invoice_error::ErroneousField {
81         let mut inner_val = &mut this_ptr.get_native_mut_ref().erroneous_field;
82         let mut local_inner_val = crate::lightning::offers::invoice_error::ErroneousField { inner: unsafe { (if inner_val.is_none() { core::ptr::null() } else { ObjOps::nonnull_ptr_to_inner( { (inner_val.as_ref().unwrap()) }) } as *const lightning::offers::invoice_error::ErroneousField<>) as *mut _ }, is_owned: false };
83         local_inner_val
84 }
85 /// The field in the [`InvoiceRequest`] or the [`Bolt12Invoice`] that contained an error.
86 ///
87 /// [`InvoiceRequest`]: crate::offers::invoice_request::InvoiceRequest
88 /// [`Bolt12Invoice`]: crate::offers::invoice::Bolt12Invoice
89 ///
90 /// Note that val (or a relevant inner pointer) may be NULL or all-0s to represent None
91 #[no_mangle]
92 pub extern "C" fn InvoiceError_set_erroneous_field(this_ptr: &mut InvoiceError, mut val: crate::lightning::offers::invoice_error::ErroneousField) {
93         let mut local_val = if val.inner.is_null() { None } else { Some( { *unsafe { Box::from_raw(val.take_inner()) } }) };
94         unsafe { &mut *ObjOps::untweak_ptr(this_ptr.inner) }.erroneous_field = local_val;
95 }
96 /// An explanation of the error.
97 #[no_mangle]
98 pub extern "C" fn InvoiceError_get_message(this_ptr: &InvoiceError) -> crate::lightning::util::string::UntrustedString {
99         let mut inner_val = &mut this_ptr.get_native_mut_ref().message;
100         crate::lightning::util::string::UntrustedString { inner: unsafe { ObjOps::nonnull_ptr_to_inner((inner_val as *const lightning::util::string::UntrustedString<>) as *mut _) }, is_owned: false }
101 }
102 /// An explanation of the error.
103 #[no_mangle]
104 pub extern "C" fn InvoiceError_set_message(this_ptr: &mut InvoiceError, mut val: crate::lightning::util::string::UntrustedString) {
105         unsafe { &mut *ObjOps::untweak_ptr(this_ptr.inner) }.message = *unsafe { Box::from_raw(val.take_inner()) };
106 }
107 /// Constructs a new InvoiceError given each field
108 #[must_use]
109 #[no_mangle]
110 pub extern "C" fn InvoiceError_new(mut erroneous_field_arg: crate::lightning::offers::invoice_error::ErroneousField, mut message_arg: crate::lightning::util::string::UntrustedString) -> InvoiceError {
111         let mut local_erroneous_field_arg = if erroneous_field_arg.inner.is_null() { None } else { Some( { *unsafe { Box::from_raw(erroneous_field_arg.take_inner()) } }) };
112         InvoiceError { inner: ObjOps::heap_alloc(nativeInvoiceError {
113                 erroneous_field: local_erroneous_field_arg,
114                 message: *unsafe { Box::from_raw(message_arg.take_inner()) },
115         }), is_owned: true }
116 }
117 impl Clone for InvoiceError {
118         fn clone(&self) -> Self {
119                 Self {
120                         inner: if <*mut nativeInvoiceError>::is_null(self.inner) { core::ptr::null_mut() } else {
121                                 ObjOps::heap_alloc(unsafe { &*ObjOps::untweak_ptr(self.inner) }.clone()) },
122                         is_owned: true,
123                 }
124         }
125 }
126 #[allow(unused)]
127 /// Used only if an object of this type is returned as a trait impl by a method
128 pub(crate) extern "C" fn InvoiceError_clone_void(this_ptr: *const c_void) -> *mut c_void {
129         Box::into_raw(Box::new(unsafe { (*(this_ptr as *mut nativeInvoiceError)).clone() })) as *mut c_void
130 }
131 #[no_mangle]
132 /// Creates a copy of the InvoiceError
133 pub extern "C" fn InvoiceError_clone(orig: &InvoiceError) -> InvoiceError {
134         orig.clone()
135 }
136
137 use lightning::offers::invoice_error::ErroneousField as nativeErroneousFieldImport;
138 pub(crate) type nativeErroneousField = nativeErroneousFieldImport;
139
140 /// The field in the [`InvoiceRequest`] or the [`Bolt12Invoice`] that contained an error.
141 ///
142 /// [`InvoiceRequest`]: crate::offers::invoice_request::InvoiceRequest
143 /// [`Bolt12Invoice`]: crate::offers::invoice::Bolt12Invoice
144 #[must_use]
145 #[repr(C)]
146 pub struct ErroneousField {
147         /// A pointer to the opaque Rust object.
148
149         /// Nearly everywhere, inner must be non-null, however in places where
150         /// the Rust equivalent takes an Option, it may be set to null to indicate None.
151         pub inner: *mut nativeErroneousField,
152         /// Indicates that this is the only struct which contains the same pointer.
153
154         /// Rust functions which take ownership of an object provided via an argument require
155         /// this to be true and invalidate the object pointed to by inner.
156         pub is_owned: bool,
157 }
158
159 impl Drop for ErroneousField {
160         fn drop(&mut self) {
161                 if self.is_owned && !<*mut nativeErroneousField>::is_null(self.inner) {
162                         let _ = unsafe { Box::from_raw(ObjOps::untweak_ptr(self.inner)) };
163                 }
164         }
165 }
166 /// Frees any resources used by the ErroneousField, if is_owned is set and inner is non-NULL.
167 #[no_mangle]
168 pub extern "C" fn ErroneousField_free(this_obj: ErroneousField) { }
169 #[allow(unused)]
170 /// Used only if an object of this type is returned as a trait impl by a method
171 pub(crate) extern "C" fn ErroneousField_free_void(this_ptr: *mut c_void) {
172         let _ = unsafe { Box::from_raw(this_ptr as *mut nativeErroneousField) };
173 }
174 #[allow(unused)]
175 impl ErroneousField {
176         pub(crate) fn get_native_ref(&self) -> &'static nativeErroneousField {
177                 unsafe { &*ObjOps::untweak_ptr(self.inner) }
178         }
179         pub(crate) fn get_native_mut_ref(&self) -> &'static mut nativeErroneousField {
180                 unsafe { &mut *ObjOps::untweak_ptr(self.inner) }
181         }
182         /// When moving out of the pointer, we have to ensure we aren't a reference, this makes that easy
183         pub(crate) fn take_inner(mut self) -> *mut nativeErroneousField {
184                 assert!(self.is_owned);
185                 let ret = ObjOps::untweak_ptr(self.inner);
186                 self.inner = core::ptr::null_mut();
187                 ret
188         }
189 }
190 /// The type number of the TLV field containing the error.
191 #[no_mangle]
192 pub extern "C" fn ErroneousField_get_tlv_fieldnum(this_ptr: &ErroneousField) -> u64 {
193         let mut inner_val = &mut this_ptr.get_native_mut_ref().tlv_fieldnum;
194         *inner_val
195 }
196 /// The type number of the TLV field containing the error.
197 #[no_mangle]
198 pub extern "C" fn ErroneousField_set_tlv_fieldnum(this_ptr: &mut ErroneousField, mut val: u64) {
199         unsafe { &mut *ObjOps::untweak_ptr(this_ptr.inner) }.tlv_fieldnum = val;
200 }
201 /// A value to use for the TLV field to avoid the error.
202 ///
203 /// Returns a copy of the field.
204 #[no_mangle]
205 pub extern "C" fn ErroneousField_get_suggested_value(this_ptr: &ErroneousField) -> crate::c_types::derived::COption_CVec_u8ZZ {
206         let mut inner_val = this_ptr.get_native_mut_ref().suggested_value.clone();
207         let mut local_inner_val = if inner_val.is_none() { crate::c_types::derived::COption_CVec_u8ZZ::None } else { crate::c_types::derived::COption_CVec_u8ZZ::Some( { let mut local_inner_val_0 = Vec::new(); for mut item in inner_val.unwrap().drain(..) { local_inner_val_0.push( { item }); }; local_inner_val_0.into() }) };
208         local_inner_val
209 }
210 /// A value to use for the TLV field to avoid the error.
211 #[no_mangle]
212 pub extern "C" fn ErroneousField_set_suggested_value(this_ptr: &mut ErroneousField, mut val: crate::c_types::derived::COption_CVec_u8ZZ) {
213         let mut local_val = { /*val*/ let val_opt = val; if val_opt.is_none() { None } else { Some({ { let mut local_val_0 = Vec::new(); for mut item in { val_opt.take() }.into_rust().drain(..) { local_val_0.push( { item }); }; local_val_0 }})} };
214         unsafe { &mut *ObjOps::untweak_ptr(this_ptr.inner) }.suggested_value = local_val;
215 }
216 /// Constructs a new ErroneousField given each field
217 #[must_use]
218 #[no_mangle]
219 pub extern "C" fn ErroneousField_new(mut tlv_fieldnum_arg: u64, mut suggested_value_arg: crate::c_types::derived::COption_CVec_u8ZZ) -> ErroneousField {
220         let mut local_suggested_value_arg = { /*suggested_value_arg*/ let suggested_value_arg_opt = suggested_value_arg; if suggested_value_arg_opt.is_none() { None } else { Some({ { let mut local_suggested_value_arg_0 = Vec::new(); for mut item in { suggested_value_arg_opt.take() }.into_rust().drain(..) { local_suggested_value_arg_0.push( { item }); }; local_suggested_value_arg_0 }})} };
221         ErroneousField { inner: ObjOps::heap_alloc(nativeErroneousField {
222                 tlv_fieldnum: tlv_fieldnum_arg,
223                 suggested_value: local_suggested_value_arg,
224         }), is_owned: true }
225 }
226 impl Clone for ErroneousField {
227         fn clone(&self) -> Self {
228                 Self {
229                         inner: if <*mut nativeErroneousField>::is_null(self.inner) { core::ptr::null_mut() } else {
230                                 ObjOps::heap_alloc(unsafe { &*ObjOps::untweak_ptr(self.inner) }.clone()) },
231                         is_owned: true,
232                 }
233         }
234 }
235 #[allow(unused)]
236 /// Used only if an object of this type is returned as a trait impl by a method
237 pub(crate) extern "C" fn ErroneousField_clone_void(this_ptr: *const c_void) -> *mut c_void {
238         Box::into_raw(Box::new(unsafe { (*(this_ptr as *mut nativeErroneousField)).clone() })) as *mut c_void
239 }
240 #[no_mangle]
241 /// Creates a copy of the ErroneousField
242 pub extern "C" fn ErroneousField_clone(orig: &ErroneousField) -> ErroneousField {
243         orig.clone()
244 }
245 #[no_mangle]
246 /// Serialize the InvoiceError object into a byte array which can be read by InvoiceError_read
247 pub extern "C" fn InvoiceError_write(obj: &crate::lightning::offers::invoice_error::InvoiceError) -> crate::c_types::derived::CVec_u8Z {
248         crate::c_types::serialize_obj(unsafe { &*obj }.get_native_ref())
249 }
250 #[no_mangle]
251 pub(crate) extern "C" fn InvoiceError_write_void(obj: *const c_void) -> crate::c_types::derived::CVec_u8Z {
252         crate::c_types::serialize_obj(unsafe { &*(obj as *const nativeInvoiceError) })
253 }
254 #[no_mangle]
255 /// Read a InvoiceError from a byte array, created by InvoiceError_write
256 pub extern "C" fn InvoiceError_read(ser: crate::c_types::u8slice) -> crate::c_types::derived::CResult_InvoiceErrorDecodeErrorZ {
257         let res: Result<lightning::offers::invoice_error::InvoiceError, lightning::ln::msgs::DecodeError> = crate::c_types::deserialize_obj(ser);
258         let mut local_res = match res { Ok(mut o) => crate::c_types::CResultTempl::ok( { crate::lightning::offers::invoice_error::InvoiceError { inner: ObjOps::heap_alloc(o), is_owned: true } }).into(), Err(mut e) => crate::c_types::CResultTempl::err( { crate::lightning::ln::msgs::DecodeError::native_into(e) }).into() };
259         local_res
260 }