Architecture

This page documents the architectural design desicions.

Python and Rust split

The codebase is split into the Python API (src/viking) and the Rust implementation (src/viking_core). In most cases, the Rust side does the heavy lifting, and the Python side exposes a nice API for downstream projects to integrate into their tooling scripts.

As a rule of thumb, when deciding whether something should be in Rust or Python:

Python-Rust interop

Interop modules

There are 3 modules responsible for Python-Rust interop: pyinterop, python, and pyapi, each serves a different role in the dependency graph:

Calling Python from Rust

Only Python code calls Rust directly. Rust never calls into Python in the same process. This way, Python adds no overhead to paths that don't need it, even when other paths in the same command do (like check).

When Rust needs to run Python code, it does so through PyCmd, which serializes the command as a JSON payload and runs the py-json-call command of the Python CLI in a new process.

To add a new Python call:

"Global" state in Rust

The Rust side should not have process-wide singletons, to support Python's upcoming multi-interpreter feature, where a single process may host multiple Python interpreters. This means avoiding things like static MY_STATE: LazyLock<...> = ... in Rust.

Unfortunately, PyO3 does not currently support per-module state. We work around this by allocating the global state in Rust once during module initialization, then passing (leaking) the handle to Python, which stores it as a module-scope variable. Rust functions that depend on this global state must take it as an argument from the Python side. See core.py.

This global state should not be exposed in the public Python API. Instead, add a wrapper that calls the _core API internally and passes in core_state.