| 1 |
// Note that we don't yet support `indirect` for enums. |
| 2 |
// See https://github.com/mozilla/uniffi-rs/issues/396 for further discussion. |
| 3 |
{%- call swift::docstring(e, 0) %} |
| 4 |
{% match e.variant_discr_type() %} |
| 5 |
{% when None %} |
| 6 |
public enum {{ type_name }} { |
| 7 |
{% for variant in e.variants() %} |
| 8 |
{%- call swift::docstring(variant, 4) %} |
| 9 |
case {{ variant.name()|enum_variant_swift_quoted }}{% if variant.fields().len() > 0 %}( |
| 10 |
{%- call swift::field_list_decl(variant, variant.has_nameless_fields()) %} |
| 11 |
){% endif -%} |
| 12 |
{% endfor %} |
| 13 |
} |
| 14 |
{% when Some(variant_discr_type) %} |
| 15 |
public enum {{ type_name }} : {{ variant_discr_type|type_name }} { |
| 16 |
{% for variant in e.variants() %} |
| 17 |
{%- call swift::docstring(variant, 4) %} |
| 18 |
case {{ variant.name()|enum_variant_swift_quoted }} = {{ e|variant_discr_literal(loop.index0) }}{% if variant.fields().len() > 0 %}( |
| 19 |
{%- call swift::field_list_decl(variant, variant.has_nameless_fields()) %} |
| 20 |
){% endif -%} |
| 21 |
{% endfor %} |
| 22 |
} |
| 23 |
{% endmatch %} |
| 24 |
|
| 25 |
#if compiler(>=6) |
| 26 |
extension {{ type_name }}: Sendable {} |
| 27 |
#endif |
| 28 |
|
| 29 |
#if swift(>=5.8) |
| 30 |
@_documentation(visibility: private) |
| 31 |
#endif |
| 32 |
public struct {{ ffi_converter_name }}: FfiConverterRustBuffer { |
| 33 |
typealias SwiftType = {{ type_name }} |
| 34 |
|
| 35 |
public static func read(from buf: inout (data: Data, offset: Data.Index)) throws -> {{ type_name }} { |
| 36 |
let variant: Int32 = try readInt(&buf) |
| 37 |
switch variant { |
| 38 |
{% for variant in e.variants() %} |
| 39 |
case {{ loop.index }}: return .{{ variant.name()|enum_variant_swift_quoted }}{% if variant.has_fields() %}( |
| 40 |
{%- for field in variant.fields() %} |
| 41 |
{%- if variant.has_nameless_fields() -%} |
| 42 |
try {{ field|read_fn }}(from: &buf) |
| 43 |
{%- else -%} |
| 44 |
{{ field.name()|arg_name }}: try {{ field|read_fn }}(from: &buf) |
| 45 |
{%- endif -%} |
| 46 |
{%- if !loop.last %}, {% endif %} |
| 47 |
{%- endfor %} |
| 48 |
){%- endif %} |
| 49 |
{% endfor %} |
| 50 |
default: throw UniffiInternalError.unexpectedEnumCase |
| 51 |
} |
| 52 |
} |
| 53 |
|
| 54 |
public static func write(_ value: {{ type_name }}, into buf: inout [UInt8]) { |
| 55 |
switch value { |
| 56 |
{% for variant in e.variants() %} |
| 57 |
{% if variant.has_fields() %} |
| 58 |
case let .{{ variant.name()|enum_variant_swift_quoted }}({% for field in variant.fields() %}{%- call swift::field_name(field, loop.index) -%}{%- if loop.last -%}{%- else -%},{%- endif -%}{% endfor %}): |
| 59 |
writeInt(&buf, Int32({{ loop.index }})) |
| 60 |
{% for field in variant.fields() -%} |
| 61 |
{{ field|write_fn }}({% call swift::field_name(field, loop.index) %}, into: &buf) |
| 62 |
{% endfor -%} |
| 63 |
{% else %} |
| 64 |
case .{{ variant.name()|enum_variant_swift_quoted }}: |
| 65 |
writeInt(&buf, Int32({{ loop.index }})) |
| 66 |
{% endif %} |
| 67 |
{%- endfor %} |
| 68 |
} |
| 69 |
} |
| 70 |
} |
| 71 |
|
| 72 |
{# |
| 73 |
We always write these public functions just in case the enum is used as |
| 74 |
an external type by another crate. |
| 75 |
#} |
| 76 |
#if swift(>=5.8) |
| 77 |
@_documentation(visibility: private) |
| 78 |
#endif |
| 79 |
public func {{ ffi_converter_name }}_lift(_ buf: RustBuffer) throws -> {{ type_name }} { |
| 80 |
return try {{ ffi_converter_name }}.lift(buf) |
| 81 |
} |
| 82 |
|
| 83 |
#if swift(>=5.8) |
| 84 |
@_documentation(visibility: private) |
| 85 |
#endif |
| 86 |
public func {{ ffi_converter_name }}_lower(_ value: {{ type_name }}) -> RustBuffer { |
| 87 |
return {{ ffi_converter_name }}.lower(value) |
| 88 |
} |
| 89 |
|
| 90 |
{% if !contains_object_references %} |
| 91 |
extension {{ type_name }}: Equatable, Hashable {} |
| 92 |
{% if config.generate_codable_conformance() %} |
| 93 |
extension {{ type_name }}: Codable {} |
| 94 |
{% endif %} |
| 95 |
{% endif %} |
| 96 |
|
| 97 |
{% if config.generate_case_iterable_conformance() && !e.contains_variant_fields() %} |
| 98 |
extension {{ type_name }}: CaseIterable {} |
| 99 |
{% endif %} |