| 1 |
{# |
| 2 |
// Template to call into rust. Used in several places. |
| 3 |
// Variable names in `arg_list_decl` should match up with arg lists |
| 4 |
// passed to rust via `arg_list_lowered` |
| 5 |
#} |
| 6 |
|
| 7 |
{%- macro to_ffi_call(func) -%} |
| 8 |
{%- call _to_ffi_call_with_prefix_arg("", func) %} |
| 9 |
{%- endmacro -%} |
| 10 |
|
| 11 |
{%- macro to_ffi_call_with_prefix(prefix, func) -%} |
| 12 |
{%- call _to_ffi_call_with_prefix_arg(format!("{},", prefix), func) %} |
| 13 |
{%- endmacro -%} |
| 14 |
|
| 15 |
{%- macro _to_ffi_call_with_prefix_arg(prefix, func) -%} |
| 16 |
{%- match func.throws_type() -%} |
| 17 |
{%- when Some with (e) -%} |
| 18 |
{%- match e -%} |
| 19 |
{%- when Type::Enum { .. } -%} |
| 20 |
_uniffi_rust_call_with_error({{ e|ffi_converter_name }}, |
| 21 |
{%- when Type::Object { .. } -%} |
| 22 |
_uniffi_rust_call_with_error({{ e|ffi_converter_name }}__as_error, |
| 23 |
{%- else %} |
| 24 |
# unsupported error type! |
| 25 |
{%- endmatch %} |
| 26 |
{%- else -%} |
| 27 |
_uniffi_rust_call( |
| 28 |
{%- endmatch -%} |
| 29 |
_UniffiLib.{{ func.ffi_func().name() }}, |
| 30 |
{{- prefix }} |
| 31 |
{%- call arg_list_lowered(func) -%} |
| 32 |
) |
| 33 |
{%- endmacro -%} |
| 34 |
|
| 35 |
{%- macro arg_list_lowered(func) %} |
| 36 |
{%- for arg in func.arguments() %} |
| 37 |
{{ arg|lower_fn }}({{ arg.name() }}) |
| 38 |
{%- if !loop.last %},{% endif %} |
| 39 |
{%- endfor %} |
| 40 |
{%- endmacro -%} |
| 41 |
|
| 42 |
{%- macro docstring_value(maybe_docstring, indent_spaces) %} |
| 43 |
{%- if let Some(docstring) = maybe_docstring %} |
| 44 |
{{ docstring|docstring(indent_spaces) }} |
| 45 |
{{ "" }} |
| 46 |
{%- endif %} |
| 47 |
{%- endmacro %} |
| 48 |
|
| 49 |
{%- macro docstring(defn, indent_spaces) %} |
| 50 |
{%- call docstring_value(defn.docstring(), indent_spaces) %} |
| 51 |
{%- endmacro %} |
| 52 |
|
| 53 |
{#- |
| 54 |
// Arglist as used in Python declarations of methods, functions and constructors. |
| 55 |
// Note the type_name filters. |
| 56 |
-#} |
| 57 |
|
| 58 |
{% macro arg_list_decl(func) %} |
| 59 |
{%- for arg in func.arguments() -%} |
| 60 |
{{ arg.name() }} |
| 61 |
{%- match arg.default_value() %} |
| 62 |
{%- when Some with(literal) %}: "typing.Union[object, {{ arg|type_name -}}]" = _DEFAULT |
| 63 |
{%- else %}: "{{ arg|type_name -}}" |
| 64 |
{%- endmatch %} |
| 65 |
{%- if !loop.last %},{% endif -%} |
| 66 |
{%- endfor %} |
| 67 |
{%- endmacro %} |
| 68 |
|
| 69 |
{#- |
| 70 |
// Arglist as used in the _UniffiLib function declarations. |
| 71 |
// Note unfiltered name but ffi_type_name filters. |
| 72 |
-#} |
| 73 |
{%- macro arg_list_ffi_decl(func) %} |
| 74 |
{%- for arg in func.arguments() %} |
| 75 |
{{ arg.type_().borrow()|ffi_type_name(ci) }}, |
| 76 |
{%- endfor %} |
| 77 |
{%- if func.has_rust_call_status_arg() %} |
| 78 |
ctypes.POINTER(_UniffiRustCallStatus),{% endif %} |
| 79 |
{% endmacro -%} |
| 80 |
|
| 81 |
{# |
| 82 |
# Setup function arguments by initializing default values. |
| 83 |
#} |
| 84 |
{%- macro setup_args(func) %} |
| 85 |
{%- for arg in func.arguments() %} |
| 86 |
{%- if let Some(literal) = arg.default_value() %} |
| 87 |
if {{ arg.name() }} is _DEFAULT: |
| 88 |
{{ arg.name() }} = {{ literal|literal_py(arg.as_type().borrow()) }} |
| 89 |
{%- endif %} |
| 90 |
{{ arg|check_lower_fn }}({{ arg.name() }}) |
| 91 |
{% endfor -%} |
| 92 |
{%- endmacro -%} |
| 93 |
|
| 94 |
{# |
| 95 |
# Exactly the same thing as `setup_args()` but with an extra 4 spaces of |
| 96 |
# indent so that it works with object methods. |
| 97 |
#} |
| 98 |
{%- macro setup_args_extra_indent(func) %} |
| 99 |
{%- for arg in func.arguments() %} |
| 100 |
{%- if let Some(literal) = arg.default_value() %} |
| 101 |
if {{ arg.name() }} is _DEFAULT: |
| 102 |
{{ arg.name() }} = {{ literal|literal_py(arg.as_type().borrow()) }} |
| 103 |
{%- endif %} |
| 104 |
{{ arg|check_lower_fn }}({{ arg.name() }}) |
| 105 |
{% endfor -%} |
| 106 |
{%- endmacro -%} |
| 107 |
|
| 108 |
{# |
| 109 |
# Macro to call methods |
| 110 |
#} |
| 111 |
{%- macro method_decl(py_method_name, meth) %} |
| 112 |
{% if meth.is_async() %} |
| 113 |
|
| 114 |
{%- match meth.return_type() %} |
| 115 |
{%- when Some(return_type) %} |
| 116 |
async def {{ py_method_name }}(self, {% call arg_list_decl(meth) %}) -> "{{ return_type|type_name }}": |
| 117 |
{%- when None %} |
| 118 |
async def {{ py_method_name }}(self, {% call arg_list_decl(meth) %}) -> None: |
| 119 |
{% endmatch %} |
| 120 |
|
| 121 |
{%- call docstring(meth, 8) %} |
| 122 |
{%- call setup_args_extra_indent(meth) %} |
| 123 |
return await _uniffi_rust_call_async( |
| 124 |
_UniffiLib.{{ meth.ffi_func().name() }}( |
| 125 |
self._uniffi_clone_pointer(), {% call arg_list_lowered(meth) %} |
| 126 |
), |
| 127 |
_UniffiLib.{{ meth.ffi_rust_future_poll(ci) }}, |
| 128 |
_UniffiLib.{{ meth.ffi_rust_future_complete(ci) }}, |
| 129 |
_UniffiLib.{{ meth.ffi_rust_future_free(ci) }}, |
| 130 |
# lift function |
| 131 |
{%- match meth.return_type() %} |
| 132 |
{%- when Some(return_type) %} |
| 133 |
{{ return_type|lift_fn }}, |
| 134 |
{%- when None %} |
| 135 |
lambda val: None, |
| 136 |
{% endmatch %} |
| 137 |
{% call error_ffi_converter(meth) %} |
| 138 |
) |
| 139 |
|
| 140 |
{%- else -%} |
| 141 |
{%- match meth.return_type() %} |
| 142 |
|
| 143 |
{%- when Some(return_type) %} |
| 144 |
|
| 145 |
def {{ py_method_name }}(self, {% call arg_list_decl(meth) %}) -> "{{ return_type|type_name }}": |
| 146 |
{%- call docstring(meth, 8) %} |
| 147 |
{%- call setup_args_extra_indent(meth) %} |
| 148 |
return {{ return_type|lift_fn }}( |
| 149 |
{% call to_ffi_call_with_prefix("self._uniffi_clone_pointer()", meth) %} |
| 150 |
) |
| 151 |
|
| 152 |
{%- when None %} |
| 153 |
|
| 154 |
def {{ py_method_name }}(self, {% call arg_list_decl(meth) %}) -> None: |
| 155 |
{%- call docstring(meth, 8) %} |
| 156 |
{%- call setup_args_extra_indent(meth) %} |
| 157 |
{% call to_ffi_call_with_prefix("self._uniffi_clone_pointer()", meth) %} |
| 158 |
{% endmatch %} |
| 159 |
{% endif %} |
| 160 |
|
| 161 |
{% endmacro %} |
| 162 |
|
| 163 |
{%- macro error_ffi_converter(func) %} |
| 164 |
# Error FFI converter |
| 165 |
{% match func.throws_type() %} |
| 166 |
{%- when Some(e) %} |
| 167 |
{%- match e -%} |
| 168 |
{%- when Type::Enum { .. } -%} |
| 169 |
{{ e|ffi_converter_name }}, |
| 170 |
{%- when Type::Object { .. } -%} |
| 171 |
{{ e|ffi_converter_name }}__as_error, |
| 172 |
{%- else %} |
| 173 |
# unsupported error type! |
| 174 |
{%- endmatch %} |
| 175 |
{%- when None %} |
| 176 |
None, |
| 177 |
{%- endmatch %} |
| 178 |
{% endmacro %} |