Sphinxcontrib-rust
Warning
This project is still under development. While basic features are available, there are still improvements to make. See Limitations for limitations compared to rustdoc and Compatibility with rustdoc for writing markdown syntax compatible with both rustdoc and Sphinx.
This is a Sphinx extension for integrating Rust programming language projects in Sphinx builds. You can read this documentation on Gitlab Pages or readthedocs.
Motivation
This is primarily meant for teams and projects that are already using Sphinx as a documentation build tool, and would like to include documentation for Rust projects in it along with Python, C and other languages.
Using the extension adds the following functionality:
Rust specific directives and roles that can be used to link and cross-reference rustdoc comments.
rustdoc comments may be written in reStructuredText.
Various Sphinx features and extensions can be used to generate and publish the docs.
This is not a replacement for rustdoc, and since rustdoc is a part of the Rust language itself, it will not have all the same features as rustdoc.
The goal is to provide a way for teams and projects using multiple languages to publish a single, consolidated documentation and use this, along with rustdoc, as part of the documentation workflow.
See Limitations for some cases where the tool will not work the same as rustdoc and Compatibility with rustdoc for any tweaks required to make the tool work with existing docstrings.
How to use
Installation
There are two components that are required for this to work
The
sphinx-rustdocgenRust crate for extracting the docs.The
sphinxcontrib_rustPython package, which is a Sphinx extension.
Both components are installed when installing the Python package with
pip install sphinxcontrib-rust
The installation will check for cargo in the $PATH environment variable and will use that to build and install
the Rust executable.
The executable is built with the Rust code shipped with the Python package. This ensures that the Rust executable and Python package are always compatible with each other.
Make sure that the directory where cargo installs the executable is in $PATH as well. If the default
installation directory is not part of the $PATH environment, the installed executable should be specified in the
Sphinx configuration with rust_rustdocgen option.
The Rust executable may also be installed independently from crates.io. However, it has limited use outside of the extension and the interface is designed for programmatic use rather than interactive use.
With reStructuredText
To use the extension with rst rustdoc comments, simply add the extension to the conf.py file. The various
configuration options supported by the extension, along with their defaults, are documented below.
extensions = ["sphinxcontrib_rust"]
rust_crates = {
"my_crate": ".",
"my_crate_derive": "my-crate-derive",
}
rust_doc_dir = "docs/crates/"
This will generate the documentation from your Rust crates and put them in the docs/crates/<crate_name> directories.
You can link against the documentation in your toctree by specifying the path to lib file and any executables.
See Including the docs in the Sphinx build for more details.
.. toctree::
docs/crates/my_crate/main
docs/crates/my_crate/lib
The extension also adds various roles for Rust items. The roles can be used within the Sphinx documentation and also within the docstrings themselves. The roles can even be used in docstrings of a different language that is part of the same Sphinx project. The roles are documented in Rust specific roles.
The extension also provides various Rust specific directives and Rust items index that can be used in the documentation.
With Markdown
To use the extension with markdown rustdoc comments, add the extension to the conf.py file and also add the
myst-parser extension. Sphinx also needs to be configured for Markdown builds.
Using various extensions for myst-parser, existing docstrings can be rendered with Sphinx with minimal changes required. See Compatibility with rustdoc for details on how to get compatibility with various Rust markdown extensions and write docstrings that can work with both rustdoc and Sphinx.
The various configuration options for the Rust extension, along with their defaults, are documented below. Also see the configuration options for MyST to customize the markdown.
extensions = ["sphinxcontrib_rust", "myst_parser"]
source_suffix = {
".rst": "restructuredtext",
".md": "markdown",
".txt": "markdown", # Optional
}
# See docs/compatibility for details on these extensions.
myst_enable_extensions = {
"colon_fence",
"html_admonition",
"replacements",
"smartquotes",
"strikethrough",
"tasklist",
}
rust_crates = {
"my_crate": ".",
"my_crate_derive": "my-crate-derive",
}
rust_doc_dir = "docs/crates/"
This enables all the same roles and indexes as with rst. Use the myst-parser syntax for the roles.
Options
Options are simply Python variables set in the conf.py file. Most options can be provided as a global value or a
dict of values per crate, with the crate name as the key. The options that are global only are listed separately below.
- rust_crates:
(Required) A dict of crate names and their source code directories. This must be a dict even for a single crate. It determines which crates are documented. The directory should be the one which contains the
Cargo.tomlfile for the crate and each crate in the workspace must be listed explicitly.- rust_doc_dir:
(Required) A directory under which to write the docs for all crates, or a dict of directory for each crate name. The directories will be read by Sphinx during the build, so they must be part of the source tree and not under the build directory. The build process will create a directory with the crate name under this, even when specified per crate.
- rust_rustdoc_fmt:
Either
rstormd. (Default:rst)- rust_visibility:
Only includes documentation and indexes for items with visibility greater than or equal to the setting. The value can be
pub,crateorpvt. Visibility restrictions likesuperandin <path>are not supported currently and are treated as private. (Default:pub).- rust_strip_src:
Whether to remove the
src/directory when creating output files or not. The default isTrue, since that was the initial behavior. So, instead of creating output files as<crate_name>/src/<mod_name>.rst, the output files are created as<crate_name>/<mod_name>.rst, effectively removingsrc/from the paths. Set toFalsefor crates that use a different path. (Default: True)
The below options are global options, and cannot be specified per crate.
- rust_generate_mode:
One of
always,skiporchanged. If set toalways, all documents are regenerated. If set toskip, the docs are not regenerated at all. If set tochanged, only docs whose source files have been modified since they were last modified are regenerated. (Default:changed)- rust_rustdocgen:
The path to the
sphinx-rustdocgenexecutable to use. The path must be an absolute path or relative to Sphinx’s working directory. (Default: Obtained from the$PATHenvironment variable.)