| 1 |
{# |
| 2 |
# Python has a built-in `enum` module which is nice to use, but doesn't support |
| 3 |
# variants with associated data. So, we switch here, and generate a stdlib `enum` |
| 4 |
# when none of the variants have associated data, or a generic nested-class |
| 5 |
# construct when they do. |
| 6 |
#} |
| 7 |
{% if e.is_flat() %} |
| 8 |
|
| 9 |
class {{ type_name }}(enum.Enum): |
| 10 |
{%- call py::docstring(e, 4) %} |
| 11 |
{%- for variant in e.variants() %} |
| 12 |
{{ variant.name() }} = {{ e|variant_discr_literal(loop.index0) }} |
| 13 |
{%- call py::docstring(variant, 4) %} |
| 14 |
{% endfor %} |
| 15 |
{% else %} |
| 16 |
|
| 17 |
class {{ type_name }}: |
| 18 |
{%- call py::docstring(e, 4) %} |
| 19 |
def __init__(self): |
| 20 |
raise RuntimeError("{{ type_name }} cannot be instantiated directly") |
| 21 |
|
| 22 |
# Each enum variant is a nested class of the enum itself. |
| 23 |
{% for variant in e.variants() -%} |
| 24 |
class {{ variant.name() }}: |
| 25 |
{%- call py::docstring(variant, 8) %} |
| 26 |
|
| 27 |
{%- if variant.has_nameless_fields() %} |
| 28 |
def __init__(self, *values): |
| 29 |
if len(values) != {{ variant.fields().len() }}: |
| 30 |
raise TypeError(f"Expected {{ variant.fields().len() }} arguments, found {len(values)}") |
| 31 |
self._values = values |
| 32 |
|
| 33 |
def __getitem__(self, index): |
| 34 |
return self._values[index] |
| 35 |
|
| 36 |
def __str__(self): |
| 37 |
return f"{{ type_name }}.{{ variant.name() }}{self._values!r}" |
| 38 |
|
| 39 |
def __eq__(self, other): |
| 40 |
if not other.is_{{ variant.name() }}(): |
| 41 |
return False |
| 42 |
return self._values == other._values |
| 43 |
|
| 44 |
{%- else -%} |
| 45 |
{%- for field in variant.fields() %} |
| 46 |
{{ field.name() }}: "{{ field|type_name }}" |
| 47 |
{%- call py::docstring(field, 8) %} |
| 48 |
{%- endfor %} |
| 49 |
|
| 50 |
def __init__(self,{% for field in variant.fields() %}{{ field.name() }}: "{{- field|type_name }}"{% if loop.last %}{% else %}, {% endif %}{% endfor %}): |
| 51 |
{%- if variant.has_fields() %} |
| 52 |
{%- for field in variant.fields() %} |
| 53 |
self.{{ field.name() }} = {{ field.name() }} |
| 54 |
{%- endfor %} |
| 55 |
{%- else %} |
| 56 |
pass |
| 57 |
{%- endif %} |
| 58 |
|
| 59 |
def __str__(self): |
| 60 |
return "{{ type_name }}.{{ variant.name() }}({% for field in variant.fields() %}{{ field.name() }}={}{% if loop.last %}{% else %}, {% endif %}{% endfor %})".format({% for field in variant.fields() %}self.{{ field.name() }}{% if loop.last %}{% else %}, {% endif %}{% endfor %}) |
| 61 |
|
| 62 |
def __eq__(self, other): |
| 63 |
if not other.is_{{ variant.name() }}(): |
| 64 |
return False |
| 65 |
{%- for field in variant.fields() %} |
| 66 |
if self.{{ field.name() }} != other.{{ field.name() }}: |
| 67 |
return False |
| 68 |
{%- endfor %} |
| 69 |
return True |
| 70 |
{% endif %} |
| 71 |
{% endfor %} |
| 72 |
|
| 73 |
# For each variant, we have `is_NAME` and `is_name` methods for easily checking |
| 74 |
# whether an instance is that variant. |
| 75 |
{% for variant in e.variants() -%} |
| 76 |
def is_{{ variant.name() }}(self) -> bool: |
| 77 |
return isinstance(self, {{ type_name }}.{{ variant.name() }}) |
| 78 |
|
| 79 |
{#- We used to think we used `is_NAME` but did `is_name` instead. In #2270 we decided to do both. #} |
| 80 |
{%- if variant.name() != variant.name().to_snake_case() %} |
| 81 |
def is_{{ variant.name().to_snake_case() }}(self) -> bool: |
| 82 |
return isinstance(self, {{ type_name }}.{{ variant.name() }}) |
| 83 |
{%- endif %} |
| 84 |
{% endfor %} |
| 85 |
|
| 86 |
# Now, a little trick - we make each nested variant class be a subclass of the main |
| 87 |
# enum class, so that method calls and instance checks etc will work intuitively. |
| 88 |
# We might be able to do this a little more neatly with a metaclass, but this'll do. |
| 89 |
{% for variant in e.variants() -%} |
| 90 |
{{ type_name }}.{{ variant.name() }} = type("{{ type_name }}.{{ variant.name() }}", ({{ type_name }}.{{variant.name()}}, {{ type_name }},), {}) # type: ignore |
| 91 |
{% endfor %} |
| 92 |
|
| 93 |
{% endif %} |
| 94 |
|
| 95 |
class {{ ffi_converter_name }}(_UniffiConverterRustBuffer): |
| 96 |
@staticmethod |
| 97 |
def read(buf): |
| 98 |
variant = buf.read_i32() |
| 99 |
|
| 100 |
{%- for variant in e.variants() %} |
| 101 |
if variant == {{ loop.index }}: |
| 102 |
{%- if e.is_flat() %} |
| 103 |
return {{ type_name }}.{{variant.name()}} |
| 104 |
{%- else %} |
| 105 |
return {{ type_name }}.{{variant.name()}}( |
| 106 |
{%- for field in variant.fields() %} |
| 107 |
{{ field|read_fn }}(buf), |
| 108 |
{%- endfor %} |
| 109 |
) |
| 110 |
{%- endif %} |
| 111 |
{%- endfor %} |
| 112 |
raise InternalError("Raw enum value doesn't match any cases") |
| 113 |
|
| 114 |
@staticmethod |
| 115 |
def check_lower(value): |
| 116 |
{%- if e.variants().is_empty() %} |
| 117 |
pass |
| 118 |
{%- else %} |
| 119 |
{%- for variant in e.variants() %} |
| 120 |
{%- if e.is_flat() %} |
| 121 |
if value == {{ type_name }}.{{ variant.name() }}: |
| 122 |
{%- else %} |
| 123 |
if value.is_{{ variant.name() }}(): |
| 124 |
{%- endif %} |
| 125 |
{%- for field in variant.fields() %} |
| 126 |
{%- if variant.has_nameless_fields() %} |
| 127 |
{{ field|check_lower_fn }}(value._values[{{ loop.index0 }}]) |
| 128 |
{%- else %} |
| 129 |
{{ field|check_lower_fn }}(value.{{ field.name() }}) |
| 130 |
{%- endif %} |
| 131 |
{%- endfor %} |
| 132 |
return |
| 133 |
{%- endfor %} |
| 134 |
raise ValueError(value) |
| 135 |
{%- endif %} |
| 136 |
|
| 137 |
@staticmethod |
| 138 |
def write(value, buf): |
| 139 |
{%- for variant in e.variants() %} |
| 140 |
{%- if e.is_flat() %} |
| 141 |
if value == {{ type_name }}.{{ variant.name() }}: |
| 142 |
buf.write_i32({{ loop.index }}) |
| 143 |
{%- else %} |
| 144 |
if value.is_{{ variant.name() }}(): |
| 145 |
buf.write_i32({{ loop.index }}) |
| 146 |
{%- for field in variant.fields() %} |
| 147 |
{%- if variant.has_nameless_fields() %} |
| 148 |
{{ field|write_fn }}(value._values[{{ loop.index0 }}], buf) |
| 149 |
{%- else %} |
| 150 |
{{ field|write_fn }}(value.{{ field.name() }}, buf) |
| 151 |
{%- endif %} |
| 152 |
{%- endfor %} |
| 153 |
{%- endif %} |
| 154 |
{%- endfor %} |
| 155 |
|