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