| 1 |
class RustCallStatus < FFI::Struct |
| 2 |
layout :code, :int8, |
| 3 |
:error_buf, RustBuffer |
| 4 |
|
| 5 |
def code |
| 6 |
self[:code] |
| 7 |
end |
| 8 |
|
| 9 |
def error_buf |
| 10 |
self[:error_buf] |
| 11 |
end |
| 12 |
|
| 13 |
def to_s |
| 14 |
"RustCallStatus(code=#{self[:code]})" |
| 15 |
end |
| 16 |
end |
| 17 |
|
| 18 |
# These match the values from the uniffi::rustcalls module |
| 19 |
CALL_SUCCESS = 0 |
| 20 |
CALL_ERROR = 1 |
| 21 |
CALL_PANIC = 2 |
| 22 |
{%- for e in ci.enum_definitions() %} |
| 23 |
{% if ci.is_name_used_as_error(e.name()) %} |
| 24 |
{% if e.is_flat() %} |
| 25 |
class {{ e.name()|class_name_rb }} |
| 26 |
{%- for variant in e.variants() %} |
| 27 |
{{ variant.name()|class_name_rb }} = Class.new StandardError |
| 28 |
{%- endfor %} |
| 29 |
{% else %} |
| 30 |
module {{ e.name()|class_name_rb }} |
| 31 |
{%- for variant in e.variants() %} |
| 32 |
class {{ variant.name()|class_name_rb }} < StandardError |
| 33 |
def initialize({% for field in variant.fields() %}{{ field.name()|var_name_rb }}{% if !loop.last %}, {% endif %}{% endfor %}) |
| 34 |
{%- for field in variant.fields() %} |
| 35 |
@{{ field.name()|var_name_rb }} = {{ field.name()|var_name_rb }} |
| 36 |
{%- endfor %} |
| 37 |
super() |
| 38 |
end |
| 39 |
{%- if variant.has_fields() %} |
| 40 |
|
| 41 |
attr_reader {% for field in variant.fields() %}:{{ field.name()|var_name_rb }}{% if !loop.last %}, {% endif %}{% endfor %} |
| 42 |
{% endif %} |
| 43 |
|
| 44 |
def to_s |
| 45 |
"#{self.class.name}({% for field in variant.fields() %}{{ field.name()|var_name_rb }}=#{@{{ field.name()|var_name_rb }}.inspect}{% if !loop.last %}, {% endif %}{% endfor %})" |
| 46 |
end |
| 47 |
end |
| 48 |
{%- endfor %} |
| 49 |
{% endif %} |
| 50 |
end |
| 51 |
{% endif %} |
| 52 |
{%- endfor %} |
| 53 |
|
| 54 |
# Map error modules to the RustBuffer method name that reads them |
| 55 |
ERROR_MODULE_TO_READER_METHOD = { |
| 56 |
{%- for e in ci.enum_definitions() %} |
| 57 |
{% if ci.is_name_used_as_error(e.name()) %} |
| 58 |
{%- let typ=ci.get_type(e.name()).unwrap() %} |
| 59 |
{%- let canonical_type_name = self::canonical_name(typ.borrow()).borrow()|class_name_rb %} |
| 60 |
{{ e.name()|class_name_rb }} => :read{{ canonical_type_name }}, |
| 61 |
{% endif %} |
| 62 |
{%- endfor %} |
| 63 |
} |
| 64 |
|
| 65 |
private_constant :ERROR_MODULE_TO_READER_METHOD, :CALL_SUCCESS, :CALL_ERROR, :CALL_PANIC, |
| 66 |
:RustCallStatus |
| 67 |
|
| 68 |
def self.consume_buffer_into_error(error_module, rust_buffer) |
| 69 |
rust_buffer.consumeWithStream do |stream| |
| 70 |
reader_method = ERROR_MODULE_TO_READER_METHOD[error_module] |
| 71 |
return stream.send(reader_method) |
| 72 |
end |
| 73 |
end |
| 74 |
|
| 75 |
class InternalError < StandardError |
| 76 |
end |
| 77 |
|
| 78 |
def self.rust_call(fn_name, *args) |
| 79 |
# Call a rust function |
| 80 |
rust_call_with_error(nil, fn_name, *args) |
| 81 |
end |
| 82 |
|
| 83 |
def self.rust_call_with_error(error_module, fn_name, *args) |
| 84 |
# Call a rust function and handle errors |
| 85 |
# |
| 86 |
# Use this when the rust function returns a Result<>. error_module must be the error_module that corresponds to that Result. |
| 87 |
|
| 88 |
|
| 89 |
# Note: RustCallStatus.new zeroes out the struct, which is exactly what we |
| 90 |
# want to pass to Rust (code=0, error_buf=RustBuffer(len=0, capacity=0, |
| 91 |
# data=NULL)) |
| 92 |
status = RustCallStatus.new |
| 93 |
args << status |
| 94 |
|
| 95 |
result = UniFFILib.public_send(fn_name, *args) |
| 96 |
|
| 97 |
case status.code |
| 98 |
when CALL_SUCCESS |
| 99 |
result |
| 100 |
when CALL_ERROR |
| 101 |
if error_module.nil? |
| 102 |
status.error_buf.free |
| 103 |
raise InternalError, "CALL_ERROR with no error_module set" |
| 104 |
else |
| 105 |
raise consume_buffer_into_error(error_module, status.error_buf) |
| 106 |
end |
| 107 |
when CALL_PANIC |
| 108 |
# When the rust code sees a panic, it tries to construct a RustBuffer |
| 109 |
# with the message. But if that code panics, then it just sends back |
| 110 |
# an empty buffer. |
| 111 |
if status.error_buf.len > 0 |
| 112 |
raise InternalError, status.error_buf.consumeIntoString() |
| 113 |
else |
| 114 |
raise InternalError, "Rust panic" |
| 115 |
end |
| 116 |
else |
| 117 |
raise InternalError, "Unknown call status: #{status.code}" |
| 118 |
end |
| 119 |
end |
| 120 |
|
| 121 |
private_class_method :consume_buffer_into_error |