Skip to content

Commit 3afb3c7

Browse files
committed
Add configuration to generate Swift error with lower camel case names
1 parent 8f063bf commit 3afb3c7

File tree

3 files changed

+21
-9
lines changed

3 files changed

+21
-9
lines changed

docs/manual/src/swift/configuration.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ more likely to change than other configurations.
1919
| `experimental_sendable_value_types` | `false` | Whether to mark value types as `Sendable'. |
2020
| `custom_types` | | A map which controls how custom types are exposed to Swift. See the [custom types section of the manual](../types/custom_types.md#custom-types-in-the-bindings-code) |
2121
| `omit_localized_error_conformance` | `false` | Whether to make generated error types conform to `LocalizedError`. |
22+
| `error_enum_use_lower_camel_case` | `false` | Whether to use lower camel case for error enum variants. |
2223

2324
[^1]: `namespace` is the top-level namespace from your UDL file.
2425

uniffi_bindgen/src/bindings/swift/gen_swift/mod.rs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,7 @@ pub struct Config {
198198
generate_immutable_records: Option<bool>,
199199
experimental_sendable_value_types: Option<bool>,
200200
omit_localized_error_conformance: Option<bool>,
201+
error_enum_use_lower_camel_case: Option<bool>,
201202
#[serde(default)]
202203
custom_types: HashMap<String, CustomTypeConfig>,
203204
}
@@ -270,6 +271,11 @@ impl Config {
270271
pub fn omit_localized_error_conformance(&self) -> bool {
271272
self.omit_localized_error_conformance.unwrap_or(false)
272273
}
274+
275+
/// Whether to use lower camel case for error enum variants. Default: false.
276+
pub fn error_enum_use_lower_camel_case(&self) -> bool {
277+
self.error_enum_use_lower_camel_case.unwrap_or(false)
278+
}
273279
}
274280

275281
/// Generate UniFFI component bindings for Swift, as strings in memory.
@@ -755,8 +761,12 @@ pub mod filters {
755761
}
756762

757763
/// Same as enum_variant_swift_quoted, because error types are exported as Swift enums.
758-
pub fn error_variant_swift_quoted(nm: &str) -> Result<String, rinja::Error> {
759-
Ok(quote_general_keyword(oracle().enum_variant_name(nm)))
764+
pub fn error_variant_swift_quoted(nm: &str, use_class_name: &bool) -> Result<String, rinja::Error> {
765+
if *use_class_name {
766+
Ok(quote_general_keyword(oracle().class_name(nm)))
767+
} else {
768+
Ok(quote_general_keyword(oracle().enum_variant_name(nm)))
769+
}
760770
}
761771

762772
/// Get the idiomatic Swift rendering of an FFI callback function name

uniffi_bindgen/src/bindings/swift/templates/ErrorTemplate.swift

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
11
{%- call swift::docstring(e, 0) %}
2+
{% let use_class_name = config.error_enum_use_lower_camel_case() == false %}
23
public enum {{ type_name }} {
34

45
{% if e.is_flat() %}
56
{% for variant in e.variants() %}
67
{%- call swift::docstring(variant, 4) %}
7-
case {{ variant.name()|error_variant_swift_quoted }}(message: String)
8+
case {{ variant.name()|error_variant_swift_quoted(use_class_name) }}(message: String)
89
{% endfor %}
910

1011
{%- else %}
1112
{% for variant in e.variants() %}
1213
{%- call swift::docstring(variant, 4) %}
13-
case {{ variant.name()|error_variant_swift_quoted }}{% if variant.fields().len() > 0 %}(
14+
case {{ variant.name()|error_variant_swift_quoted(use_class_name) }}{% if variant.fields().len() > 0 %}(
1415
{%- call swift::field_list_decl(variant, variant.has_nameless_fields()) %}
1516
){% endif -%}
1617
{% endfor %}
@@ -32,15 +33,15 @@ public struct {{ ffi_converter_name }}: FfiConverterRustBuffer {
3233
{% if e.is_flat() %}
3334

3435
{% for variant in e.variants() %}
35-
case {{ loop.index }}: return .{{ variant.name()|error_variant_swift_quoted }}(
36+
case {{ loop.index }}: return .{{ variant.name()|error_variant_swift_quoted(use_class_name) }}(
3637
message: try {{ Type::String.borrow()|read_fn }}(from: &buf)
3738
)
3839
{% endfor %}
3940

4041
{% else %}
4142

4243
{% for variant in e.variants() %}
43-
case {{ loop.index }}: return .{{ variant.name()|error_variant_swift_quoted }}{% if variant.has_fields() %}(
44+
case {{ loop.index }}: return .{{ variant.name()|error_variant_swift_quoted(use_class_name) }}{% if variant.has_fields() %}(
4445
{% for field in variant.fields() -%}
4546
{%- if variant.has_nameless_fields() -%}
4647
try {{ field|read_fn }}(from: &buf)
@@ -63,21 +64,21 @@ public struct {{ ffi_converter_name }}: FfiConverterRustBuffer {
6364
{% if e.is_flat() %}
6465

6566
{% for variant in e.variants() %}
66-
case .{{ variant.name()|error_variant_swift_quoted }}(_ /* message is ignored*/):
67+
case .{{ variant.name()|error_variant_swift_quoted(use_class_name) }}(_ /* message is ignored*/):
6768
writeInt(&buf, Int32({{ loop.index }}))
6869
{%- endfor %}
6970

7071
{% else %}
7172

7273
{% for variant in e.variants() %}
7374
{% 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+
case let .{{ variant.name()|error_variant_swift_quoted(use_class_name) }}({% for field in variant.fields() %}{%- call swift::field_name(field, loop.index) -%}{%- if loop.last -%}{%- else -%},{%- endif -%}{% endfor %}):
7576
writeInt(&buf, Int32({{ loop.index }}))
7677
{% for field in variant.fields() -%}
7778
{{ field|write_fn }}({% call swift::field_name(field, loop.index) %}, into: &buf)
7879
{% endfor -%}
7980
{% else %}
80-
case .{{ variant.name()|error_variant_swift_quoted }}:
81+
case .{{ variant.name()|error_variant_swift_quoted(use_class_name) }}:
8182
writeInt(&buf, Int32({{ loop.index }}))
8283
{% endif %}
8384
{%- endfor %}

0 commit comments

Comments
 (0)