# matrix-org/matrix-rust-components-swift architecture

> Architecture diagram of the GitHub repository matrix-org/matrix-rust-components-swift, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-04-28). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/matrix-org/matrix-rust-components-swift
- Repository: https://github.com/matrix-org/matrix-rust-components-swift

## Overview

##### Project type and purpose
- This is not a full‐stack application but an open‐source “packaging” library / wrapper. Its goal is to expose the Matrix Rust SDK to Swift projects via the Swift Package Manager (SPM). It does so by bundling a pre-built Rust binary and providing Swift/FFI glue code.

##### File structure
- Top‐level:
- .github: issue/pr templates, CI and contribution guidelines
- Package.swift, README.md, CONTRIBUTING.md, LICENSE, SECURITY.md: SPM manifest and repo docs
- Sources/MatrixRustSDK: Swift source modules (matrix_sdk.swift, base, common, crypto, ffi, ui)
- Tools/Release: a Swift tool and script (Release.swift) to automate tagging and packaging of new Rust binaries
- docs: “Getting Started” guide
- .swiftpm/xcode: Xcode workspace metadata
- Patterns: modular Swift wrapper (split by functionality), FFI bridge module, release-automation tooling

##### README for additional insights
- Describes usage (import via SwiftPM), release process (run Tools/Release script when upstream Rust SDK releases), local testing (development happens in the Rust SDK repo, not here).

a. Main components:
- Swift Package Manager (SPM) resolver (consumer of this package)
- MatrixRustSDK Swift modules:
– matrix_sdk_common, matrix_sdk_base, matrix_sdk_crypto, matrix_sdk_ui
– matrix_sdk_ffi (FFI layer)
- Embedded pre-compiled Rust SDK binary
- Tools/Release script (automates tagging, fetching new Rust binaries, updating Package.swift)
- GitHub repository (host, CI, issue templates)
b. Relationships and interactions:
- SPM pulls the package (Package.swift) from GitHub
- Package defines targets for Swift modules, declares a binary target pointing to the Rust SDK archive
- matrix_sdk_ffi uses Swift’s C interop to load and call into the Rust binary
- Other Swift modules wrap, extend, or expose higher-level APIs to Swift apps
- Tools/Release interacts with Git tags, downloads new Rust binary, updates package metadata, commits changes
c. Architectural patterns and design principles:
- Modular layering:
– FFI layer isolated from higher-level Swift logic
– Each functional area (crypto, UI, common) separated into distinct targets
- Binary packaging pattern: SPM binaryTarget to embed platform-specific pre-built library
- Release automation: single-responsibility tool that updates and publishes new versions
d. Key technologies and frameworks:
- Swift Package Manager (Package.swift, binaryTarget)
- Swift language modules
- Rust compiled into a C‐compatible binary (Cargo, rustc)
- Swift FFI / C interop
- Xcode workspace for local IDE support
- GitHub Actions or other CI (implied by .github)

## Diagram (Mermaid)

```mermaid
graph TB
    classDef code fill:#e0f7fa,stroke:#006064,stroke-width:1px
    classDef external fill:#f1f8e9,stroke:#33691e,stroke-width:1px
    classDef tool fill:#fff3e0,stroke:#e65100,stroke-width:1px
    classDef binary fill:#f3e5f5,stroke:#4a148c,stroke-width:1px

    subgraph "GitHub Repository"
        direction TB
        RepoMeta([".github/ISSUE_TEMPLATE"]):::external
        BugTemplate(["bug_report.md"]):::external
        FeatureTemplate(["feature_request.md"]):::external
        ConfigYml(["config.yaml"]):::external
        RepoDocs(["README.md, CONTRIBUTING.md, SECURITY.md, LICENSE"]):::external

        RepoMeta --> BugTemplate
        RepoMeta --> FeatureTemplate
        RepoMeta --> ConfigYml
    end

    subgraph Tools
        direction TB
        ReleaseTool(["Release Tool (Swift)"]):::tool
    end

    subgraph "Sources/MatrixRustSDK"
        direction TB
        MatrixSDK(["matrix_sdk.swift"]):::code
        SDKBase(["matrix_sdk_base.swift"]):::code
        SDKCommon(["matrix_sdk_common.swift"]):::code
        SDKCrypto(["matrix_sdk_crypto.swift"]):::code
        SDKUI(["matrix_sdk_ui.swift"]):::code
        SDKFFI(["matrix_sdk_ffi.swift"]):::code
    end

    RustBinary(["Embedded Rust SDK Binary"]):::binary
    SwiftPM(["SwiftPM Registry"]):::external
    Consumer(["SwiftPM Consumer"]):::external

    ReleaseTool -->|"publishes release"| RepoMeta
    RepoMeta -->|"hosts package manifest"| SwiftPM
    SwiftPM -->|"resolves & fetches package"| Consumer

    Consumer -->|"imports entry point"| MatrixSDK

    MatrixSDK -->|"wraps APIs via"| SDKBase
    MatrixSDK -->|"wraps APIs via"| SDKCommon
    MatrixSDK -->|"wraps APIs via"| SDKCrypto
    MatrixSDK -->|"wraps APIs via"| SDKUI
    MatrixSDK -->|"bridges via"| SDKFFI

    SDKFFI -->|"FFI calls"| RustBinary

    click RepoMeta "https://github.com/matrix-org/matrix-rust-components-swift/blob/main/.github/ISSUE_TEMPLATE/bug_report.md"
    click BugTemplate "https://github.com/matrix-org/matrix-rust-components-swift/blob/main/.github/ISSUE_TEMPLATE/bug_report.md"
    click FeatureTemplate "https://github.com/matrix-org/matrix-rust-components-swift/blob/main/.github/ISSUE_TEMPLATE/feature_request.md"
    click ConfigYml "https://github.com/matrix-org/matrix-rust-components-swift/blob/main/.github/ISSUE_TEMPLATE/config.yaml"
    click MatrixSDK "https://github.com/matrix-org/matrix-rust-components-swift/blob/main/Sources/MatrixRustSDK/matrix_sdk.swift"
    click SDKBase "https://github.com/matrix-org/matrix-rust-components-swift/blob/main/Sources/MatrixRustSDK/matrix_sdk_base.swift"
    click SDKCommon "https://github.com/matrix-org/matrix-rust-components-swift/blob/main/Sources/MatrixRustSDK/matrix_sdk_common.swift"
    click SDKCrypto "https://github.com/matrix-org/matrix-rust-components-swift/blob/main/Sources/MatrixRustSDK/matrix_sdk_crypto.swift"
    click SDKUI "https://github.com/matrix-org/matrix-rust-components-swift/blob/main/Sources/MatrixRustSDK/matrix_sdk_ui.swift"
    click SDKFFI "https://github.com/matrix-org/matrix-rust-components-swift/blob/main/Sources/MatrixRustSDK/matrix_sdk_ffi.swift"
    click ReleaseTool "https://github.com/matrix-org/matrix-rust-components-swift/blob/main/Tools/Release/Sources/Release.swift"
```
