How tfachmann/typst-as-library works
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.