1 // This file is Copyright its original authors, visible in version control
2 // history and in the source files from which this was generated.
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.
9 //! Data structures and encoding for `invoice_error` messages.
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};
21 use lightning::offers::invoice_error::InvoiceError as nativeInvoiceErrorImport;
22 pub(crate) type nativeInvoiceError = nativeInvoiceErrorImport;
24 /// An error in response to an [`InvoiceRequest`] or an [`Bolt12Invoice`].
26 /// [`InvoiceRequest`]: crate::offers::invoice_request::InvoiceRequest
27 /// [`Bolt12Invoice`]: crate::offers::invoice::Bolt12Invoice
30 pub struct InvoiceError {
31 /// A pointer to the opaque Rust object.
33 /// Nearly everywhere, inner must be non-null, however in places where
34 /// the Rust equivalent takes an Option, it may be set to null to indicate None.
35 pub inner: *mut nativeInvoiceError,
36 /// Indicates that this is the only struct which contains the same pointer.
38 /// Rust functions which take ownership of an object provided via an argument require
39 /// this to be true and invalidate the object pointed to by inner.
43 impl Drop for InvoiceError {
45 if self.is_owned && !<*mut nativeInvoiceError>::is_null(self.inner) {
46 let _ = unsafe { Box::from_raw(ObjOps::untweak_ptr(self.inner)) };
50 /// Frees any resources used by the InvoiceError, if is_owned is set and inner is non-NULL.
52 pub extern "C" fn InvoiceError_free(this_obj: InvoiceError) { }
54 /// Used only if an object of this type is returned as a trait impl by a method
55 pub(crate) extern "C" fn InvoiceError_free_void(this_ptr: *mut c_void) {
56 let _ = unsafe { Box::from_raw(this_ptr as *mut nativeInvoiceError) };
60 pub(crate) fn get_native_ref(&self) -> &'static nativeInvoiceError {
61 unsafe { &*ObjOps::untweak_ptr(self.inner) }
63 pub(crate) fn get_native_mut_ref(&self) -> &'static mut nativeInvoiceError {
64 unsafe { &mut *ObjOps::untweak_ptr(self.inner) }
66 /// When moving out of the pointer, we have to ensure we aren't a reference, this makes that easy
67 pub(crate) fn take_inner(mut self) -> *mut nativeInvoiceError {
68 assert!(self.is_owned);
69 let ret = ObjOps::untweak_ptr(self.inner);
70 self.inner = core::ptr::null_mut();
74 /// The field in the [`InvoiceRequest`] or the [`Bolt12Invoice`] that contained an error.
76 /// [`InvoiceRequest`]: crate::offers::invoice_request::InvoiceRequest
77 /// [`Bolt12Invoice`]: crate::offers::invoice::Bolt12Invoice
79 /// Note that the return value (or a relevant inner pointer) may be NULL or all-0s to represent None
81 pub extern "C" fn InvoiceError_get_erroneous_field(this_ptr: &InvoiceError) -> crate::lightning::offers::invoice_error::ErroneousField {
82 let mut inner_val = &mut this_ptr.get_native_mut_ref().erroneous_field;
83 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 };
86 /// The field in the [`InvoiceRequest`] or the [`Bolt12Invoice`] that contained an error.
88 /// [`InvoiceRequest`]: crate::offers::invoice_request::InvoiceRequest
89 /// [`Bolt12Invoice`]: crate::offers::invoice::Bolt12Invoice
91 /// Note that val (or a relevant inner pointer) may be NULL or all-0s to represent None
93 pub extern "C" fn InvoiceError_set_erroneous_field(this_ptr: &mut InvoiceError, mut val: crate::lightning::offers::invoice_error::ErroneousField) {
94 let mut local_val = if val.inner.is_null() { None } else { Some( { *unsafe { Box::from_raw(val.take_inner()) } }) };
95 unsafe { &mut *ObjOps::untweak_ptr(this_ptr.inner) }.erroneous_field = local_val;
97 /// An explanation of the error.
99 pub extern "C" fn InvoiceError_get_message(this_ptr: &InvoiceError) -> crate::lightning::util::string::UntrustedString {
100 let mut inner_val = &mut this_ptr.get_native_mut_ref().message;
101 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 }
103 /// An explanation of the error.
105 pub extern "C" fn InvoiceError_set_message(this_ptr: &mut InvoiceError, mut val: crate::lightning::util::string::UntrustedString) {
106 unsafe { &mut *ObjOps::untweak_ptr(this_ptr.inner) }.message = *unsafe { Box::from_raw(val.take_inner()) };
108 /// Constructs a new InvoiceError given each field
110 /// Note that erroneous_field_arg (or a relevant inner pointer) may be NULL or all-0s to represent None
113 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 {
114 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()) } }) };
115 InvoiceError { inner: ObjOps::heap_alloc(nativeInvoiceError {
116 erroneous_field: local_erroneous_field_arg,
117 message: *unsafe { Box::from_raw(message_arg.take_inner()) },
120 impl Clone for InvoiceError {
121 fn clone(&self) -> Self {
123 inner: if <*mut nativeInvoiceError>::is_null(self.inner) { core::ptr::null_mut() } else {
124 ObjOps::heap_alloc(unsafe { &*ObjOps::untweak_ptr(self.inner) }.clone()) },
130 /// Used only if an object of this type is returned as a trait impl by a method
131 pub(crate) extern "C" fn InvoiceError_clone_void(this_ptr: *const c_void) -> *mut c_void {
132 Box::into_raw(Box::new(unsafe { (*(this_ptr as *const nativeInvoiceError)).clone() })) as *mut c_void
135 /// Creates a copy of the InvoiceError
136 pub extern "C" fn InvoiceError_clone(orig: &InvoiceError) -> InvoiceError {
139 /// Get a string which allows debug introspection of a InvoiceError object
140 pub extern "C" fn InvoiceError_debug_str_void(o: *const c_void) -> Str {
141 alloc::format!("{:?}", unsafe { o as *const crate::lightning::offers::invoice_error::InvoiceError }).into()}
143 use lightning::offers::invoice_error::ErroneousField as nativeErroneousFieldImport;
144 pub(crate) type nativeErroneousField = nativeErroneousFieldImport;
146 /// The field in the [`InvoiceRequest`] or the [`Bolt12Invoice`] that contained an error.
148 /// [`InvoiceRequest`]: crate::offers::invoice_request::InvoiceRequest
149 /// [`Bolt12Invoice`]: crate::offers::invoice::Bolt12Invoice
152 pub struct ErroneousField {
153 /// A pointer to the opaque Rust object.
155 /// Nearly everywhere, inner must be non-null, however in places where
156 /// the Rust equivalent takes an Option, it may be set to null to indicate None.
157 pub inner: *mut nativeErroneousField,
158 /// Indicates that this is the only struct which contains the same pointer.
160 /// Rust functions which take ownership of an object provided via an argument require
161 /// this to be true and invalidate the object pointed to by inner.
165 impl Drop for ErroneousField {
167 if self.is_owned && !<*mut nativeErroneousField>::is_null(self.inner) {
168 let _ = unsafe { Box::from_raw(ObjOps::untweak_ptr(self.inner)) };
172 /// Frees any resources used by the ErroneousField, if is_owned is set and inner is non-NULL.
174 pub extern "C" fn ErroneousField_free(this_obj: ErroneousField) { }
176 /// Used only if an object of this type is returned as a trait impl by a method
177 pub(crate) extern "C" fn ErroneousField_free_void(this_ptr: *mut c_void) {
178 let _ = unsafe { Box::from_raw(this_ptr as *mut nativeErroneousField) };
181 impl ErroneousField {
182 pub(crate) fn get_native_ref(&self) -> &'static nativeErroneousField {
183 unsafe { &*ObjOps::untweak_ptr(self.inner) }
185 pub(crate) fn get_native_mut_ref(&self) -> &'static mut nativeErroneousField {
186 unsafe { &mut *ObjOps::untweak_ptr(self.inner) }
188 /// When moving out of the pointer, we have to ensure we aren't a reference, this makes that easy
189 pub(crate) fn take_inner(mut self) -> *mut nativeErroneousField {
190 assert!(self.is_owned);
191 let ret = ObjOps::untweak_ptr(self.inner);
192 self.inner = core::ptr::null_mut();
196 /// The type number of the TLV field containing the error.
198 pub extern "C" fn ErroneousField_get_tlv_fieldnum(this_ptr: &ErroneousField) -> u64 {
199 let mut inner_val = &mut this_ptr.get_native_mut_ref().tlv_fieldnum;
202 /// The type number of the TLV field containing the error.
204 pub extern "C" fn ErroneousField_set_tlv_fieldnum(this_ptr: &mut ErroneousField, mut val: u64) {
205 unsafe { &mut *ObjOps::untweak_ptr(this_ptr.inner) }.tlv_fieldnum = val;
207 /// A value to use for the TLV field to avoid the error.
209 /// Returns a copy of the field.
211 pub extern "C" fn ErroneousField_get_suggested_value(this_ptr: &ErroneousField) -> crate::c_types::derived::COption_CVec_u8ZZ {
212 let mut inner_val = this_ptr.get_native_mut_ref().suggested_value.clone();
213 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() }) };
216 /// A value to use for the TLV field to avoid the error.
218 pub extern "C" fn ErroneousField_set_suggested_value(this_ptr: &mut ErroneousField, mut val: crate::c_types::derived::COption_CVec_u8ZZ) {
219 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 }})} };
220 unsafe { &mut *ObjOps::untweak_ptr(this_ptr.inner) }.suggested_value = local_val;
222 /// Constructs a new ErroneousField given each field
225 pub extern "C" fn ErroneousField_new(mut tlv_fieldnum_arg: u64, mut suggested_value_arg: crate::c_types::derived::COption_CVec_u8ZZ) -> ErroneousField {
226 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 }})} };
227 ErroneousField { inner: ObjOps::heap_alloc(nativeErroneousField {
228 tlv_fieldnum: tlv_fieldnum_arg,
229 suggested_value: local_suggested_value_arg,
232 impl Clone for ErroneousField {
233 fn clone(&self) -> Self {
235 inner: if <*mut nativeErroneousField>::is_null(self.inner) { core::ptr::null_mut() } else {
236 ObjOps::heap_alloc(unsafe { &*ObjOps::untweak_ptr(self.inner) }.clone()) },
242 /// Used only if an object of this type is returned as a trait impl by a method
243 pub(crate) extern "C" fn ErroneousField_clone_void(this_ptr: *const c_void) -> *mut c_void {
244 Box::into_raw(Box::new(unsafe { (*(this_ptr as *const nativeErroneousField)).clone() })) as *mut c_void
247 /// Creates a copy of the ErroneousField
248 pub extern "C" fn ErroneousField_clone(orig: &ErroneousField) -> ErroneousField {
251 /// Get a string which allows debug introspection of a ErroneousField object
252 pub extern "C" fn ErroneousField_debug_str_void(o: *const c_void) -> Str {
253 alloc::format!("{:?}", unsafe { o as *const crate::lightning::offers::invoice_error::ErroneousField }).into()}
254 /// Creates an [`InvoiceError`] with the given message.
257 pub extern "C" fn InvoiceError_from_string(mut s: crate::c_types::Str) -> crate::lightning::offers::invoice_error::InvoiceError {
258 let mut ret = lightning::offers::invoice_error::InvoiceError::from_string(s.into_string());
259 crate::lightning::offers::invoice_error::InvoiceError { inner: ObjOps::heap_alloc(ret), is_owned: true }
263 /// Serialize the InvoiceError object into a byte array which can be read by InvoiceError_read
264 pub extern "C" fn InvoiceError_write(obj: &crate::lightning::offers::invoice_error::InvoiceError) -> crate::c_types::derived::CVec_u8Z {
265 crate::c_types::serialize_obj(unsafe { &*obj }.get_native_ref())
268 pub(crate) extern "C" fn InvoiceError_write_void(obj: *const c_void) -> crate::c_types::derived::CVec_u8Z {
269 crate::c_types::serialize_obj(unsafe { &*(obj as *const nativeInvoiceError) })
272 /// Read a InvoiceError from a byte array, created by InvoiceError_write
273 pub extern "C" fn InvoiceError_read(ser: crate::c_types::u8slice) -> crate::c_types::derived::CResult_InvoiceErrorDecodeErrorZ {
274 let res: Result<lightning::offers::invoice_error::InvoiceError, lightning::ln::msgs::DecodeError> = crate::c_types::deserialize_obj(ser);
275 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() };