Add a configuration to allow removing `LocalizedError` conformation (#2319)

This commit is contained in:
Tony Li 2024-12-11 16:48:14 +13:00 коммит произвёл GitHub
Родитель 286de8d66e
Коммит f4b0824003
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: B5690EEEBB952194
4 изменённых файлов: 13 добавлений и 1 удалений

Просмотреть файл

@ -13,11 +13,12 @@ more likely to change than other configurations.
| `module_name` | `{namespace}`[^1] | The name of the Swift module containing the high-level foreign-language bindings. |
| `ffi_module_name` | `{module_name}FFI` | The name of the lower-level C module containing the FFI declarations. |
| `ffi_module_filename` | `{ffi_module_name}` | The filename stem for the lower-level C module containing the FFI declarations. |
| `generate_module_map` | `true` | Whether to generate a `.modulemap` file for the lower-level C module with FFI declarations. (ignored by `uniffi-bindgen-swift`) |
| `generate_module_map` | `true` | Whether to generate a `.modulemap` file for the lower-level C module with FFI declarations. (ignored by `uniffi-bindgen-swift`) |
| `omit_argument_labels` | `false` | Whether to omit argument labels in Swift function definitions. |
| `generate_immutable_records` | `false` | Whether to generate records with immutable fields (`let` instead of `var`). |
| `experimental_sendable_value_types` | `false` | Whether to mark value types as `Sendable'. |
| `custom_types` | | A map which controls how custom types are exposed to Swift. See the [custom types section of the manual](../udl/custom_types.md#custom-types-in-the-bindings-code) |
| `omit_localized_error_conformance` | `false` | Whether to make generated error types conform to `LocalizedError`. |
[^1]: `namespace` is the top-level namespace from your UDL file.

Просмотреть файл

@ -197,6 +197,7 @@ pub struct Config {
omit_argument_labels: Option<bool>,
generate_immutable_records: Option<bool>,
experimental_sendable_value_types: Option<bool>,
omit_localized_error_conformance: Option<bool>,
#[serde(default)]
custom_types: HashMap<String, CustomTypeConfig>,
}
@ -264,6 +265,11 @@ impl Config {
pub fn experimental_sendable_value_types(&self) -> bool {
self.experimental_sendable_value_types.unwrap_or(false)
}
// Whether to make generated error types conform to `LocalizedError`. Default: false.
pub fn omit_localized_error_conformance(&self) -> bool {
self.omit_localized_error_conformance.unwrap_or(false)
}
}
/// Generate UniFFI component bindings for Swift, as strings in memory.

Просмотреть файл

@ -90,8 +90,11 @@ public struct {{ ffi_converter_name }}: FfiConverterRustBuffer {
{% if !contains_object_references %}
extension {{ type_name }}: Equatable, Hashable {}
{% endif %}
{% if !config.omit_localized_error_conformance() %}
extension {{ type_name }}: Foundation.LocalizedError {
public var errorDescription: String? {
String(reflecting: self)
}
}
{% endif %}

Просмотреть файл

@ -174,11 +174,13 @@ public struct {{ ffi_converter_name }}: FfiConverter {
{# Objects as error #}
{%- if is_error %}
{% if !config.omit_localized_error_conformance() %}
extension {{ type_name }}: Foundation.LocalizedError {
public var errorDescription: String? {
String(reflecting: self)
}
}
{% endif %}
{# Due to some mismatches in the ffi converter mechanisms, errors are a RustBuffer holding a pointer #}
#if swift(>=5.8)