Update CI/Cargo.toml references to 0.0.122
[ldk-c-bindings] / lightning-c-bindings / src / lightning / util / errors.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 //! Error types live here.
10
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};
19
20 /// Indicates an error on the client's part (usually some variant of attempting to use too-low or
21 /// too-high values)
22 #[derive(Clone)]
23 #[must_use]
24 #[repr(C)]
25 pub enum APIError {
26         /// Indicates the API was wholly misused (see err for more). Cases where these can be returned
27         /// are documented, but generally indicates some precondition of a function was violated.
28         APIMisuseError {
29                 /// A human-readable error message
30                 err: crate::c_types::Str,
31         },
32         /// Due to a high feerate, we were unable to complete the request.
33         /// For example, this may be returned if the feerate implies we cannot open a channel at the
34         /// requested value, but opening a larger channel would succeed.
35         FeeRateTooHigh {
36                 /// A human-readable error message
37                 err: crate::c_types::Str,
38                 /// The feerate which was too high.
39                 feerate: u32,
40         },
41         /// A malformed Route was provided (eg overflowed value, node id mismatch, overly-looped route,
42         /// too-many-hops, etc).
43         InvalidRoute {
44                 /// A human-readable error message
45                 err: crate::c_types::Str,
46         },
47         /// We were unable to complete the request as the Channel required to do so is unable to
48         /// complete the request (or was not found). This can take many forms, including disconnected
49         /// peer, channel at capacity, channel shutting down, etc.
50         ChannelUnavailable {
51                 /// A human-readable error message
52                 err: crate::c_types::Str,
53         },
54         /// An attempt to call [`chain::Watch::watch_channel`]/[`chain::Watch::update_channel`]
55         /// returned a [`ChannelMonitorUpdateStatus::InProgress`] indicating the persistence of a
56         /// monitor update is awaiting async resolution. Once it resolves the attempted action should
57         /// complete automatically.
58         ///
59         /// [`chain::Watch::watch_channel`]: crate::chain::Watch::watch_channel
60         /// [`chain::Watch::update_channel`]: crate::chain::Watch::update_channel
61         /// [`ChannelMonitorUpdateStatus::InProgress`]: crate::chain::ChannelMonitorUpdateStatus::InProgress
62         MonitorUpdateInProgress,
63         /// [`SignerProvider::get_shutdown_scriptpubkey`] returned a shutdown scriptpubkey incompatible
64         /// with the channel counterparty as negotiated in [`InitFeatures`].
65         ///
66         /// Using a SegWit v0 script should resolve this issue. If you cannot, you won't be able to open
67         /// a channel or cooperatively close one with this peer (and will have to force-close instead).
68         ///
69         /// [`SignerProvider::get_shutdown_scriptpubkey`]: crate::sign::SignerProvider::get_shutdown_scriptpubkey
70         /// [`InitFeatures`]: crate::ln::features::InitFeatures
71         IncompatibleShutdownScript {
72                 /// The incompatible shutdown script.
73                 script: crate::lightning::ln::script::ShutdownScript,
74         },
75 }
76 use lightning::util::errors::APIError as APIErrorImport;
77 pub(crate) type nativeAPIError = APIErrorImport;
78
79 impl APIError {
80         #[allow(unused)]
81         pub(crate) fn to_native(&self) -> nativeAPIError {
82                 match self {
83                         APIError::APIMisuseError {ref err, } => {
84                                 let mut err_nonref = Clone::clone(err);
85                                 nativeAPIError::APIMisuseError {
86                                         err: err_nonref.into_string(),
87                                 }
88                         },
89                         APIError::FeeRateTooHigh {ref err, ref feerate, } => {
90                                 let mut err_nonref = Clone::clone(err);
91                                 let mut feerate_nonref = Clone::clone(feerate);
92                                 nativeAPIError::FeeRateTooHigh {
93                                         err: err_nonref.into_string(),
94                                         feerate: feerate_nonref,
95                                 }
96                         },
97                         APIError::InvalidRoute {ref err, } => {
98                                 let mut err_nonref = Clone::clone(err);
99                                 nativeAPIError::InvalidRoute {
100                                         err: err_nonref.into_string(),
101                                 }
102                         },
103                         APIError::ChannelUnavailable {ref err, } => {
104                                 let mut err_nonref = Clone::clone(err);
105                                 nativeAPIError::ChannelUnavailable {
106                                         err: err_nonref.into_string(),
107                                 }
108                         },
109                         APIError::MonitorUpdateInProgress => nativeAPIError::MonitorUpdateInProgress,
110                         APIError::IncompatibleShutdownScript {ref script, } => {
111                                 let mut script_nonref = Clone::clone(script);
112                                 nativeAPIError::IncompatibleShutdownScript {
113                                         script: *unsafe { Box::from_raw(script_nonref.take_inner()) },
114                                 }
115                         },
116                 }
117         }
118         #[allow(unused)]
119         pub(crate) fn into_native(self) -> nativeAPIError {
120                 match self {
121                         APIError::APIMisuseError {mut err, } => {
122                                 nativeAPIError::APIMisuseError {
123                                         err: err.into_string(),
124                                 }
125                         },
126                         APIError::FeeRateTooHigh {mut err, mut feerate, } => {
127                                 nativeAPIError::FeeRateTooHigh {
128                                         err: err.into_string(),
129                                         feerate: feerate,
130                                 }
131                         },
132                         APIError::InvalidRoute {mut err, } => {
133                                 nativeAPIError::InvalidRoute {
134                                         err: err.into_string(),
135                                 }
136                         },
137                         APIError::ChannelUnavailable {mut err, } => {
138                                 nativeAPIError::ChannelUnavailable {
139                                         err: err.into_string(),
140                                 }
141                         },
142                         APIError::MonitorUpdateInProgress => nativeAPIError::MonitorUpdateInProgress,
143                         APIError::IncompatibleShutdownScript {mut script, } => {
144                                 nativeAPIError::IncompatibleShutdownScript {
145                                         script: *unsafe { Box::from_raw(script.take_inner()) },
146                                 }
147                         },
148                 }
149         }
150         #[allow(unused)]
151         pub(crate) fn from_native(native: &APIErrorImport) -> Self {
152                 let native = unsafe { &*(native as *const _ as *const c_void as *const nativeAPIError) };
153                 match native {
154                         nativeAPIError::APIMisuseError {ref err, } => {
155                                 let mut err_nonref = Clone::clone(err);
156                                 APIError::APIMisuseError {
157                                         err: err_nonref.into(),
158                                 }
159                         },
160                         nativeAPIError::FeeRateTooHigh {ref err, ref feerate, } => {
161                                 let mut err_nonref = Clone::clone(err);
162                                 let mut feerate_nonref = Clone::clone(feerate);
163                                 APIError::FeeRateTooHigh {
164                                         err: err_nonref.into(),
165                                         feerate: feerate_nonref,
166                                 }
167                         },
168                         nativeAPIError::InvalidRoute {ref err, } => {
169                                 let mut err_nonref = Clone::clone(err);
170                                 APIError::InvalidRoute {
171                                         err: err_nonref.into(),
172                                 }
173                         },
174                         nativeAPIError::ChannelUnavailable {ref err, } => {
175                                 let mut err_nonref = Clone::clone(err);
176                                 APIError::ChannelUnavailable {
177                                         err: err_nonref.into(),
178                                 }
179                         },
180                         nativeAPIError::MonitorUpdateInProgress => APIError::MonitorUpdateInProgress,
181                         nativeAPIError::IncompatibleShutdownScript {ref script, } => {
182                                 let mut script_nonref = Clone::clone(script);
183                                 APIError::IncompatibleShutdownScript {
184                                         script: crate::lightning::ln::script::ShutdownScript { inner: ObjOps::heap_alloc(script_nonref), is_owned: true },
185                                 }
186                         },
187                 }
188         }
189         #[allow(unused)]
190         pub(crate) fn native_into(native: nativeAPIError) -> Self {
191                 match native {
192                         nativeAPIError::APIMisuseError {mut err, } => {
193                                 APIError::APIMisuseError {
194                                         err: err.into(),
195                                 }
196                         },
197                         nativeAPIError::FeeRateTooHigh {mut err, mut feerate, } => {
198                                 APIError::FeeRateTooHigh {
199                                         err: err.into(),
200                                         feerate: feerate,
201                                 }
202                         },
203                         nativeAPIError::InvalidRoute {mut err, } => {
204                                 APIError::InvalidRoute {
205                                         err: err.into(),
206                                 }
207                         },
208                         nativeAPIError::ChannelUnavailable {mut err, } => {
209                                 APIError::ChannelUnavailable {
210                                         err: err.into(),
211                                 }
212                         },
213                         nativeAPIError::MonitorUpdateInProgress => APIError::MonitorUpdateInProgress,
214                         nativeAPIError::IncompatibleShutdownScript {mut script, } => {
215                                 APIError::IncompatibleShutdownScript {
216                                         script: crate::lightning::ln::script::ShutdownScript { inner: ObjOps::heap_alloc(script), is_owned: true },
217                                 }
218                         },
219                 }
220         }
221 }
222 /// Frees any resources used by the APIError
223 #[no_mangle]
224 pub extern "C" fn APIError_free(this_ptr: APIError) { }
225 /// Creates a copy of the APIError
226 #[no_mangle]
227 pub extern "C" fn APIError_clone(orig: &APIError) -> APIError {
228         orig.clone()
229 }
230 #[allow(unused)]
231 /// Used only if an object of this type is returned as a trait impl by a method
232 pub(crate) extern "C" fn APIError_clone_void(this_ptr: *const c_void) -> *mut c_void {
233         Box::into_raw(Box::new(unsafe { (*(this_ptr as *const APIError)).clone() })) as *mut c_void
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 APIError_free_void(this_ptr: *mut c_void) {
238         let _ = unsafe { Box::from_raw(this_ptr as *mut APIError) };
239 }
240 #[no_mangle]
241 /// Utility method to constructs a new APIMisuseError-variant APIError
242 pub extern "C" fn APIError_apimisuse_error(err: crate::c_types::Str) -> APIError {
243         APIError::APIMisuseError {
244                 err,
245         }
246 }
247 #[no_mangle]
248 /// Utility method to constructs a new FeeRateTooHigh-variant APIError
249 pub extern "C" fn APIError_fee_rate_too_high(err: crate::c_types::Str, feerate: u32) -> APIError {
250         APIError::FeeRateTooHigh {
251                 err,
252                 feerate,
253         }
254 }
255 #[no_mangle]
256 /// Utility method to constructs a new InvalidRoute-variant APIError
257 pub extern "C" fn APIError_invalid_route(err: crate::c_types::Str) -> APIError {
258         APIError::InvalidRoute {
259                 err,
260         }
261 }
262 #[no_mangle]
263 /// Utility method to constructs a new ChannelUnavailable-variant APIError
264 pub extern "C" fn APIError_channel_unavailable(err: crate::c_types::Str) -> APIError {
265         APIError::ChannelUnavailable {
266                 err,
267         }
268 }
269 #[no_mangle]
270 /// Utility method to constructs a new MonitorUpdateInProgress-variant APIError
271 pub extern "C" fn APIError_monitor_update_in_progress() -> APIError {
272         APIError::MonitorUpdateInProgress}
273 #[no_mangle]
274 /// Utility method to constructs a new IncompatibleShutdownScript-variant APIError
275 pub extern "C" fn APIError_incompatible_shutdown_script(script: crate::lightning::ln::script::ShutdownScript) -> APIError {
276         APIError::IncompatibleShutdownScript {
277                 script,
278         }
279 }
280 /// Checks if two APIErrors contain equal inner contents.
281 /// This ignores pointers and is_owned flags and looks at the values in fields.
282 #[no_mangle]
283 pub extern "C" fn APIError_eq(a: &APIError, b: &APIError) -> bool {
284         if &a.to_native() == &b.to_native() { true } else { false }
285 }
286 /// Get a string which allows debug introspection of a APIError object
287 pub extern "C" fn APIError_debug_str_void(o: *const c_void) -> Str {
288         alloc::format!("{:?}", unsafe { o as *const crate::lightning::util::errors::APIError }).into()}
289 #[no_mangle]
290 /// Serialize the APIError object into a byte array which can be read by APIError_read
291 pub extern "C" fn APIError_write(obj: &crate::lightning::util::errors::APIError) -> crate::c_types::derived::CVec_u8Z {
292         crate::c_types::serialize_obj(&unsafe { &*obj }.to_native())
293 }
294 #[allow(unused)]
295 pub(crate) extern "C" fn APIError_write_void(obj: *const c_void) -> crate::c_types::derived::CVec_u8Z {
296         APIError_write(unsafe { &*(obj as *const APIError) })
297 }
298 #[no_mangle]
299 /// Read a APIError from a byte array, created by APIError_write
300 pub extern "C" fn APIError_read(ser: crate::c_types::u8slice) -> crate::c_types::derived::CResult_COption_APIErrorZDecodeErrorZ {
301         let res: Result<Option<lightning::util::errors::APIError>, lightning::ln::msgs::DecodeError> = crate::c_types::maybe_deserialize_obj(ser);
302         let mut local_res = match res { Ok(mut o) => crate::c_types::CResultTempl::ok( { let mut local_res_0 = if o.is_none() { crate::c_types::derived::COption_APIErrorZ::None } else { crate::c_types::derived::COption_APIErrorZ::Some( { crate::lightning::util::errors::APIError::native_into(o.unwrap()) }) }; local_res_0 }).into(), Err(mut e) => crate::c_types::CResultTempl::err( { crate::lightning::ln::msgs::DecodeError::native_into(e) }).into() };
303         local_res
304 }