Update auto-generated bindings with new module structure
[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 std::ffi::c_void;
12 use bitcoin::hashes::Hash;
13 use crate::c_types::*;
14
15 /// Indicates an error on the client's part (usually some variant of attempting to use too-low or
16 /// too-high values)
17 #[must_use]
18 #[derive(Clone)]
19 #[repr(C)]
20 pub enum APIError {
21         /// Indicates the API was wholly misused (see err for more). Cases where these can be returned
22         /// are documented, but generally indicates some precondition of a function was violated.
23         APIMisuseError {
24                 /// A human-readable error message
25                 err: crate::c_types::derived::CVec_u8Z,
26         },
27         /// Due to a high feerate, we were unable to complete the request.
28         /// For example, this may be returned if the feerate implies we cannot open a channel at the
29         /// requested value, but opening a larger channel would succeed.
30         FeeRateTooHigh {
31                 /// A human-readable error message
32                 err: crate::c_types::derived::CVec_u8Z,
33                 /// The feerate which was too high.
34                 feerate: u32,
35         },
36         /// A malformed Route was provided (eg overflowed value, node id mismatch, overly-looped route,
37         /// too-many-hops, etc).
38         RouteError {
39                 /// A human-readable error message
40                 err: crate::c_types::Str,
41         },
42         /// We were unable to complete the request as the Channel required to do so is unable to
43         /// complete the request (or was not found). This can take many forms, including disconnected
44         /// peer, channel at capacity, channel shutting down, etc.
45         ChannelUnavailable {
46                 /// A human-readable error message
47                 err: crate::c_types::derived::CVec_u8Z,
48         },
49         /// An attempt to call watch/update_channel returned an Err (ie you did this!), causing the
50         /// attempted action to fail.
51         MonitorUpdateFailed,
52 }
53 use lightning::util::errors::APIError as nativeAPIError;
54 impl APIError {
55         #[allow(unused)]
56         pub(crate) fn to_native(&self) -> nativeAPIError {
57                 match self {
58                         APIError::APIMisuseError {ref err, } => {
59                                 let mut err_nonref = (*err).clone();
60                                 nativeAPIError::APIMisuseError {
61                                         err: String::from_utf8(err_nonref.into_rust()).unwrap(),
62                                 }
63                         },
64                         APIError::FeeRateTooHigh {ref err, ref feerate, } => {
65                                 let mut err_nonref = (*err).clone();
66                                 let mut feerate_nonref = (*feerate).clone();
67                                 nativeAPIError::FeeRateTooHigh {
68                                         err: String::from_utf8(err_nonref.into_rust()).unwrap(),
69                                         feerate: feerate_nonref,
70                                 }
71                         },
72                         APIError::RouteError {ref err, } => {
73                                 let mut err_nonref = (*err).clone();
74                                 nativeAPIError::RouteError {
75                                         err: err_nonref.into(),
76                                 }
77                         },
78                         APIError::ChannelUnavailable {ref err, } => {
79                                 let mut err_nonref = (*err).clone();
80                                 nativeAPIError::ChannelUnavailable {
81                                         err: String::from_utf8(err_nonref.into_rust()).unwrap(),
82                                 }
83                         },
84                         APIError::MonitorUpdateFailed => nativeAPIError::MonitorUpdateFailed,
85                 }
86         }
87         #[allow(unused)]
88         pub(crate) fn into_native(self) -> nativeAPIError {
89                 match self {
90                         APIError::APIMisuseError {mut err, } => {
91                                 nativeAPIError::APIMisuseError {
92                                         err: String::from_utf8(err.into_rust()).unwrap(),
93                                 }
94                         },
95                         APIError::FeeRateTooHigh {mut err, mut feerate, } => {
96                                 nativeAPIError::FeeRateTooHigh {
97                                         err: String::from_utf8(err.into_rust()).unwrap(),
98                                         feerate: feerate,
99                                 }
100                         },
101                         APIError::RouteError {mut err, } => {
102                                 nativeAPIError::RouteError {
103                                         err: err.into(),
104                                 }
105                         },
106                         APIError::ChannelUnavailable {mut err, } => {
107                                 nativeAPIError::ChannelUnavailable {
108                                         err: String::from_utf8(err.into_rust()).unwrap(),
109                                 }
110                         },
111                         APIError::MonitorUpdateFailed => nativeAPIError::MonitorUpdateFailed,
112                 }
113         }
114         #[allow(unused)]
115         pub(crate) fn from_native(native: &nativeAPIError) -> Self {
116                 match native {
117                         nativeAPIError::APIMisuseError {ref err, } => {
118                                 let mut err_nonref = (*err).clone();
119                                 APIError::APIMisuseError {
120                                         err: err_nonref.into_bytes().into(),
121                                 }
122                         },
123                         nativeAPIError::FeeRateTooHigh {ref err, ref feerate, } => {
124                                 let mut err_nonref = (*err).clone();
125                                 let mut feerate_nonref = (*feerate).clone();
126                                 APIError::FeeRateTooHigh {
127                                         err: err_nonref.into_bytes().into(),
128                                         feerate: feerate_nonref,
129                                 }
130                         },
131                         nativeAPIError::RouteError {ref err, } => {
132                                 let mut err_nonref = (*err).clone();
133                                 APIError::RouteError {
134                                         err: err_nonref.into(),
135                                 }
136                         },
137                         nativeAPIError::ChannelUnavailable {ref err, } => {
138                                 let mut err_nonref = (*err).clone();
139                                 APIError::ChannelUnavailable {
140                                         err: err_nonref.into_bytes().into(),
141                                 }
142                         },
143                         nativeAPIError::MonitorUpdateFailed => APIError::MonitorUpdateFailed,
144                 }
145         }
146         #[allow(unused)]
147         pub(crate) fn native_into(native: nativeAPIError) -> Self {
148                 match native {
149                         nativeAPIError::APIMisuseError {mut err, } => {
150                                 APIError::APIMisuseError {
151                                         err: err.into_bytes().into(),
152                                 }
153                         },
154                         nativeAPIError::FeeRateTooHigh {mut err, mut feerate, } => {
155                                 APIError::FeeRateTooHigh {
156                                         err: err.into_bytes().into(),
157                                         feerate: feerate,
158                                 }
159                         },
160                         nativeAPIError::RouteError {mut err, } => {
161                                 APIError::RouteError {
162                                         err: err.into(),
163                                 }
164                         },
165                         nativeAPIError::ChannelUnavailable {mut err, } => {
166                                 APIError::ChannelUnavailable {
167                                         err: err.into_bytes().into(),
168                                 }
169                         },
170                         nativeAPIError::MonitorUpdateFailed => APIError::MonitorUpdateFailed,
171                 }
172         }
173 }
174 /// Frees any resources used by the APIError
175 #[no_mangle]
176 pub extern "C" fn APIError_free(this_ptr: APIError) { }
177 /// Creates a copy of the APIError
178 #[no_mangle]
179 pub extern "C" fn APIError_clone(orig: &APIError) -> APIError {
180         orig.clone()
181 }