# tfachmann/typst-as-library architecture

> Architecture diagram of the GitHub repository tfachmann/typst-as-library, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-03-30). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/tfachmann/typst-as-library
- Repository: https://github.com/tfachmann/typst-as-library

## Overview

##### Project type and purpose
- This is a Rust library that demonstrates how to integrate and use the typst library. It provides a wrapper (named in the example as "TypstWrapperWorld") to compile typst documents and then export them to PDF.
- The project features a core library (with src/lib.rs) and an example “native” application (under examples/native) that shows how the library is used.

##### File structure
- Top-level files such as Cargo.toml, LICENSE, and README.md indicate a standard Rust project structure.
- The src directory holds the library’s primary code (src/lib.rs).
- The examples directory, especially the examples/native subdirectory, contains a stand-alone application that demonstrates invoking the library.
- The presence of multiple Cargo manifest files (one for the main library and one for the native example) shows an intentional separation between the library code and the example app.

##### README for architectural insights
- The README explains how to interface with the typst library. The provided Rust snippet highlights:
- Initializing a wrapper (TypstWrapperWorld) with content and a directory.
- Compiling the document via typst::compile.
- Converting the compiled document to PDF using typst_pdf.
- Writing the generated PDF to the filesystem.
- This gives clues as to the major components and their interactions.

a. Main components to include:
- Core Library Module (src/lib.rs): Contains the wrapping logic around typst.
- Example Application (examples/native/main.rs): Demonstrates how to consume the library.
- External Libraries/Dependencies: typst and typst_pdf.
- Filesystem Interaction: For loading content and writing output (PDF).

b. Relationships and interactions:
- The Example Application acts as the entry point, instantiating the TypstWrapperWorld.
- The Core Library Component (TypstWrapperWorld) processes the document content and delegates the rendering task to typst’s compilation function.
- After the document is compiled, a call is made to typst_pdf for converting the document into a PDF.
- The final output is then written to disk using standard filesystem I/O.

c. Architectural patterns and design principles:
- Separation of Concerns: Distinguishing between the library’s functionality and the example usage.
- Layered Architecture: Clearly differentiating between the business logic (document processing and PDF conversion) and the infrastructure (file I/O and external library calls).
- External Dependency Abstraction: The core library wraps calls to external modules (typst and typst_pdf) enabling easier testing and potential replacement.

d. Relevant technologies and frameworks:
- Rust as the programming language.
- Cargo for package management and build automation.
- External Rust crates: typst and typst_pdf.
- Standard Rust file I/O libraries.

##### Instructing the principal software engineer
- Clear labels for each component:
- Example Application (entry point)
- Typst Library Wrapper (implementation in src/lib.rs)
- External Dependencies (typst, typst_pdf)
- File System (for reading input and writing PDF output)
- From the Example Application to the Typst Library Wrapper to indicate instantiation.
- From the Typst Library to typst for document compilation.
- From typst to typst_pdf for generating the PDF output.
- From the PDF generation back to the Example Application (or directly to File System) to indicate saving the file.
- This aids in visual separation and quickly communicates the type of each element.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Application Layer
    subgraph "Application Layer"
        A["Example Application"]:::app
        CAppConfig["Cargo.toml (Native App)"]:::config
    end

    %% Core Library
    subgraph "Core Library"
        B["Typst Library Wrapper"]:::core
        LibConfig["Cargo.toml (Library)"]:::config
    end

    %% External Dependencies
    subgraph "External Dependencies"
        D["typst (Compilation)"]:::external
        E["typst_pdf (PDF Conversion)"]:::external
    end

    %% Infrastructure
    subgraph "Infrastructure"
        F["File System I/O"]:::infra
    end

    %% Data Flow
    A -->|"instantiates"| B
    B -->|"calls_compile"| D
    D -->|"delegates_conversion"| E
    E -->|"writes_PDF"| F

    %% Click Events
    click B "https://github.com/tfachmann/typst-as-library/blob/main/src/lib.rs"
    click A "https://github.com/tfachmann/typst-as-library/blob/main/examples/native/src/main.rs"
    click LibConfig "https://github.com/tfachmann/typst-as-library/blob/main/Cargo.toml"
    click CAppConfig "https://github.com/tfachmann/typst-as-library/blob/main/examples/native/Cargo.toml"

    %% Styles
    classDef app fill:#adebad,stroke:#333,stroke-width:2px;
    classDef core fill:#cce5ff,stroke:#333,stroke-width:2px;
    classDef external fill:#fff2cc,stroke:#333,stroke-width:2px;
    classDef infra fill:#e6ccff,stroke:#333,stroke-width:2px;
    classDef config fill:#ffcccb,stroke:#333,stroke-width:2px;
```
