| 1 | /* This file was generated by uniffi-bindgen-cpp. */ |
| 2 | #include <string> |
| 3 | |
| 4 | #include "gram_wallet.hpp" |
| 5 | |
| 6 | namespace gram_wallet { |
| 7 | namespace uniffi { |
| 8 | template <class> inline constexpr bool always_false_v = false; |
| 9 | |
| 10 | namespace { |
| 11 | void ensure_initialized() { |
| 12 | auto bindings_contract_version = 30; |
| 13 | auto scaffolding_contract_version = ffi_gram_wallet_uniffi_contract_version(); |
| 14 | |
| 15 | if (bindings_contract_version != scaffolding_contract_version) { |
| 16 | throw std::runtime_error("UniFFI contract version mismatch: try cleaning and rebuilding your project"); |
| 17 | } |
| 18 | if (uniffi_gram_wallet_checksum_func_is_valid_cell_boc() != 22263) { |
| 19 | throw std::runtime_error("UniFFI API checksum mismatch: try cleaning and rebuilding your project"); |
| 20 | } |
| 21 | if (uniffi_gram_wallet_checksum_func_message_body_hash() != 36445) { |
| 22 | throw std::runtime_error("UniFFI API checksum mismatch: try cleaning and rebuilding your project"); |
| 23 | } |
| 24 | if (uniffi_gram_wallet_checksum_func_gram_ton_connect_crypto() != 45039) { |
| 25 | throw std::runtime_error("UniFFI API checksum mismatch: try cleaning and rebuilding your project"); |
| 26 | } |
| 27 | |
| 28 | } |
| 29 | |
| 30 | // Note: we need this indirection here and can't inline this code in the rust_call function |
| 31 | // as it's a templated function |
| 32 | void initialize() { |
| 33 | static std::once_flag init_flag; |
| 34 | std::call_once(init_flag, ensure_initialized); |
| 35 | } |
| 36 | } |
| 37 | |
| 38 | template <typename F> |
| 39 | void check_rust_call(const RustCallStatus &status, F error_cb) { |
| 40 | switch (status.code) { |
| 41 | case 0: |
| 42 | return; |
| 43 | |
| 44 | case 1: |
| 45 | if constexpr (!std::is_null_pointer_v<F>) { |
| 46 | error_cb(status.error_buf)->throw_underlying(); |
| 47 | } |
| 48 | break; |
| 49 | |
| 50 | case 2: |
| 51 | if (status.error_buf.len > 0) { |
| 52 | throw std::runtime_error(FfiConverterString::lift(status.error_buf)); |
| 53 | } |
| 54 | |
| 55 | throw std::runtime_error("A Rust panic has occurred"); |
| 56 | } |
| 57 | |
| 58 | throw std::runtime_error("Unexpected Rust call status"); |
| 59 | } |
| 60 | |
| 61 | template <typename F, typename EF, typename... Args, typename R = std::invoke_result_t<F, Args..., RustCallStatus *>> |
| 62 | R rust_call(F f, EF error_cb, Args... args) { |
| 63 | initialize(); |
| 64 | |
| 65 | RustCallStatus status = { 0 }; |
| 66 | |
| 67 | if constexpr (std::is_void_v<R>) { |
| 68 | f(args..., &status); |
| 69 | check_rust_call(status, error_cb); |
| 70 | } else { |
| 71 | auto ret = f(args..., &status); |
| 72 | check_rust_call(status, error_cb); |
| 73 | |
| 74 | return ret; |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | namespace { |
| 79 | struct RustFutureContinuationState { |
| 80 | std::mutex mutex; |
| 81 | std::condition_variable condition; |
| 82 | bool called = false; |
| 83 | int8_t poll_result = 0; |
| 84 | }; |
| 85 | |
| 86 | void rust_future_continuation(uint64_t data, int8_t poll_result) { |
| 87 | auto *state = reinterpret_cast<RustFutureContinuationState *>(data); |
| 88 | { |
| 89 | std::lock_guard<std::mutex> lock(state->mutex); |
| 90 | state->poll_result = poll_result; |
| 91 | state->called = true; |
| 92 | } |
| 93 | state->condition.notify_one(); |
| 94 | } |
| 95 | |
| 96 | void foreign_future_drop_noop(uint64_t) {} |
| 97 | |
| 98 | template <typename F> |
| 99 | struct RustFutureHandleGuard { |
| 100 | uint64_t handle; |
| 101 | F free; |
| 102 | |
| 103 | ~RustFutureHandleGuard() { |
| 104 | free(handle); |
| 105 | } |
| 106 | }; |
| 107 | } |
| 108 | |
| 109 | template <typename Start, typename Poll, typename Complete, typename Free, typename EF, |
| 110 | typename R = std::invoke_result_t<Complete, uint64_t, RustCallStatus *>> |
| 111 | R rust_call_async(Start start, Poll poll, Complete complete, Free free, EF error_cb) { |
| 112 | initialize(); |
| 113 | |
| 114 | auto handle = start(); |
| 115 | RustFutureHandleGuard guard { handle, free }; |
| 116 | |
| 117 | while (true) { |
| 118 | RustFutureContinuationState state; |
| 119 | poll( |
| 120 | handle, |
| 121 | reinterpret_cast<void *>(&rust_future_continuation), |
| 122 | reinterpret_cast<uint64_t>(&state) |
| 123 | ); |
| 124 | |
| 125 | std::unique_lock<std::mutex> lock(state.mutex); |
| 126 | state.condition.wait(lock, [&state] { return state.called; }); |
| 127 | if (state.poll_result == 0) { |
| 128 | break; |
| 129 | } |
| 130 | if (state.poll_result != 1) { |
| 131 | throw std::runtime_error("Unexpected Rust future poll result"); |
| 132 | } |
| 133 | } |
| 134 | |
| 135 | RustCallStatus status = {}; |
| 136 | if constexpr (std::is_void_v<R>) { |
| 137 | complete(handle, &status); |
| 138 | check_rust_call(status, error_cb); |
| 139 | } else { |
| 140 | auto ret = complete(handle, &status); |
| 141 | check_rust_call(status, error_cb); |
| 142 | return ret; |
| 143 | } |
| 144 | } |
| 145 | |
| 146 | template <typename F, typename W> |
| 147 | void rust_call_trait_interface(RustCallStatus* status, F make_call, W write_value) { |
| 148 | initialize(); |
| 149 | |
| 150 | constexpr bool has_return_type = std::negation<std::is_same<void, std::invoke_result_t<F>>>::value; |
| 151 | |
| 152 | try { |
| 153 | if constexpr(has_return_type) { |
| 154 | write_value(make_call()); |
| 155 | } else { |
| 156 | make_call(); |
| 157 | } |
| 158 | } catch (std::exception &e) { |
| 159 | status->code = 2; |
| 160 | status->error_buf = FfiConverterString::lower(e.what()); |
| 161 | } |
| 162 | } |
| 163 | |
| 164 | template <typename E, typename W, typename F, typename EF> |
| 165 | void rust_call_trait_interface_with_error(RustCallStatus* status, F make_call, W write_value, EF error_cb) { |
| 166 | initialize(); |
| 167 | |
| 168 | constexpr bool has_return_type = std::negation<std::is_same<void, std::invoke_result_t<F>>>::value; |
| 169 | |
| 170 | try { |
| 171 | try { |
| 172 | if constexpr(has_return_type) { |
| 173 | write_value(make_call()); |
| 174 | } else { |
| 175 | make_call(); |
| 176 | } |
| 177 | } catch (E &e) { |
| 178 | status->code = 1; |
| 179 | status->error_buf = error_cb(e); |
| 180 | } |
| 181 | } catch (std::exception &e) { |
| 182 | status->code = 2; |
| 183 | status->error_buf = FfiConverterString::lower(e.what()); |
| 184 | } |
| 185 | } |
| 186 | |
| 187 | |
| 188 | RustBuffer rustbuffer_alloc(uint64_t len) { |
| 189 | RustCallStatus status = { 0 }; |
| 190 | auto buffer = ffi_gram_wallet_rustbuffer_alloc(len, &status); |
| 191 | |
| 192 | check_rust_call(status, nullptr); |
| 193 | |
| 194 | return buffer; |
| 195 | } |
| 196 | |
| 197 | RustBuffer rustbuffer_from_bytes(const ForeignBytes &bytes) { |
| 198 | RustCallStatus status = { 0 }; |
| 199 | auto buffer = ffi_gram_wallet_rustbuffer_from_bytes(bytes, &status); |
| 200 | |
| 201 | check_rust_call(status, nullptr); |
| 202 | |
| 203 | return buffer; |
| 204 | } |
| 205 | |
| 206 | void rustbuffer_free(RustBuffer buf) { |
| 207 | RustCallStatus status = { 0 }; |
| 208 | |
| 209 | ffi_gram_wallet_rustbuffer_free(std::move(buf), &status); |
| 210 | check_rust_call(status, nullptr); |
| 211 | } |
| 212 | |
| 213 | |
| 214 | |
| 215 | bool FfiConverterBool::lift(uint8_t val) { |
| 216 | return !!val; |
| 217 | } |
| 218 | |
| 219 | uint8_t FfiConverterBool::lower(bool val) { |
| 220 | return val; |
| 221 | } |
| 222 | |
| 223 | bool FfiConverterBool::read(RustStream &stream) { |
| 224 | uint8_t val; |
| 225 | stream >> val; |
| 226 | |
| 227 | return val; |
| 228 | } |
| 229 | |
| 230 | void FfiConverterBool::write(RustStream &stream, bool val) { |
| 231 | stream << val; |
| 232 | } |
| 233 | |
| 234 | uint64_t FfiConverterBool::allocation_size(bool) { |
| 235 | return 1; |
| 236 | } |
| 237 | std::string FfiConverterString::lift(RustBuffer buf) { |
| 238 | auto string = std::string(reinterpret_cast<char *>(buf.data), buf.len); |
| 239 | |
| 240 | rustbuffer_free(buf); |
| 241 | |
| 242 | return string; |
| 243 | } |
| 244 | |
| 245 | RustBuffer FfiConverterString::lower(const std::string &val) { |
| 246 | auto len = static_cast<int32_t>(val.length()); |
| 247 | auto bytes = ForeignBytes { len, reinterpret_cast<uint8_t *>(const_cast<char *>(val.data())) }; |
| 248 | |
| 249 | return rustbuffer_from_bytes(bytes); |
| 250 | } |
| 251 | |
| 252 | std::string FfiConverterString::read(RustStream &stream) { |
| 253 | int32_t len; |
| 254 | std::string string; |
| 255 | |
| 256 | stream >> len; |
| 257 | |
| 258 | string.resize(len); |
| 259 | stream.read(string.data(), len); |
| 260 | |
| 261 | return string; |
| 262 | } |
| 263 | |
| 264 | void FfiConverterString::write(RustStream &stream, const std::string &val) { |
| 265 | stream << static_cast<int32_t>(val.length()); |
| 266 | stream.write(val.data(), val.length()); |
| 267 | } |
| 268 | |
| 269 | uint64_t FfiConverterString::allocation_size(const std::string &val) { |
| 270 | return static_cast<uint64_t>(sizeof(int32_t) + val.length()); |
| 271 | } |
| 272 | } // namespace uniffi |
| 273 | |
| 274 | |
| 275 | |
| 276 | |
| 277 | |
| 278 | namespace uniffi { |
| 279 | |
| 280 | |
| 281 | |
| 282 | std::shared_ptr<GramWalletError> FfiConverterGramWalletError::lift(RustBuffer buf) { |
| 283 | auto stream = RustStream(&buf); |
| 284 | auto ret = FfiConverterGramWalletError::read(stream); |
| 285 | |
| 286 | rustbuffer_free(buf); |
| 287 | |
| 288 | return ret; |
| 289 | } |
| 290 | |
| 291 | RustBuffer FfiConverterGramWalletError::lower(const GramWalletError &val) { |
| 292 | auto buf = rustbuffer_alloc(allocation_size(val)); |
| 293 | auto stream = RustStream(&buf); |
| 294 | |
| 295 | FfiConverterGramWalletError::write(stream, val); |
| 296 | |
| 297 | return std::move(buf); |
| 298 | } |
| 299 | |
| 300 | std::shared_ptr<GramWalletError> FfiConverterGramWalletError::read(RustStream &stream) { |
| 301 | int32_t v; |
| 302 | stream >> v; |
| 303 | |
| 304 | switch (v) { |
| 305 | case 1: |
| 306 | { |
| 307 | gram_wallet_error::InvalidData var("GramWalletError::InvalidData"); |
| 308 | return std::make_shared<gram_wallet_error::InvalidData>(var); |
| 309 | } |
| 310 | case 2: |
| 311 | { |
| 312 | gram_wallet_error::Protocol var("GramWalletError::Protocol"); |
| 313 | var.message = FfiConverterString::read(stream); |
| 314 | return std::make_shared<gram_wallet_error::Protocol>(var); |
| 315 | } |
| 316 | default: |
| 317 | throw std::runtime_error("Unexpected error variant"); |
| 318 | } |
| 319 | } |
| 320 | |
| 321 | void FfiConverterGramWalletError::write(RustStream &stream, const GramWalletError &val) { |
| 322 | stream << val.get_variant_idx(); |
| 323 | switch (val.get_variant_idx()) { |
| 324 | case 1: |
| 325 | { |
| 326 | auto var = static_cast<const gram_wallet_error::InvalidData&>(val); |
| 327 | break; |
| 328 | } |
| 329 | case 2: |
| 330 | { |
| 331 | auto var = static_cast<const gram_wallet_error::Protocol&>(val); |
| 332 | FfiConverterString::write(stream, var.message); |
| 333 | break; |
| 334 | } |
| 335 | } |
| 336 | } |
| 337 | |
| 338 | uint64_t FfiConverterGramWalletError::allocation_size(const GramWalletError &val) { |
| 339 | switch (val.get_variant_idx()) { |
| 340 | case 1: |
| 341 | { |
| 342 | auto var = static_cast<const gram_wallet_error::InvalidData&>(val); |
| 343 | return static_cast<uint64_t>(sizeof(int32_t)); |
| 344 | } |
| 345 | case 2: |
| 346 | { |
| 347 | auto var = static_cast<const gram_wallet_error::Protocol&>(val); |
| 348 | return static_cast<uint64_t>(sizeof(int32_t) |
| 349 | + FfiConverterString::allocation_size(var.message)); |
| 350 | } |
| 351 | default: |
| 352 | throw std::runtime_error("Unexpected error variant"); |
| 353 | } |
| 354 | } |
| 355 | |
| 356 | } |
| 357 | |
| 358 | bool is_valid_cell_boc(const std::string &boc) { |
| 359 | auto ret = |
| 360 | uniffi::rust_call( |
| 361 | uniffi_gram_wallet_fn_func_is_valid_cell_boc, |
| 362 | nullptr, uniffi::FfiConverterString::lower(boc)); |
| 363 | |
| 364 | return uniffi::FfiConverterBool::lift(ret); |
| 365 | } |
| 366 | |
| 367 | |
| 368 | std::string message_body_hash(const std::string &boc) { |
| 369 | auto ret = |
| 370 | uniffi::rust_call( |
| 371 | uniffi_gram_wallet_fn_func_message_body_hash, |
| 372 | uniffi::FfiConverterGramWalletError::lift, uniffi::FfiConverterString::lower(boc)); |
| 373 | |
| 374 | return uniffi::FfiConverterString::lift(ret); |
| 375 | } |
| 376 | |
| 377 | |
| 378 | std::string gram_ton_connect_crypto(const std::string &phrase, const std::string ¶meters) { |
| 379 | auto ret = |
| 380 | uniffi::rust_call( |
| 381 | uniffi_gram_wallet_fn_func_gram_ton_connect_crypto, |
| 382 | uniffi::FfiConverterGramWalletError::lift, uniffi::FfiConverterString::lower(phrase), uniffi::FfiConverterString::lower(parameters)); |
| 383 | |
| 384 | return uniffi::FfiConverterString::lift(ret); |
| 385 | } |
| 386 | } // namespace gram_wallet |