alef 0.20.1

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
//! Swift trait bridge codegen for outbound plugins.
//!
//! For each configured `TraitBridgeConfig` entry (when `bind_via = "function_param"`),
//! generates:
//!
//! 1. A Swift `protocol Swift<TraitName>Bridge` declaring the trait methods with
//!    async/throws matching the Rust trait method signatures. Excluded/internal types
//!    are marshalled as JSON strings at the boundary.
//! 2. A Swift `struct Swift<TraitName>Adapter` wrapping an instance of the protocol
//!    and exposing methods that handle marshalling (conversion from/to JSON for excluded types,
//!    conversion from/to proper Swift types for visible types).
//! 3. A `register<TraitName>(_ bridge: Swift<TraitName>Bridge)` function that
//!    constructs the adapter and calls into Rust to register it.

use crate::backends::swift::naming::bridge_protocol_name;
use crate::core::config::TraitBridgeConfig;
use crate::core::ir::{TypeDef, TypeRef};
use heck::{ToLowerCamelCase, ToSnakeCase};
use std::collections::HashSet;

/// Generate Swift trait bridge protocol and adapter for outbound plugins.
///
/// `exclude_types` is the set of types that are not visible in the generated Swift binding.
/// These types are marshalled as JSON strings at the trait boundary.
///
/// Returns a list of (filename, content) tuples ready for emission.
pub fn gen_trait_bridge_files(
    bridges: &[(String, &TraitBridgeConfig, &TypeDef)],
    exclude_types: &HashSet<String>,
) -> Vec<(String, String)> {
    let mut files = Vec::new();

    for (trait_name, bridge_cfg, trait_def) in bridges {
        // Skip if swift is in exclude_languages
        if bridge_cfg.exclude_languages.iter().any(|lang| lang == "swift") {
            continue;
        }

        // Skip if not function_param binding (only outbound plugins use this codegen)
        if !matches!(bridge_cfg.bind_via, crate::core::config::BridgeBinding::FunctionParam) {
            continue;
        }

        let content = gen_single_trait_bridge_file(trait_name, bridge_cfg, trait_def, exclude_types);
        // Use the canonical protocol name as the filename base so the filename
        // stays in sync with the protocol declaration.
        let protocol = bridge_protocol_name(trait_name);
        let filename = format!("{protocol}.swift");
        files.push((filename, content));
    }

    files
}

/// Generate Swift trait bridge code for a single trait.
///
/// `exclude_types` contains type names that are not visible in the Swift binding surface.
/// These types are marshalled as JSON strings at trait boundaries.
fn gen_single_trait_bridge_file(
    trait_name: &str,
    bridge_cfg: &TraitBridgeConfig,
    trait_def: &TypeDef,
    exclude_types: &HashSet<String>,
) -> String {
    let mut out = String::new();

    // Header
    out.push_str("// Generated by alef. Do not edit by hand.\n");
    out.push_str("// swift-format-ignore-file\n");
    out.push_str("// This file contains generated FFI glue for trait bridge registration.\n\n");
    out.push_str("import Foundation\n");
    out.push_str("import RustBridge\n\n");

    // MARK: Protocol Declaration
    let protocol = bridge_protocol_name(trait_name);
    out.push_str(&format!(
        "/// Protocol for outbound `{trait_name}` implementations.\n\
         /// Conform your Swift class or struct to this protocol to implement\n\
         /// a Rust trait from the host side.\n\
         public protocol {protocol}: AnyObject {{\n"
    ));

    for method in &trait_def.methods {
        if method.has_default_impl {
            continue;
        }

        let method_camel = method.name.to_lower_camel_case();
        // Build params, marshalling excluded types as JSON (String)
        let params_sig = swift_method_params(&method.params, exclude_types);
        // Build return type, marshalling excluded types as JSON (String)
        let return_type = swift_return_type(&method.return_type, exclude_types);
        let throws = if method.error_type.is_some() { " throws" } else { "" };
        let async_kw = if method.is_async { " async" } else { "" };

        out.push_str(&format!(
            "    func {method_camel}({params_sig}){async_kw}{throws} -> {return_type}\n"
        ));
    }

    out.push_str("}\n\n");

    // MARK: Adapter Class
    out.push_str(&format!(
        "/// Internal adapter wrapping a `{protocol}` conformer.\n\
         /// Marshals Swift types and trait calls to/from the C boundary.\n\
         /// Excluded/internal types are serialised to/from JSON strings.\n\
         final class Swift{trait_name}Adapter {{\n\
         \x20   private let bridge: any {protocol}\n\n"
    ));

    // Constructor
    out.push_str(&format!(
        "    init(bridge: any {protocol}) {{\n\
         \x20\x20\x20\x20self.bridge = bridge\n\
         \x20   }}\n\n"
    ));

    // Method entry points — these marshal types across the boundary
    for method in &trait_def.methods {
        if method.has_default_impl {
            continue;
        }

        let method_camel = method.name.to_lower_camel_case();
        // Build parameter signature for the adapter method (input from Rust across the boundary)
        let params_sig = swift_method_params(&method.params, exclude_types);
        // Build return type for the adapter method (output back to Rust)
        let return_type = swift_return_type(&method.return_type, exclude_types);

        out.push_str(&format!(
            "    func {method_camel}Call({params_sig}) -> {return_type} {{\n"
        ));

        // Generate method body: construct call arguments and handle return value.
        let (call_args, call_expr) = build_adapter_call_expr(method, exclude_types);
        let call_args_str = call_args.join(", ");

        if method.error_type.is_some() {
            // Error-returning method: wrap result in try-catch and return JSON envelope
            out.push_str(&format!(
                "        do {{\n\
                 \x20\x20\x20\x20\x20\x20\x20\x20let result = try self.bridge.{method_camel}({call_args_str})\n"
            ));
            out.push_str(&format!(
                "            return marshal_ok_result({call_expr})\n\
                 \x20\x20\x20\x20}} catch {{\n\
                 \x20\x20\x20\x20\x20\x20\x20\x20return marshal_error_result(error)\n\
                 \x20\x20\x20\x20}}\n"
            ));
        } else if method.is_async {
            // Async method without error: return the result directly (already marshalled)
            out.push_str(&format!(
                "        let result = await self.bridge.{method_camel}({call_args_str})\n"
            ));
            out.push_str(&format!("        return {call_expr}\n"));
        } else {
            // Sync method without error: return the result directly
            out.push_str(&format!(
                "        let result = self.bridge.{method_camel}({call_args_str})\n"
            ));
            out.push_str(&format!("        return {call_expr}\n"));
        }

        out.push_str("    }\n\n");
    }

    out.push_str("}\n\n");

    // MARK: Helper functions for marshalling
    out.push_str("// MARK: - Marshalling helpers\n\n");
    out.push_str(
        "private func marshal_ok_result<T: Encodable>(_ value: T) -> String {\n\
         \x20\x20\x20\x20let encoder = JSONEncoder()\n\
         \x20\x20\x20\x20if let data = try? encoder.encode(value),\n\
         \x20\x20\x20\x20   let jsonString = String(data: data, encoding: .utf8) {\n\
         \x20\x20\x20\x20\x20\x20\x20\x20return \"{\\\"ok\\\": \\(jsonString)}\"\n\
         \x20\x20\x20\x20}\n\
         \x20\x20\x20\x20return \"{\\\"ok\\\": null}\"\n\
         }\n\n\
         private func marshal_error_result(_ error: any Error) -> String {\n\
         \x20\x20\x20\x20let errorString = String(describing: error)\n\
         \x20\x20\x20\x20let encoder = JSONEncoder()\n\
         \x20\x20\x20\x20if let data = try? encoder.encode(errorString),\n\
         \x20\x20\x20\x20   let jsonString = String(data: data, encoding: .utf8) {\n\
         \x20\x20\x20\x20\x20\x20\x20\x20return \"{\\\"err\\\": \\(jsonString)}\"\n\
         \x20\x20\x20\x20}\n\
         \x20\x20\x20\x20return \"{\\\"err\\\": \\\"unknown error\\\"}\"\n\
         }\n\n",
    );

    // MARK: Registration Function
    if let Some(register_fn) = bridge_cfg.register_fn.as_deref() {
        let camel = register_fn.to_lower_camel_case();
        out.push_str(&format!(
            "/// Register an outbound `{trait_name}` plugin.\n\
             /// Pass an instance conforming to `{protocol}`.\n\
             public func {camel}(_ bridge: any {protocol}) throws {{\n\
             \x20   let adapter = Swift{trait_name}Adapter(bridge: bridge)\n\
             \x20   // Call into Rust to register the adapter\n\
             \x20   try RustBridge.{camel}(adapter)\n\
             }}\n"
        ));
    }

    out
}

/// Emit Swift method parameter signature from MethodDef params.
///
/// Excluded types (not in the visible binding surface) are marshalled as JSON strings.
fn swift_method_params(params: &[crate::core::ir::ParamDef], exclude_types: &HashSet<String>) -> String {
    if params.is_empty() {
        return String::new();
    }

    params
        .iter()
        .map(|p| {
            let name = p.name.to_snake_case();
            let ty = swift_type_name(&p.ty, exclude_types);
            format!("{}: {}", name, ty)
        })
        .collect::<Vec<_>>()
        .join(", ")
}

/// Get the Swift type name for a TypeRef.
///
/// Excluded/internal types (not in the visible binding surface) are marshalled as JSON strings.
fn swift_type_name(ty: &TypeRef, exclude_types: &HashSet<String>) -> String {
    match ty {
        TypeRef::Primitive(p) => match p {
            crate::core::ir::PrimitiveType::Bool => "Bool".to_string(),
            crate::core::ir::PrimitiveType::I8 => "Int8".to_string(),
            crate::core::ir::PrimitiveType::I16 => "Int16".to_string(),
            crate::core::ir::PrimitiveType::I32 => "Int32".to_string(),
            crate::core::ir::PrimitiveType::I64 => "Int64".to_string(),
            crate::core::ir::PrimitiveType::U8 => "UInt8".to_string(),
            crate::core::ir::PrimitiveType::U16 => "UInt16".to_string(),
            crate::core::ir::PrimitiveType::U32 => "UInt32".to_string(),
            crate::core::ir::PrimitiveType::U64 => "UInt64".to_string(),
            crate::core::ir::PrimitiveType::Usize => "Int".to_string(), // Maps to platform-dependent size
            crate::core::ir::PrimitiveType::Isize => "Int".to_string(), // Maps to platform-dependent size
            crate::core::ir::PrimitiveType::F32 => "Float".to_string(),
            crate::core::ir::PrimitiveType::F64 => "Double".to_string(),
        },
        TypeRef::String => "String".to_string(),
        TypeRef::Bytes => "Data".to_string(),
        TypeRef::Path => "URL".to_string(),
        TypeRef::Char => "Character".to_string(),
        TypeRef::Named(name) => {
            // If the named type is excluded (internal/not visible), marshal as JSON string
            if exclude_types.contains(name) {
                "String".to_string() // JSON-marshalled as String
            } else {
                name.clone()
            }
        }
        TypeRef::Vec(inner) => format!("[{}]", swift_type_name(inner, exclude_types)),
        TypeRef::Map(k, v) => format!(
            "[{}: {}]",
            swift_type_name(k, exclude_types),
            swift_type_name(v, exclude_types)
        ),
        TypeRef::Optional(inner) => format!("{}?", swift_type_name(inner, exclude_types)),
        TypeRef::Unit => "Void".to_string(),
        TypeRef::Json => "String".to_string(), // JSON is marshalled as String
        TypeRef::Duration => "TimeInterval".to_string(), // Duration -> TimeInterval in Swift
    }
}

/// Emit Swift return type from TypeRef.
fn swift_return_type(ty: &TypeRef, exclude_types: &HashSet<String>) -> String {
    swift_type_name(ty, exclude_types)
}

/// Build the call arguments and return expression for the adapter method.
///
/// Returns (call_args: Vec<String>, return_expr: String) where:
/// - call_args: formatted arguments to pass to the bridge method
/// - return_expr: expression to marshal the result back across the boundary
fn build_adapter_call_expr(
    method: &crate::core::ir::MethodDef,
    exclude_types: &HashSet<String>,
) -> (Vec<String>, String) {
    // Build the call arguments — for now, pass them through as-is
    // (they're already in the correct type after boundary marshalling)
    let call_args: Vec<String> = method.params.iter().map(|p| p.name.to_snake_case()).collect();

    // Build the return expression — marshal the result back to the boundary type
    let return_expr = match &method.return_type {
        TypeRef::Named(name) if exclude_types.contains(name) => {
            // Excluded type: encode to JSON string
            "try JSONEncoder().encode(result)...".to_string() // Placeholder
        }
        TypeRef::String | TypeRef::Bytes | TypeRef::Primitive(_) | TypeRef::Unit => "result".to_string(),
        _ => "result".to_string(), // Other types pass through
    };

    (call_args, return_expr)
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::core::config::BridgeBinding;

    fn make_trait_def(name: &str) -> TypeDef {
        TypeDef {
            name: name.to_string(),
            rust_path: format!("testcrate::{}", name),
            original_rust_path: String::new(),
            fields: vec![],
            methods: vec![],
            is_opaque: false,
            is_clone: true,
            is_copy: false,
            is_trait: true,
            has_default: false,
            has_stripped_cfg_fields: false,
            is_return_type: false,
            serde_rename_all: None,
            has_serde: false,
            super_traits: vec![],
            doc: String::new(),
            cfg: None,
            binding_excluded: false,
            binding_exclusion_reason: None,
        }
    }

    fn make_bridge_cfg(trait_name: &str) -> TraitBridgeConfig {
        TraitBridgeConfig {
            trait_name: trait_name.to_string(),
            param_name: None,
            type_alias: None,
            exclude_languages: vec![],
            super_trait: None,
            registry_getter: None,
            register_fn: Some(format!("register{}", trait_name)),
            unregister_fn: None,
            clear_fn: None,
            register_extra_args: None,
            bind_via: BridgeBinding::FunctionParam,
            options_type: None,
            options_field: None,
            context_type: None,
            result_type: None,
            ffi_skip_methods: Vec::new(),
        }
    }

    #[test]
    fn test_trait_bridge_protocol_generated() {
        let trait_def = make_trait_def("OcrBackend");
        let bridge_cfg = make_bridge_cfg("OcrBackend");
        let bridges = vec![("OcrBackend".to_string(), &bridge_cfg, &trait_def)];
        let exclude_types = HashSet::new();
        let files = gen_trait_bridge_files(&bridges, &exclude_types);

        assert_eq!(files.len(), 1);
        assert_eq!(files[0].0, "SwiftOcrBackendBridge.swift");
        assert!(files[0].1.contains("protocol SwiftOcrBackendBridge"));
    }

    #[test]
    fn test_trait_bridge_excludes_swift_language() {
        let trait_def = make_trait_def("OcrBackend");
        let mut bridge_cfg = make_bridge_cfg("OcrBackend");
        bridge_cfg.exclude_languages = vec!["swift".to_string()];
        let bridges = vec![("OcrBackend".to_string(), &bridge_cfg, &trait_def)];
        let exclude_types = HashSet::new();
        let files = gen_trait_bridge_files(&bridges, &exclude_types);

        assert!(files.is_empty());
    }

    #[test]
    fn test_trait_bridge_skips_non_function_param() {
        let trait_def = make_trait_def("OcrBackend");
        let mut bridge_cfg = make_bridge_cfg("OcrBackend");
        bridge_cfg.bind_via = BridgeBinding::OptionsField;
        let bridges = vec![("OcrBackend".to_string(), &bridge_cfg, &trait_def)];
        let exclude_types = HashSet::new();
        let files = gen_trait_bridge_files(&bridges, &exclude_types);

        assert!(files.is_empty());
    }

    #[test]
    fn test_swift_type_mapping() {
        use crate::core::ir::PrimitiveType;
        let exclude_types = HashSet::new();
        assert_eq!(swift_type_name(&TypeRef::String, &exclude_types), "String");
        assert_eq!(swift_type_name(&TypeRef::Bytes, &exclude_types), "Data");
        assert_eq!(swift_type_name(&TypeRef::Unit, &exclude_types), "Void");
        assert_eq!(
            swift_type_name(&TypeRef::Primitive(PrimitiveType::I32), &exclude_types),
            "Int32"
        );
        assert_eq!(swift_type_name(&TypeRef::Duration, &exclude_types), "TimeInterval");
    }

    #[test]
    fn test_swift_marshals_excluded_types_as_json() {
        let mut exclude_types = HashSet::new();
        exclude_types.insert("InternalDocument".to_string());
        assert_eq!(
            swift_type_name(&TypeRef::Named("InternalDocument".to_string()), &exclude_types),
            "String",
            "Excluded types should be marshalled as JSON strings"
        );
        assert_eq!(
            swift_type_name(&TypeRef::Named("ExtractionResult".to_string()), &exclude_types),
            "ExtractionResult",
            "Non-excluded types should keep their original names"
        );
    }
}