summaryrefslogtreecommitdiffstats
path: root/third_party/rust/uniffi_bindgen/src/bindings/swift/templates/CustomType.swift
blob: 504cadfc1d5635e441fe571dd3afdbdff25bd563 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
{%- let ffi_type_name=builtin|ffi_type|ffi_type_name %}
{%- match config.custom_types.get(name.as_str())  %}
{%- when None %}
{#- No config, just forward all methods to our builtin type #}
/**
 * Typealias from the type name used in the UDL file to the builtin type.  This
 * is needed because the UDL type name is used in function/method signatures.
 */
public typealias {{ name }} = {{ builtin|type_name }}
public struct FfiConverterType{{ name }}: FfiConverter {
    public static func read(from buf: inout (data: Data, offset: Data.Index)) throws -> {{ name }} {
        return try {{ builtin|read_fn }}(from: &buf)
    }

    public static func write(_ value: {{ name }}, into buf: inout [UInt8]) {
        return {{ builtin|write_fn }}(value, into: &buf)
    }

    public static func lift(_ value: {{ ffi_type_name }}) throws -> {{ name }} {
        return try {{ builtin|lift_fn }}(value)
    }

    public static func lower(_ value: {{ name }}) -> {{ ffi_type_name }} {
        return {{ builtin|lower_fn }}(value)
    }
}

{%- when Some with (config) %}

{# When the config specifies a different type name, create a typealias for it #}
{%- match config.type_name %}
{%- when Some with (concrete_type_name) %}
/**
 * Typealias from the type name used in the UDL file to the custom type.  This
 * is needed because the UDL type name is used in function/method signatures.
 */
public typealias {{ name }} = {{ concrete_type_name }}
{%- else %}
{%- endmatch %}

{%- match config.imports %}
{%- when Some(imports) %}
{%- for import_name in imports %}
{{ self.add_import(import_name) }}
{%- endfor %}
{%- else %}
{%- endmatch %}

public struct FfiConverterType{{ name }}: FfiConverter {
    {#- Custom type config supplied, use it to convert the builtin type #}

    public static func read(from buf: inout (data: Data, offset: Data.Index)) throws -> {{ name }} {
        let builtinValue = try {{ builtin|read_fn }}(from: &buf)
        return {{ config.into_custom.render("builtinValue") }}
    }

    public static func write(_ value: {{ name }}, into buf: inout [UInt8]) {
        let builtinValue = {{ config.from_custom.render("value") }}
        return {{ builtin|write_fn }}(builtinValue, into: &buf)
    }

    public static func lift(_ value: {{ ffi_type_name }}) throws -> {{ name }} {
        let builtinValue = try {{ builtin|lift_fn }}(value)
        return {{ config.into_custom.render("builtinValue") }}
    }

    public static func lower(_ value: {{ name }}) -> {{ ffi_type_name }} {
        let builtinValue = {{ config.from_custom.render("value") }}
        return {{ builtin|lower_fn }}(builtinValue)
    }
}

{%- endmatch %}

{#
We always write these public functions just incase the type is used as
an external type by another crate.
#}
public func FfiConverterType{{ name }}_lift(_ value: {{ ffi_type_name }}) throws -> {{ name }} {
    return try FfiConverterType{{ name }}.lift(value)
}

public func FfiConverterType{{ name }}_lower(_ value: {{ name }}) -> {{ ffi_type_name }} {
    return FfiConverterType{{ name }}.lower(value)
}