| 1 |
{%- call swift::docstring(e, 0) %} |
| 2 |
public enum {{ type_name }}: Swift.Error { |
| 3 |
|
| 4 |
{% if e.is_flat() %} |
| 5 |
{% for variant in e.variants() %} |
| 6 |
{%- call swift::docstring(variant, 4) %} |
| 7 |
case {{ variant.name()|class_name }}(message: String) |
| 8 |
{% endfor %} |
| 9 |
|
| 10 |
{%- else %} |
| 11 |
{% for variant in e.variants() %} |
| 12 |
{%- call swift::docstring(variant, 4) %} |
| 13 |
case {{ variant.name()|class_name }}{% if variant.fields().len() > 0 %}( |
| 14 |
{%- call swift::field_list_decl(variant, variant.has_nameless_fields()) %} |
| 15 |
){% endif -%} |
| 16 |
{% endfor %} |
| 17 |
|
| 18 |
{%- endif %} |
| 19 |
} |
| 20 |
|
| 21 |
|
| 22 |
#if swift(>=5.8) |
| 23 |
@_documentation(visibility: private) |
| 24 |
#endif |
| 25 |
public struct {{ ffi_converter_name }}: FfiConverterRustBuffer { |
| 26 |
typealias SwiftType = {{ type_name }} |
| 27 |
|
| 28 |
public static func read(from buf: inout (data: Data, offset: Data.Index)) throws -> {{ type_name }} { |
| 29 |
let variant: Int32 = try readInt(&buf) |
| 30 |
switch variant { |
| 31 |
|
| 32 |
{% if e.is_flat() %} |
| 33 |
|
| 34 |
{% for variant in e.variants() %} |
| 35 |
case {{ loop.index }}: return .{{ variant.name()|class_name }}( |
| 36 |
message: try {{ Type::String.borrow()|read_fn }}(from: &buf) |
| 37 |
) |
| 38 |
{% endfor %} |
| 39 |
|
| 40 |
{% else %} |
| 41 |
|
| 42 |
{% for variant in e.variants() %} |
| 43 |
case {{ loop.index }}: return .{{ variant.name()|error_variant_swift_quoted }}{% if variant.has_fields() %}( |
| 44 |
{% for field in variant.fields() -%} |
| 45 |
{%- if variant.has_nameless_fields() -%} |
| 46 |
try {{ field|read_fn }}(from: &buf) |
| 47 |
{%- else -%} |
| 48 |
{{ field.name()|var_name }}: try {{ field|read_fn }}(from: &buf) |
| 49 |
{%- endif -%} |
| 50 |
{%- if !loop.last %}, {% endif %} |
| 51 |
{% endfor -%} |
| 52 |
){% endif -%} |
| 53 |
{% endfor %} |
| 54 |
|
| 55 |
{% endif -%} |
| 56 |
default: throw UniffiInternalError.unexpectedEnumCase |
| 57 |
} |
| 58 |
} |
| 59 |
|
| 60 |
public static func write(_ value: {{ type_name }}, into buf: inout [UInt8]) { |
| 61 |
switch value { |
| 62 |
|
| 63 |
{% if e.is_flat() %} |
| 64 |
|
| 65 |
{% for variant in e.variants() %} |
| 66 |
case .{{ variant.name()|class_name }}(_ /* message is ignored*/): |
| 67 |
writeInt(&buf, Int32({{ loop.index }})) |
| 68 |
{%- endfor %} |
| 69 |
|
| 70 |
{% else %} |
| 71 |
|
| 72 |
{% for variant in e.variants() %} |
| 73 |
{% if variant.has_fields() %} |
| 74 |
case let .{{ variant.name()|error_variant_swift_quoted }}({% for field in variant.fields() %}{%- call swift::field_name(field, loop.index) -%}{%- if loop.last -%}{%- else -%},{%- endif -%}{% endfor %}): |
| 75 |
writeInt(&buf, Int32({{ loop.index }})) |
| 76 |
{% for field in variant.fields() -%} |
| 77 |
{{ field|write_fn }}({% call swift::field_name(field, loop.index) %}, into: &buf) |
| 78 |
{% endfor -%} |
| 79 |
{% else %} |
| 80 |
case .{{ variant.name()|class_name }}: |
| 81 |
writeInt(&buf, Int32({{ loop.index }})) |
| 82 |
{% endif %} |
| 83 |
{%- endfor %} |
| 84 |
|
| 85 |
{%- endif %} |
| 86 |
} |
| 87 |
} |
| 88 |
} |
| 89 |
|
| 90 |
{# |
| 91 |
We always write these public functions just in case the error is used as |
| 92 |
an external type by another crate. |
| 93 |
#} |
| 94 |
#if swift(>=5.8) |
| 95 |
@_documentation(visibility: private) |
| 96 |
#endif |
| 97 |
public func {{ ffi_converter_name }}_lift(_ buf: RustBuffer) throws -> {{ type_name }} { |
| 98 |
return try {{ ffi_converter_name }}.lift(buf) |
| 99 |
} |
| 100 |
|
| 101 |
#if swift(>=5.8) |
| 102 |
@_documentation(visibility: private) |
| 103 |
#endif |
| 104 |
public func {{ ffi_converter_name }}_lower(_ value: {{ type_name }}) -> RustBuffer { |
| 105 |
return {{ ffi_converter_name }}.lower(value) |
| 106 |
} |
| 107 |
|
| 108 |
{% if !contains_object_references %} |
| 109 |
extension {{ type_name }}: Equatable, Hashable {} |
| 110 |
{% if config.generate_codable_conformance() %} |
| 111 |
extension {{ type_name }}: Codable {} |
| 112 |
{% endif %} |
| 113 |
{% endif %} |
| 114 |
|
| 115 |
{% if !config.omit_localized_error_conformance() %} |
| 116 |
extension {{ type_name }}: Foundation.LocalizedError { |
| 117 |
public var errorDescription: String? { |
| 118 |
String(reflecting: self) |
| 119 |
} |
| 120 |
} |
| 121 |
{% endif %} |
| 122 |
|
| 123 |
{% if config.generate_case_iterable_conformance() && !e.is_flat() && !e.contains_variant_fields() %} |
| 124 |
extension {{ type_name }}: CaseIterable {} |
| 125 |
{% endif %} |