Debian Codegraph @

third_party/rust/uniffi_bindgen/src/bindings/kotlin/templates/ErrorTemplate.kt

raw
1 {%- let type_name = type_|type_name(ci) %}
2 {%- let ffi_converter_name = type_|ffi_converter_name %}
3 {%- let canonical_type_name = type_|canonical_name %}
4
5 {% if e.is_flat() %}
6 {%- call kt::docstring(e, 0) %}
7 sealed class {{ type_name }}(message: String): kotlin.Exception(message){% if contains_object_references %}, Disposable {% endif %} {
8 {% for variant in e.variants() -%}
9 {%- call kt::docstring(variant, 4) %}
10 class {{ variant|error_variant_name }}(message: String) : {{ type_name }}(message)
11 {% endfor %}
12
13 companion object ErrorHandler : UniffiRustCallStatusErrorHandler<{{ type_name }}> {
14 override fun lift(error_buf: RustBuffer.ByValue): {{ type_name }} = {{ ffi_converter_name }}.lift(error_buf)
15 }
16 }
17 {%- else %}
18 {%- call kt::docstring(e, 0) %}
19 sealed class {{ type_name }}: kotlin.Exception(){% if contains_object_references %}, Disposable {% endif %} {
20 {% for variant in e.variants() -%}
21 {%- call kt::docstring(variant, 4) %}
22 {%- let variant_name = variant|error_variant_name %}
23 class {{ variant_name }}(
24 {% for field in variant.fields() -%}
25 {%- call kt::docstring(field, 8) %}
26 val {% call kt::field_name(field, loop.index) %}: {{ field|type_name(ci) }}{% if loop.last %}{% else %}, {% endif %}
27 {% endfor -%}
28 ) : {{ type_name }}() {
29 override val message
30 get() = "{%- for field in variant.fields() %}{% call kt::field_name_unquoted(field, loop.index) %}=${ {% call kt::field_name(field, loop.index) %} }{% if !loop.last %}, {% endif %}{% endfor %}"
31 }
32 {% endfor %}
33
34 companion object ErrorHandler : UniffiRustCallStatusErrorHandler<{{ type_name }}> {
35 override fun lift(error_buf: RustBuffer.ByValue): {{ type_name }} = {{ ffi_converter_name }}.lift(error_buf)
36 }
37
38 {% if contains_object_references %}
39 @Suppress("UNNECESSARY_SAFE_CALL") // codegen is much simpler if we unconditionally emit safe calls here
40 override fun destroy() {
41 when(this) {
42 {%- for variant in e.variants() %}
43 is {{ type_name }}.{{ variant|error_variant_name }} -> {
44 {%- if variant.has_fields() %}
45 {% call kt::destroy_fields(variant) %}
46 {% else -%}
47 // Nothing to destroy
48 {%- endif %}
49 }
50 {%- endfor %}
51 }.let { /* this makes the `when` an expression, which ensures it is exhaustive */ }
52 }
53 {% endif %}
54 }
55 {%- endif %}
56
57 /**
58 * @suppress
59 */
60 public object {{ e|ffi_converter_name }} : FfiConverterRustBuffer<{{ type_name }}> {
61 override fun read(buf: ByteBuffer): {{ type_name }} {
62 {% if e.is_flat() %}
63 return when(buf.getInt()) {
64 {%- for variant in e.variants() %}
65 {{ loop.index }} -> {{ type_name }}.{{ variant|error_variant_name }}({{ Type::String.borrow()|read_fn }}(buf))
66 {%- endfor %}
67 else -> throw RuntimeException("invalid error enum value, something is very wrong!!")
68 }
69 {% else %}
70
71 return when(buf.getInt()) {
72 {%- for variant in e.variants() %}
73 {{ loop.index }} -> {{ type_name }}.{{ variant|error_variant_name }}({% if variant.has_fields() %}
74 {% for field in variant.fields() -%}
75 {{ field|read_fn }}(buf),
76 {% endfor -%}
77 {%- endif -%})
78 {%- endfor %}
79 else -> throw RuntimeException("invalid error enum value, something is very wrong!!")
80 }
81 {%- endif %}
82 }
83
84 override fun allocationSize(value: {{ type_name }}): ULong {
85 {%- if e.is_flat() %}
86 return 4UL
87 {%- else %}
88 return when(value) {
89 {%- for variant in e.variants() %}
90 is {{ type_name }}.{{ variant|error_variant_name }} -> (
91 // Add the size for the Int that specifies the variant plus the size needed for all fields
92 4UL
93 {%- for field in variant.fields() %}
94 + {{ field|allocation_size_fn }}(value.{% call kt::field_name(field, loop.index) %})
95 {%- endfor %}
96 )
97 {%- endfor %}
98 }
99 {%- endif %}
100 }
101
102 override fun write(value: {{ type_name }}, buf: ByteBuffer) {
103 when(value) {
104 {%- for variant in e.variants() %}
105 is {{ type_name }}.{{ variant|error_variant_name }} -> {
106 buf.putInt({{ loop.index }})
107 {%- for field in variant.fields() %}
108 {{ field|write_fn }}(value.{% call kt::field_name(field, loop.index) %}, buf)
109 {%- endfor %}
110 Unit
111 }
112 {%- endfor %}
113 }.let { /* this makes the `when` an expression, which ensures it is exhaustive */ }
114 }
115
116 }