evgenyigumnov / rustsn

This Rust-based tool generates, compiles, and tests code using LLMs, resolves dependencies, and provides explanations of existing code through embeddings.
Apache License 2.0
60 stars 15 forks source link

Add Swift support #10

Closed evgenyigumnov closed 1 month ago

evgenyigumnov commented 1 month ago

Launch example

rustsn --lang=swift

Example query:

take 2 parameters and add them and return the result

Example generation:

Package.swift

// swift-tools-version:5.7
import PackageDescription

let package = Package(
    name: "Solution",
    products: [
        .executable(name: "Solution", targets: ["Solution"]),
    ],
    dependencies: [
        .package(url: "https://github.com/apple/swift-test.git", from: "1.0.0"),
    ],
    targets: [
        .target(
            name: "Solution",
            dependencies: []),
        .testTarget(
            name: "SolutionTests",
            dependencies: ["Solution"]),
    ]
)

Sources/Solution/main.swift

public func solution(a: Any, b: Any) -> Any {
    if let intA = a as? Int, let intB = b as? Int {
        return intA + intB
    } else if let stringA = a as? String, let intB = b as? Int {
        return stringA + String(intB)
    }
    return "Invalid input types"
}

// Example usage
// print(solution(a: 1, b: 2)) // Outputs: 3

Tests/SolutionTests/SolutionTests.swift

import XCTest
@testable import Solution

final class SolutionTests: XCTestCase {
    func testAdditionOfIntegers() {
        XCTAssertEqual(solution(a: 1, b: 2) as? Int, 3)
    }

    func testAdditionOfNegativeIntegers() {
        XCTAssertEqual(solution(a: -1, b: -2) as? Int, -3)
    }

    func testAdditionOfStringAndInteger() {
        XCTAssertEqual(solution(a: "1", b: 2) as? String, "12")
    }

    func testInvalidInputTypes() {
        XCTAssertEqual(solution(a: 1.5, b: "test") as? String, "Invalid input types")
    }
}

Example install dependencies

swift package resolve

Example build the project

swift build

Example run tests

swift test