Skip to content

Conversation

jamiepine
Copy link

Add Specta Swift - Rust to Swift Type Exporter

What's Added

This PR introduces specta-swift, a new exporter for the Specta ecosystem that generates Swift code from Rust type definitions.

Key Features

  • Swift Code Generation: Converts Rust structs and enums to idiomatic Swift
  • Complex Enum Support: Handles Rust enums with named fields, generating separate Swift structs
  • Duration Mapping: Automatically maps std::time::Duration to a custom RustDuration helper struct
  • Custom Codable: Generates proper Swift Codable implementations for complex enums
  • Configurable Output: Support for different naming conventions, indentation styles, and optional syntax

Implementation

  • Builder API: Fluent configuration with methods like .naming(), .indent(), .add_protocol()
  • Type Mapping: Comprehensive mapping from Rust primitives to Swift equivalents
  • Struct Naming: Enum variants with named fields get prefixed struct names (e.g., UserRoleUserRoleUserData)
  • Documentation: Preserves Rust doc comments in generated Swift code

Testing & Examples

  • 12 test files covering basic types, complex unions, string enums, and edge cases
  • 8 example files demonstrating all configuration options
  • 23+ generated Swift files showing actual output

Usage

use specta::{Type, TypeCollection};
use specta_swift::Swift;

#[derive(Type)]
struct User {
    id: u32,
    name: String,
    email: Option<String>,
}

fn main() {
    let types = TypeCollection::default().register::<User>();
    
    Swift::new()
        .naming(NamingConvention::CamelCase)
        .with_serde()
        .export_to("./Types.swift", &types)
        .unwrap();
}

Generates:

import Foundation
import Codable

public struct User: Codable {
    public let id: UInt32
    public let name: String
    public let email: String?
}

Files Added

  • specta-swift/ - Main crate with Swift code generation
  • specta-swift/src/ - Core implementation (lib.rs, swift.rs, primitives.rs, error.rs)
  • specta-swift/examples/ - 8 comprehensive examples
  • specta-swift/tests/ - 12 test files
  • specta-swift/README.md - Documentation and usage guide

This completes the Swift exporter for the Specta ecosystem, providing a robust way to share types between Rust backends and Swift/iOS applications.

- Added `specta-serde`, `thiserror`, and `serde` dependencies to `Cargo.toml`.
- Updated `specta` dependency to include the `derive` feature.
- Enhanced documentation in `lib.rs` with usage examples for exporting Rust types to Swift.
- Refactored `tests` to rename `Type` enum to `TypeEnum` for clarity.
- Introduced new structs and enums including Admin, Circle, Shape, User, and UserType to enhance type representation.
- Added ApiResponse and DatabaseResult enums for better API response handling.
- Updated naming conventions in primitives.rs for enum case conversion.
- Enhanced the Swift naming convention implementation in swift.rs to support enum case formatting.
- Changed author in Cargo.toml and updated dependencies to include `uuid` and `chrono`.
- Introduced CommentsExample.swift to demonstrate multi-line comment support in generated Swift code.
- Improved comment handling in primitives.rs to format multi-line documentation correctly.
- Added tests for multi-line comments and support for common types, including UUID and Chrono.
…eneration

- Introduced CriticalFixesDemo.swift to showcase improvements in handling Duration fields, tuple structs, multi-line documentation, and complex union types.
- Updated primitives.rs to correctly convert Duration structs to TimeInterval and handle special standard library types.
- Added tests for verifying the correct generation of Swift code from Rust types, ensuring no malformed syntax is produced.
- Enhanced overall documentation and examples for better clarity in generated Swift output.
- Added `is_string_enum` method to determine if an enum should be serialized as a string enum based on its variants.
- Updated `EnumRepr` to include a representation for string enums with optional `rename_all` attribute.
- Enhanced Swift code generation to support string enums, including raw value assignments for unit variants.
- Introduced tests for various string enum scenarios, ensuring correct Swift output for different naming conventions.
- Improved handling of struct-like variants in enums, generating appropriate Swift structs and Codable implementations.
…on for adjacently tagged enums

- Changed the conversion of Duration structs from `TimeInterval` to `RustDuration`.
- Updated handling of special standard types to reflect the new Duration conversion.
- Improved the generation of custom Codable implementations for adjacently tagged enums, including type keys and variant handling.
- Refactored code for better readability and maintainability.
- Implemented handling for string enums with a `rename_all` attribute to support various naming conventions (snake_case, UPPERCASE, camelCase, PascalCase, kebab-case).
- Added error handling to ensure string enums do not have fields.
- Updated validation logic to reflect that string enums are treated as string literals, not objects.
- Added multiple example files to showcase various Rust to Swift type conversions, including basic types, advanced unions, configuration options, and special types.
- Removed outdated example files such as CommentsExample.swift and CriticalFixesDemo.swift to streamline the codebase.
- Updated README.md to reflect the new examples and their functionalities.
- Improved documentation and organization of example files for better clarity and usability.
…on status

- Updated the README to include a new section detailing support for exporting structs and enums to multiple languages, including TypeScript and Swift.
- Added a table summarizing the status of various language exporters, including their features and implementation status.
- Improved examples to demonstrate exporting types to multiple languages, enhancing clarity for users.
- Provided a legend to explain the stability status of each language exporter.
- Updated multiple enum declarations across various Swift example files to remove the explicit Codable conformance, as it will now be handled via extensions.
- Ensured that the generated Swift code maintains proper syntax and functionality while improving clarity and reducing redundancy.
- Adjusted tests to verify the correct generation of enums without direct Codable conformance, confirming that extensions are used for Codable implementation instead.
@Brendonovich
Copy link
Collaborator

what the sigma

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants