| 1 |
# A handful of classes and functions to support the generated data structures. |
| 2 |
# This would be a good candidate for isolating in its own ffi-support lib. |
| 3 |
|
| 4 |
class InternalError(Exception): |
| 5 |
pass |
| 6 |
|
| 7 |
class _UniffiRustCallStatus(ctypes.Structure): |
| 8 |
""" |
| 9 |
Error runtime. |
| 10 |
""" |
| 11 |
_fields_ = [ |
| 12 |
("code", ctypes.c_int8), |
| 13 |
("error_buf", _UniffiRustBuffer), |
| 14 |
] |
| 15 |
|
| 16 |
# These match the values from the uniffi::rustcalls module |
| 17 |
CALL_SUCCESS = 0 |
| 18 |
CALL_ERROR = 1 |
| 19 |
CALL_UNEXPECTED_ERROR = 2 |
| 20 |
|
| 21 |
@staticmethod |
| 22 |
def default(): |
| 23 |
return _UniffiRustCallStatus(code=_UniffiRustCallStatus.CALL_SUCCESS, error_buf=_UniffiRustBuffer.default()) |
| 24 |
|
| 25 |
def __str__(self): |
| 26 |
if self.code == _UniffiRustCallStatus.CALL_SUCCESS: |
| 27 |
return "_UniffiRustCallStatus(CALL_SUCCESS)" |
| 28 |
elif self.code == _UniffiRustCallStatus.CALL_ERROR: |
| 29 |
return "_UniffiRustCallStatus(CALL_ERROR)" |
| 30 |
elif self.code == _UniffiRustCallStatus.CALL_UNEXPECTED_ERROR: |
| 31 |
return "_UniffiRustCallStatus(CALL_UNEXPECTED_ERROR)" |
| 32 |
else: |
| 33 |
return "_UniffiRustCallStatus(<invalid code>)" |
| 34 |
|
| 35 |
def _uniffi_rust_call(fn, *args): |
| 36 |
# Call a rust function |
| 37 |
return _uniffi_rust_call_with_error(None, fn, *args) |
| 38 |
|
| 39 |
def _uniffi_rust_call_with_error(error_ffi_converter, fn, *args): |
| 40 |
# Call a rust function and handle any errors |
| 41 |
# |
| 42 |
# This function is used for rust calls that return Result<> and therefore can set the CALL_ERROR status code. |
| 43 |
# error_ffi_converter must be set to the _UniffiConverter for the error class that corresponds to the result. |
| 44 |
call_status = _UniffiRustCallStatus.default() |
| 45 |
|
| 46 |
args_with_error = args + (ctypes.byref(call_status),) |
| 47 |
result = fn(*args_with_error) |
| 48 |
_uniffi_check_call_status(error_ffi_converter, call_status) |
| 49 |
return result |
| 50 |
|
| 51 |
def _uniffi_check_call_status(error_ffi_converter, call_status): |
| 52 |
if call_status.code == _UniffiRustCallStatus.CALL_SUCCESS: |
| 53 |
pass |
| 54 |
elif call_status.code == _UniffiRustCallStatus.CALL_ERROR: |
| 55 |
if error_ffi_converter is None: |
| 56 |
call_status.error_buf.free() |
| 57 |
raise InternalError("_uniffi_rust_call_with_error: CALL_ERROR, but error_ffi_converter is None") |
| 58 |
else: |
| 59 |
raise error_ffi_converter.lift(call_status.error_buf) |
| 60 |
elif call_status.code == _UniffiRustCallStatus.CALL_UNEXPECTED_ERROR: |
| 61 |
# When the rust code sees a panic, it tries to construct a _UniffiRustBuffer |
| 62 |
# with the message. But if that code panics, then it just sends back |
| 63 |
# an empty buffer. |
| 64 |
if call_status.error_buf.len > 0: |
| 65 |
msg = _UniffiConverterString.lift(call_status.error_buf) |
| 66 |
else: |
| 67 |
msg = "Unknown rust panic" |
| 68 |
raise InternalError(msg) |
| 69 |
else: |
| 70 |
raise InternalError("Invalid _UniffiRustCallStatus code: {}".format( |
| 71 |
call_status.code)) |
| 72 |
|
| 73 |
def _uniffi_trait_interface_call(call_status, make_call, write_return_value): |
| 74 |
try: |
| 75 |
return write_return_value(make_call()) |
| 76 |
except Exception as e: |
| 77 |
call_status.code = _UniffiRustCallStatus.CALL_UNEXPECTED_ERROR |
| 78 |
call_status.error_buf = {{ Type::String.borrow()|lower_fn }}(repr(e)) |
| 79 |
|
| 80 |
def _uniffi_trait_interface_call_with_error(call_status, make_call, write_return_value, error_type, lower_error): |
| 81 |
try: |
| 82 |
try: |
| 83 |
return write_return_value(make_call()) |
| 84 |
except error_type as e: |
| 85 |
call_status.code = _UniffiRustCallStatus.CALL_ERROR |
| 86 |
call_status.error_buf = lower_error(e) |
| 87 |
except Exception as e: |
| 88 |
call_status.code = _UniffiRustCallStatus.CALL_UNEXPECTED_ERROR |
| 89 |
call_status.error_buf = {{ Type::String.borrow()|lower_fn }}(repr(e)) |