alef 0.115.7

Opinionated polyglot binding generator for Rust libraries
Documentation
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
use crate::backends::swift::naming::swift_rust_shim_ident as swift_case_ident;
use crate::backends::swift::type_map::SwiftMapper;
use crate::codegen::type_mapper::TypeMapper;
use crate::core::ir::{ErrorDef, ErrorVariant, FieldDef, PrimitiveType, TypeRef};
use heck::ToLowerCamelCase;
use std::collections::BTreeSet;

/// The Swift name of the enum for `error`: `{module_name}Error` when the Rust error is named
/// `Error`, because `public enum Error: Error` would parse as a circular raw-type binding.
pub(super) fn swift_error_name(error: &ErrorDef, module_name: &str) -> String {
    if error.name == "Error" {
        format!("{module_name}Error")
    } else {
        error.name.clone()
    }
}

/// Whether the Rust error has a `status_code() -> u16` method.
fn has_status_code_method(error: &ErrorDef) -> bool {
    error.methods.iter().any(|method| {
        method.name == "status_code"
            && !method.is_static
            && !method.is_async
            && method.params.is_empty()
            && matches!(method.return_type, TypeRef::Primitive(PrimitiveType::U16))
    })
}

/// Whether `field` holds the HTTP status of its variant.
fn is_status_field(field: &FieldDef) -> bool {
    field.name == "status" || field.name == "status_code" || field.name.to_lower_camel_case() == "statusCode"
}

/// The error as the Swift enum declares it.
///
/// A Rust error with a `status_code` method reports the status of every variant, also of a
/// variant whose fields hold none (`RateLimited` is always 429). The Swift enum cannot compute
/// that status, so every variant without a status field gets a `status: UInt16` field. The
/// error converted from the bridge fills it and `statusCode` reads it back.
fn swift_view(error: &ErrorDef) -> ErrorDef {
    let mut view = error.clone();
    let has_status = has_status_code_method(error);
    for variant in &mut view.variants {
        let has_source = variant.has_source;
        variant.fields.retain_mut(|field| {
            if !field.binding_excluded {
                return true;
            }
            // ~keep Nullable error causes shipped as Swift String? associated values. Keep
            // their case arity while transporting no value from an explicitly skipped source.
            if has_source
                && field.name == "source"
                && (field.optional || matches!(field.ty, TypeRef::Optional(_)))
                && !field.sensitive
            {
                field.ty = TypeRef::String;
                field.optional = true;
                return true;
            }
            false
        });
        if !has_status || variant.fields.iter().any(is_status_field) {
            continue;
        }
        variant.is_unit = false;
        variant.fields.push(FieldDef {
            name: "status".to_string(),
            ty: TypeRef::Primitive(PrimitiveType::U16),
            ..FieldDef::default()
        });
    }
    view
}

/// One associated value of a Swift error case.
struct AssociatedValue<'a> {
    label: String,
    swift_type: String,
    /// The Rust field, or `None` for the synthetic `message` value.
    field: Option<&'a FieldDef>,
}

/// The associated values of the Swift case for `variant`, in declaration order. A variant without
/// a `message` field gets a leading `message: String`.
fn associated_values<'a>(variant: &'a ErrorVariant, mapper: &SwiftMapper) -> Vec<AssociatedValue<'a>> {
    let mut values: Vec<AssociatedValue<'a>> = Vec::with_capacity(variant.fields.len() + 1);
    let mut seen_message = false;
    let mut labels: BTreeSet<String> = BTreeSet::new();
    for (idx, f) in variant.fields.iter().enumerate() {
        let already_optional = matches!(&f.ty, TypeRef::Optional(_));
        let ty_str = mapper.map_type(&f.ty);
        let ty_with_opt = if f.optional && !already_optional {
            format!("{ty_str}?")
        } else {
            ty_str
        };
        let mut label = super::enums::swift_associated_label(&f.name, idx);
        while labels.contains(&label) {
            label = format!("{label}{idx}");
        }
        labels.insert(label.clone());
        if label == "message" {
            seen_message = true;
        }
        values.push(AssociatedValue {
            label,
            swift_type: ty_with_opt,
            field: Some(f),
        });
    }
    if !seen_message {
        values.insert(
            0,
            AssociatedValue {
                label: "message".to_string(),
                swift_type: "String".to_string(),
                field: None,
            },
        );
    }
    values
}

/// Emits a Swift `Swift.Error`-conforming `public enum` for the given `ErrorDef`.
///
/// When the Rust error type is named `Error`, Swift would parse `public enum Error: Error`
/// as a circular raw-type binding rather than protocol conformance. In that case the enum
/// is renamed to `{module_name}Error` (e.g. `SampleLanguagePackError`) to avoid the
/// clash. The protocol reference is always qualified as `Swift.Error` for clarity.
pub(super) fn emit_error(error: &ErrorDef, module_name: &str, out: &mut String, mapper: &SwiftMapper) {
    let name = swift_error_name(error, module_name);
    let error = &swift_view(error);
    super::client::emit_doc_comment(&error.doc, "", out);
    out.push_str(&crate::backends::swift::template_env::render(
        "error_enum_header.jinja",
        crate::alef_context! {
            name => &name,
        },
    ));
    for variant in &error.variants {
        super::client::emit_doc_comment(&variant.doc, "    ", out);
        let case_name = super::enums::swift_variant_case(&variant.name);
        if variant.is_unit || variant.fields.is_empty() {
            out.push_str(&crate::backends::swift::template_env::render(
                "error_case.jinja",
                crate::alef_context! {
                    case_name => &case_name,
                },
            ));
        } else {
            let assoc: Vec<String> = associated_values(variant, mapper)
                .iter()
                .map(|value| format!("{}: {}", value.label, value.swift_type))
                .collect();
            out.push_str(&crate::backends::swift::template_env::render(
                "error_case_with_data.jinja",
                crate::alef_context! {
                    case_name => &case_name,
                    associated_values => assoc.join(", "),
                },
            ));
        }
    }
    let has_validation_variant = error
        .variants
        .iter()
        .any(|v| super::enums::swift_variant_case(&v.name) == "validation");
    if !has_validation_variant {
        out.push_str("    /// Synthetic case raised when a Rust serde unit-enum raw value cannot\n");
        out.push_str("    /// be mapped to a known Swift case during DTO unmarshaling.\n");
        out.push_str("    case validation(message: String, source: String)\n");
    }
    out.push_str("}\n");
    description::emit(error, &name, mapper, out);
    if !error.methods.is_empty() {
        out.push('\n');
        let mut properties = String::new();
        for method in &error.methods {
            let prop_name = swift_case_ident(&method.name.to_lower_camel_case());
            let return_ty = super::overloads::swift_type_name(&method.return_type);
            let default_val = swift_default_for_type(&method.return_type);
            let mut cases = String::new();
            for variant in &error.variants {
                let case_name = super::enums::swift_variant_case(&variant.name);
                let field_match = variant.fields.iter().find(|f| {
                    let camel = f.name.to_lower_camel_case();
                    let prop_snake = method.name.as_str();
                    camel == prop_name
                        || f.name == prop_snake
                        || (prop_snake == "status_code" && (f.name == "status" || camel == "status"))
                });
                let wildcard = if variant.is_unit || variant.fields.is_empty() {
                    String::new()
                } else {
                    let mut args: Vec<String> = variant
                        .fields
                        .iter()
                        .enumerate()
                        .map(|(i, f)| {
                            let label = super::enums::swift_associated_label(&f.name, i);
                            if let Some(fm) = &field_match
                                && fm.name == f.name
                            {
                                return format!("{label}: let matched");
                            }
                            format!("{label}: _")
                        })
                        .collect();
                    let has_message_field = variant.fields.iter().any(|f| f.name == "message");
                    if !has_message_field {
                        args.insert(0, "message: _".to_string());
                    }
                    format!("({})", args.join(", "))
                };
                let ret_expr = if field_match.is_some() && !variant.is_unit && !variant.fields.is_empty() {
                    "matched".to_string()
                } else {
                    default_val.clone()
                };
                cases.push_str(&crate::backends::swift::template_env::render(
                    "swift_error_property_case.swift.jinja",
                    crate::alef_context! {
                        case_name => &case_name,
                        wildcard => &wildcard,
                        return_expression => &ret_expr,
                    },
                ));
            }
            if !has_validation_variant {
                cases.push_str(&crate::backends::swift::template_env::render(
                    "swift_error_property_case.swift.jinja",
                    crate::alef_context! {
                        case_name => "validation",
                        wildcard => "(message: _, source: _)",
                        return_expression => &default_val,
                    },
                ));
            }
            properties.push_str(&crate::backends::swift::template_env::render(
                "swift_error_property.swift.jinja",
                crate::alef_context! {
                    property_name => &prop_name,
                    return_type => &return_ty,
                    cases => cases,
                },
            ));
        }
        out.push_str(&crate::backends::swift::template_env::render(
            "swift_error_extension.swift.jinja",
            crate::alef_context! {
                name => &name,
                properties => properties,
            },
        ));
    }
}

/// Returns the Swift zero/default literal for a given `TypeRef`.
pub(super) fn swift_default_for_type(ty: &TypeRef) -> String {
    match ty {
        TypeRef::Primitive(p) => {
            use crate::core::ir::PrimitiveType;
            match p {
                PrimitiveType::Bool => "false".to_string(),
                _ => "0".to_string(),
            }
        }
        TypeRef::String => "\"\"".to_string(),
        TypeRef::Optional(_) => "nil".to_string(),
        _ => "nil".to_string(),
    }
}

pub(crate) fn bridge_payload_supported(ty: &TypeRef) -> bool {
    match ty {
        TypeRef::Primitive(_) | TypeRef::String | TypeRef::Path | TypeRef::Char => true,
        TypeRef::Optional(inner) | TypeRef::Vec(inner) => bridge_payload_supported(inner),
        TypeRef::Map(key, value) => matches!(key.as_ref(), TypeRef::String) && bridge_payload_supported(value),
        _ => false,
    }
}

/// The Swift expression that gives a case value its place in a conversion from the bridge error,
/// or `None` when its payload cannot be transported without inventing data. ~keep
///
/// `message`, `status` and `retryAfterMs` are the constants the conversion reads from the bridge
/// error before its `switch`.
fn bridge_value_expression(value: &AssociatedValue<'_>, error: &ErrorDef) -> Option<String> {
    let Some(field) = value.field else {
        return Some("message".to_string());
    };
    if field.binding_excluded {
        return Some("nil".to_string());
    }
    if is_status_field(field) && has_status_code_method(error) {
        let (inner, optional) = match &field.ty {
            TypeRef::Optional(inner) => (inner.as_ref(), true),
            ty => (ty, field.optional),
        };
        if !matches!(inner, TypeRef::Primitive(p) if !matches!(p, PrimitiveType::Bool | PrimitiveType::F32 | PrimitiveType::F64))
        {
            return None;
        }
        let integer = SwiftMapper.map_type(inner);
        return Some(if optional {
            format!("status < 0 ? nil : {integer}(clamping: status)")
        } else {
            format!("{integer}(clamping: status)")
        });
    }
    if field.name == "retry_after"
        && error.methods.iter().any(|method| {
            method.name == "retry_after" && !method.is_static && !method.is_async && method.params.is_empty()
        })
    {
        match crate::codegen::error_gen::duration_shape(&field.ty) {
            Some(crate::codegen::error_gen::DurationShape::Optional) => {
                return Some("retryAfterMs < 0 ? nil : Duration.milliseconds(retryAfterMs)".to_string());
            }
            Some(crate::codegen::error_gen::DurationShape::Bare) if field.optional => {
                return Some("retryAfterMs < 0 ? nil : Duration.milliseconds(retryAfterMs)".to_string());
            }
            Some(crate::codegen::error_gen::DurationShape::Bare) => {
                return Some("Duration.milliseconds(max(retryAfterMs, 0))".to_string());
            }
            None => {}
        }
    }
    bridge_payload_supported(&field.ty).then(|| {
        let key = serde_json::to_string(&field.name).expect("field names are strings");
        format!(
            "try alefBridgeErrorField(error, {key}, as: ({}).self)",
            value.swift_type
        )
    })
}

/// The Swift expression that builds the case for `variant` from the bridge error constants, or
/// `None` when a value cannot be transported without inventing data. ~keep
fn bridge_case_expression(variant: &ErrorVariant, mapper: &SwiftMapper, error: &ErrorDef) -> Option<String> {
    let case_name = super::enums::swift_variant_case(&variant.name);
    if variant.is_unit || variant.fields.is_empty() {
        return Some(format!(".{case_name}"));
    }
    let mut arguments: Vec<String> = Vec::new();
    for value in associated_values(variant, mapper) {
        arguments.push(format!("{}: {}", value.label, bridge_value_expression(&value, error)?));
    }
    Some(format!(".{case_name}({})", arguments.join(", ")))
}

/// Emits, for every error the bridge reports, the conversion from the error that the Rust bridge
/// throws, and the helpers that every bridged call goes through.
///
/// The conversion reads the kind, the HTTP status and the retry delay from the bridge error.
/// `alefTypedError` passes an error that is not a bridge error through unchanged.
pub(crate) fn emit_bridge_conversions(errors: &[ErrorDef], module_name: &str, out: &mut String, mapper: &SwiftMapper) {
    #[derive(serde::Serialize)]
    struct ErrorName {
        rust: String,
        swift: String,
    }
    let converted: Vec<&ErrorDef> = errors.iter().filter(|error| !error.binding_excluded).collect();
    let mut names: Vec<ErrorName> = Vec::new();
    for error in &converted {
        let name = swift_error_name(error, module_name);
        let view = swift_view(error);
        let declared_validation = view
            .variants
            .iter()
            .find(|v| super::enums::swift_variant_case(&v.name) == "validation");
        #[derive(serde::Serialize)]
        struct BridgeCase {
            name: String,
            case_name: String,
            expression: Option<String>,
        }
        let cases: Vec<BridgeCase> = view
            .variants
            .iter()
            .map(|variant| BridgeCase {
                name: variant.name.clone(),
                case_name: super::enums::swift_variant_case(&variant.name),
                expression: bridge_case_expression(variant, mapper, error),
            })
            .collect();
        out.push_str(&crate::backends::swift::template_env::render(
            "swift_error_bridge_conversion.swift.jinja",
            crate::alef_context! {
                name => &name,
                has_throwing_case => cases.iter().any(|case| case.expression.as_deref().is_some_and(|expression| expression.contains("try "))),
                cases => &cases,
                has_validation_variant => declared_validation.is_some(),
                has_kind_method => error.methods.iter().any(|method| method.name == "kind"),
            },
        ));
        out.push('\n');
        names.push(ErrorName {
            rust: error.name.clone(),
            swift: name,
        });
    }
    out.push_str(&crate::backends::swift::template_env::render(
        "swift_error_bridge_helpers.swift.jinja",
        crate::alef_context! {
            errors => &names,
        },
    ));
    out.push('\n');
}

pub(super) fn emit_errors(errors: &[ErrorDef], module_name: &str, out: &mut String, mapper: &SwiftMapper) {
    for error in errors {
        emit_error(error, module_name, out, mapper);
        out.push('\n');
    }
    emit_bridge_conversions(errors, module_name, out, mapper);
}

#[cfg(test)]
#[path = "errors_tests.rs"]
mod tests;

#[cfg(test)]
#[path = "errors_duration_tests.rs"]
mod duration_tests;

#[path = "errors_description.rs"]
mod description;