386 lines
No EOL
10 KiB
C++
386 lines
No EOL
10 KiB
C++
/* This file was generated by uniffi-bindgen-cpp. */
|
|
#include <string>
|
|
|
|
#include "gram_wallet.hpp"
|
|
|
|
namespace gram_wallet {
|
|
namespace uniffi {
|
|
template <class> inline constexpr bool always_false_v = false;
|
|
|
|
namespace {
|
|
void ensure_initialized() {
|
|
auto bindings_contract_version = 30;
|
|
auto scaffolding_contract_version = ffi_gram_wallet_uniffi_contract_version();
|
|
|
|
if (bindings_contract_version != scaffolding_contract_version) {
|
|
throw std::runtime_error("UniFFI contract version mismatch: try cleaning and rebuilding your project");
|
|
}
|
|
if (uniffi_gram_wallet_checksum_func_is_valid_cell_boc() != 22263) {
|
|
throw std::runtime_error("UniFFI API checksum mismatch: try cleaning and rebuilding your project");
|
|
}
|
|
if (uniffi_gram_wallet_checksum_func_message_body_hash() != 36445) {
|
|
throw std::runtime_error("UniFFI API checksum mismatch: try cleaning and rebuilding your project");
|
|
}
|
|
if (uniffi_gram_wallet_checksum_func_gram_ton_connect_crypto() != 45039) {
|
|
throw std::runtime_error("UniFFI API checksum mismatch: try cleaning and rebuilding your project");
|
|
}
|
|
|
|
}
|
|
|
|
// Note: we need this indirection here and can't inline this code in the rust_call function
|
|
// as it's a templated function
|
|
void initialize() {
|
|
static std::once_flag init_flag;
|
|
std::call_once(init_flag, ensure_initialized);
|
|
}
|
|
}
|
|
|
|
template <typename F>
|
|
void check_rust_call(const RustCallStatus &status, F error_cb) {
|
|
switch (status.code) {
|
|
case 0:
|
|
return;
|
|
|
|
case 1:
|
|
if constexpr (!std::is_null_pointer_v<F>) {
|
|
error_cb(status.error_buf)->throw_underlying();
|
|
}
|
|
break;
|
|
|
|
case 2:
|
|
if (status.error_buf.len > 0) {
|
|
throw std::runtime_error(FfiConverterString::lift(status.error_buf));
|
|
}
|
|
|
|
throw std::runtime_error("A Rust panic has occurred");
|
|
}
|
|
|
|
throw std::runtime_error("Unexpected Rust call status");
|
|
}
|
|
|
|
template <typename F, typename EF, typename... Args, typename R = std::invoke_result_t<F, Args..., RustCallStatus *>>
|
|
R rust_call(F f, EF error_cb, Args... args) {
|
|
initialize();
|
|
|
|
RustCallStatus status = { 0 };
|
|
|
|
if constexpr (std::is_void_v<R>) {
|
|
f(args..., &status);
|
|
check_rust_call(status, error_cb);
|
|
} else {
|
|
auto ret = f(args..., &status);
|
|
check_rust_call(status, error_cb);
|
|
|
|
return ret;
|
|
}
|
|
}
|
|
|
|
namespace {
|
|
struct RustFutureContinuationState {
|
|
std::mutex mutex;
|
|
std::condition_variable condition;
|
|
bool called = false;
|
|
int8_t poll_result = 0;
|
|
};
|
|
|
|
void rust_future_continuation(uint64_t data, int8_t poll_result) {
|
|
auto *state = reinterpret_cast<RustFutureContinuationState *>(data);
|
|
{
|
|
std::lock_guard<std::mutex> lock(state->mutex);
|
|
state->poll_result = poll_result;
|
|
state->called = true;
|
|
}
|
|
state->condition.notify_one();
|
|
}
|
|
|
|
void foreign_future_drop_noop(uint64_t) {}
|
|
|
|
template <typename F>
|
|
struct RustFutureHandleGuard {
|
|
uint64_t handle;
|
|
F free;
|
|
|
|
~RustFutureHandleGuard() {
|
|
free(handle);
|
|
}
|
|
};
|
|
}
|
|
|
|
template <typename Start, typename Poll, typename Complete, typename Free, typename EF,
|
|
typename R = std::invoke_result_t<Complete, uint64_t, RustCallStatus *>>
|
|
R rust_call_async(Start start, Poll poll, Complete complete, Free free, EF error_cb) {
|
|
initialize();
|
|
|
|
auto handle = start();
|
|
RustFutureHandleGuard guard { handle, free };
|
|
|
|
while (true) {
|
|
RustFutureContinuationState state;
|
|
poll(
|
|
handle,
|
|
reinterpret_cast<void *>(&rust_future_continuation),
|
|
reinterpret_cast<uint64_t>(&state)
|
|
);
|
|
|
|
std::unique_lock<std::mutex> lock(state.mutex);
|
|
state.condition.wait(lock, [&state] { return state.called; });
|
|
if (state.poll_result == 0) {
|
|
break;
|
|
}
|
|
if (state.poll_result != 1) {
|
|
throw std::runtime_error("Unexpected Rust future poll result");
|
|
}
|
|
}
|
|
|
|
RustCallStatus status = {};
|
|
if constexpr (std::is_void_v<R>) {
|
|
complete(handle, &status);
|
|
check_rust_call(status, error_cb);
|
|
} else {
|
|
auto ret = complete(handle, &status);
|
|
check_rust_call(status, error_cb);
|
|
return ret;
|
|
}
|
|
}
|
|
|
|
template <typename F, typename W>
|
|
void rust_call_trait_interface(RustCallStatus* status, F make_call, W write_value) {
|
|
initialize();
|
|
|
|
constexpr bool has_return_type = std::negation<std::is_same<void, std::invoke_result_t<F>>>::value;
|
|
|
|
try {
|
|
if constexpr(has_return_type) {
|
|
write_value(make_call());
|
|
} else {
|
|
make_call();
|
|
}
|
|
} catch (std::exception &e) {
|
|
status->code = 2;
|
|
status->error_buf = FfiConverterString::lower(e.what());
|
|
}
|
|
}
|
|
|
|
template <typename E, typename W, typename F, typename EF>
|
|
void rust_call_trait_interface_with_error(RustCallStatus* status, F make_call, W write_value, EF error_cb) {
|
|
initialize();
|
|
|
|
constexpr bool has_return_type = std::negation<std::is_same<void, std::invoke_result_t<F>>>::value;
|
|
|
|
try {
|
|
try {
|
|
if constexpr(has_return_type) {
|
|
write_value(make_call());
|
|
} else {
|
|
make_call();
|
|
}
|
|
} catch (E &e) {
|
|
status->code = 1;
|
|
status->error_buf = error_cb(e);
|
|
}
|
|
} catch (std::exception &e) {
|
|
status->code = 2;
|
|
status->error_buf = FfiConverterString::lower(e.what());
|
|
}
|
|
}
|
|
|
|
|
|
RustBuffer rustbuffer_alloc(uint64_t len) {
|
|
RustCallStatus status = { 0 };
|
|
auto buffer = ffi_gram_wallet_rustbuffer_alloc(len, &status);
|
|
|
|
check_rust_call(status, nullptr);
|
|
|
|
return buffer;
|
|
}
|
|
|
|
RustBuffer rustbuffer_from_bytes(const ForeignBytes &bytes) {
|
|
RustCallStatus status = { 0 };
|
|
auto buffer = ffi_gram_wallet_rustbuffer_from_bytes(bytes, &status);
|
|
|
|
check_rust_call(status, nullptr);
|
|
|
|
return buffer;
|
|
}
|
|
|
|
void rustbuffer_free(RustBuffer buf) {
|
|
RustCallStatus status = { 0 };
|
|
|
|
ffi_gram_wallet_rustbuffer_free(std::move(buf), &status);
|
|
check_rust_call(status, nullptr);
|
|
}
|
|
|
|
|
|
|
|
bool FfiConverterBool::lift(uint8_t val) {
|
|
return !!val;
|
|
}
|
|
|
|
uint8_t FfiConverterBool::lower(bool val) {
|
|
return val;
|
|
}
|
|
|
|
bool FfiConverterBool::read(RustStream &stream) {
|
|
uint8_t val;
|
|
stream >> val;
|
|
|
|
return val;
|
|
}
|
|
|
|
void FfiConverterBool::write(RustStream &stream, bool val) {
|
|
stream << val;
|
|
}
|
|
|
|
uint64_t FfiConverterBool::allocation_size(bool) {
|
|
return 1;
|
|
}
|
|
std::string FfiConverterString::lift(RustBuffer buf) {
|
|
auto string = std::string(reinterpret_cast<char *>(buf.data), buf.len);
|
|
|
|
rustbuffer_free(buf);
|
|
|
|
return string;
|
|
}
|
|
|
|
RustBuffer FfiConverterString::lower(const std::string &val) {
|
|
auto len = static_cast<int32_t>(val.length());
|
|
auto bytes = ForeignBytes { len, reinterpret_cast<uint8_t *>(const_cast<char *>(val.data())) };
|
|
|
|
return rustbuffer_from_bytes(bytes);
|
|
}
|
|
|
|
std::string FfiConverterString::read(RustStream &stream) {
|
|
int32_t len;
|
|
std::string string;
|
|
|
|
stream >> len;
|
|
|
|
string.resize(len);
|
|
stream.read(string.data(), len);
|
|
|
|
return string;
|
|
}
|
|
|
|
void FfiConverterString::write(RustStream &stream, const std::string &val) {
|
|
stream << static_cast<int32_t>(val.length());
|
|
stream.write(val.data(), val.length());
|
|
}
|
|
|
|
uint64_t FfiConverterString::allocation_size(const std::string &val) {
|
|
return static_cast<uint64_t>(sizeof(int32_t) + val.length());
|
|
}
|
|
} // namespace uniffi
|
|
|
|
|
|
|
|
|
|
|
|
namespace uniffi {
|
|
|
|
|
|
|
|
std::shared_ptr<GramWalletError> FfiConverterGramWalletError::lift(RustBuffer buf) {
|
|
auto stream = RustStream(&buf);
|
|
auto ret = FfiConverterGramWalletError::read(stream);
|
|
|
|
rustbuffer_free(buf);
|
|
|
|
return ret;
|
|
}
|
|
|
|
RustBuffer FfiConverterGramWalletError::lower(const GramWalletError &val) {
|
|
auto buf = rustbuffer_alloc(allocation_size(val));
|
|
auto stream = RustStream(&buf);
|
|
|
|
FfiConverterGramWalletError::write(stream, val);
|
|
|
|
return std::move(buf);
|
|
}
|
|
|
|
std::shared_ptr<GramWalletError> FfiConverterGramWalletError::read(RustStream &stream) {
|
|
int32_t v;
|
|
stream >> v;
|
|
|
|
switch (v) {
|
|
case 1:
|
|
{
|
|
gram_wallet_error::InvalidData var("GramWalletError::InvalidData");
|
|
return std::make_shared<gram_wallet_error::InvalidData>(var);
|
|
}
|
|
case 2:
|
|
{
|
|
gram_wallet_error::Protocol var("GramWalletError::Protocol");
|
|
var.message = FfiConverterString::read(stream);
|
|
return std::make_shared<gram_wallet_error::Protocol>(var);
|
|
}
|
|
default:
|
|
throw std::runtime_error("Unexpected error variant");
|
|
}
|
|
}
|
|
|
|
void FfiConverterGramWalletError::write(RustStream &stream, const GramWalletError &val) {
|
|
stream << val.get_variant_idx();
|
|
switch (val.get_variant_idx()) {
|
|
case 1:
|
|
{
|
|
auto var = static_cast<const gram_wallet_error::InvalidData&>(val);
|
|
break;
|
|
}
|
|
case 2:
|
|
{
|
|
auto var = static_cast<const gram_wallet_error::Protocol&>(val);
|
|
FfiConverterString::write(stream, var.message);
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
|
|
uint64_t FfiConverterGramWalletError::allocation_size(const GramWalletError &val) {
|
|
switch (val.get_variant_idx()) {
|
|
case 1:
|
|
{
|
|
auto var = static_cast<const gram_wallet_error::InvalidData&>(val);
|
|
return static_cast<uint64_t>(sizeof(int32_t));
|
|
}
|
|
case 2:
|
|
{
|
|
auto var = static_cast<const gram_wallet_error::Protocol&>(val);
|
|
return static_cast<uint64_t>(sizeof(int32_t)
|
|
+ FfiConverterString::allocation_size(var.message));
|
|
}
|
|
default:
|
|
throw std::runtime_error("Unexpected error variant");
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
bool is_valid_cell_boc(const std::string &boc) {
|
|
auto ret =
|
|
uniffi::rust_call(
|
|
uniffi_gram_wallet_fn_func_is_valid_cell_boc,
|
|
nullptr, uniffi::FfiConverterString::lower(boc));
|
|
|
|
return uniffi::FfiConverterBool::lift(ret);
|
|
}
|
|
|
|
|
|
std::string message_body_hash(const std::string &boc) {
|
|
auto ret =
|
|
uniffi::rust_call(
|
|
uniffi_gram_wallet_fn_func_message_body_hash,
|
|
uniffi::FfiConverterGramWalletError::lift, uniffi::FfiConverterString::lower(boc));
|
|
|
|
return uniffi::FfiConverterString::lift(ret);
|
|
}
|
|
|
|
|
|
std::string gram_ton_connect_crypto(const std::string &phrase, const std::string ¶meters) {
|
|
auto ret =
|
|
uniffi::rust_call(
|
|
uniffi_gram_wallet_fn_func_gram_ton_connect_crypto,
|
|
uniffi::FfiConverterGramWalletError::lift, uniffi::FfiConverterString::lower(phrase), uniffi::FfiConverterString::lower(parameters));
|
|
|
|
return uniffi::FfiConverterString::lift(ret);
|
|
}
|
|
} // namespace gram_wallet
|