# mozilla/uniffi-rs architecture

> Architecture diagram of the GitHub repository mozilla/uniffi-rs, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-01-10). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/mozilla/uniffi-rs
- Repository: https://github.com/mozilla/uniffi-rs

## Overview

##### Main Components Identification

The system should be divided into these major components:

a) Core Components:
- uniffi_core: The fundamental runtime library
- uniffi_bindgen: The binding generator engine
- uniffi_macros: Procedural macros for interface definition
- uniffi_build: Build system integration
- uniffi_meta: Metadata handling system
- uniffi_udl: UDL (UniFFI Definition Language) parser

b) Foreign Language Bindings:
- Kotlin bindings generator
- Swift bindings generator
- Python bindings generator
- Ruby bindings generator

**Top Level (Interface Layer):**
- [UDL Files] ←→ [Proc Macros]
- ↓
**Middle Level (Processing Layer):**
- [uniffi_bindgen]
- ↓
- [Code Generation Engine]
- ↓
**Bottom Level (Output Layer):**
- [Kotlin] [Swift] [Python] [Ruby]

##### Component Relationships

a) Input Flow:
- How UDL files and Proc Macros feed into the uniffi_bindgen
- Uniffi_meta for metadata processing
- Link uniffi_udl for parsing UDL files

b) Processing Flow:
- Uniffi_bindgen to the template system
- Interaction with uniffi_core for runtime support
- Link to uniffi_build for compilation integration

c) Output Flow:
- Generation of language-specific bindings
- FFI layer creation
- Final library packaging

##### Special Elements

- Template system (showing how it feeds into each language generator)
- FFI layer (showing how it bridges Rust and target languages)
- Build pipeline integration points
- Testing infrastructure

##### Key Features to Highlight

- Thread safety handling
- Memory management
- Type conversion system
- Error handling
- Async/Future support
- Custom type support

## Diagram (Mermaid)

```mermaid
flowchart TB
    %% Styles and classes
    classDef input fill:#90EE90,stroke:#333,stroke-width:2px
    classDef processing fill:#87CEEB,stroke:#333,stroke-width:2px
    classDef output fill:#FFA500,stroke:#333,stroke-width:2px
    classDef runtime fill:#DDA0DD,stroke:#333,stroke-width:2px
    classDef build fill:#808080,stroke:#333,stroke-width:2px

    %% Interface Layer
    subgraph Interface
        UDL[UDL Files]:::input
        PM[Procedural Macros]:::input
    end
    click PM "https://github.com/mozilla/uniffi-rs/tree/main/uniffi_macros/"

    %% Core Processing Layer
    subgraph Core
        UDLParser[UDL Parser]:::processing
        Meta[Metadata Handler]:::processing
        Bindgen[Binding Generator]:::processing
        Templates[Template System]:::processing
    end
    click UDLParser "https://github.com/mozilla/uniffi-rs/tree/main/uniffi_udl/"
    click Meta "https://github.com/mozilla/uniffi-rs/tree/main/uniffi_meta/"
    click Bindgen "https://github.com/mozilla/uniffi-rs/tree/main/uniffi_bindgen/"
    click Templates "https://github.com/mozilla/uniffi-rs/tree/main/uniffi_bindgen/src/bindings/*/templates/"

    %% Runtime Support Layer
    subgraph Runtime
        CoreLib[Core Runtime Library]:::runtime
        FFI[FFI Layer]:::runtime
        Interface[Interface System]:::runtime
        Scaffolding[Scaffolding System]:::runtime
    end
    click CoreLib "https://github.com/mozilla/uniffi-rs/tree/main/uniffi_core/"
    click FFI "https://github.com/mozilla/uniffi-rs/tree/main/uniffi_core/src/ffi/"
    click Interface "https://github.com/mozilla/uniffi-rs/tree/main/uniffi_bindgen/src/interface/"
    click Scaffolding "https://github.com/mozilla/uniffi-rs/tree/main/uniffi_bindgen/src/scaffolding/"

    %% Build System
    subgraph BuildSystem
        Build[Build Integration]:::build
    end
    click Build "https://github.com/mozilla/uniffi-rs/blob/main/uniffi_build/src/lib.rs"

    %% Language Bindings Layer
    subgraph Bindings
        Kotlin[Kotlin Generator]:::output
        Swift[Swift Generator]:::output
        Python[Python Generator]:::output
        Ruby[Ruby Generator]:::output
    end
    click Kotlin "https://github.com/mozilla/uniffi-rs/tree/main/uniffi_bindgen/src/bindings/kotlin/"
    click Swift "https://github.com/mozilla/uniffi-rs/tree/main/uniffi_bindgen/src/bindings/swift/"
    click Python "https://github.com/mozilla/uniffi-rs/tree/main/uniffi_bindgen/src/bindings/python/"
    click Ruby "https://github.com/mozilla/uniffi-rs/tree/main/uniffi_bindgen/src/bindings/ruby/"

    %% Connections
    UDL --> UDLParser
    PM --> Bindgen
    UDLParser --> Bindgen
    Meta --> Bindgen
    Bindgen --> Templates
    Templates --> Kotlin
    Templates --> Swift
    Templates --> Python
    Templates --> Ruby
    CoreLib --> FFI
    FFI --> Interface
    Interface --> Scaffolding
    Build --> Bindgen

    %% Legend
    subgraph Legend
        L1[Input Components]:::input
        L2[Processing Components]:::processing
        L3[Output Components]:::output
        L4[Runtime Components]:::runtime
        L5[Build Components]:::build
    end
```
