Skip to content

Commit 6f2c767

Browse files
committed
Add sphinx-rust support
1 parent 30444aa commit 6f2c767

File tree

6 files changed

+129
-7
lines changed

6 files changed

+129
-7
lines changed

.github/workflows/build.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,9 @@ jobs:
3939
with:
4040
version: ${{ matrix.python-version }}
4141

42+
- name: Install Rust
43+
uses: dtolnay/rust-toolchain@stable
44+
4245
- name: Install doxygen
4346
run: sudo apt-get install -y doxygen
4447

.github/workflows/docs.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ jobs:
1212
steps:
1313
- uses: actions/checkout@v6
1414

15+
- name: Install Rust
16+
uses: dtolnay/rust-toolchain@stable
17+
1518
- name: Install doxygen
1619
run: sudo apt-get install -y doxygen
1720

docs/src/api.md

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,13 @@ Use `doxygenindex` to document all symbols from Doxygen XML:
4040
```doxygenindex
4141
```
4242

43+
```{toctree}
44+
:hidden:
45+
:maxdepth: 2
46+
47+
/examples/cpp/docs/api
48+
```
49+
4350
### Document Individual Classes
4451

4552
Or document specific classes with `doxygenclass`:
@@ -60,22 +67,39 @@ This is an example of documenting Rust code using sphinx-rust integration.
6067

6168
Use `rust:crate` to document an entire Rust crate:
6269

63-
```rust:crate calculator
70+
```{eval-rst}
71+
.. rust:crate:: calculator
72+
73+
```
74+
75+
```{toctree}
76+
:hidden:
77+
:maxdepth: 2
78+
79+
/api/crates/calculator/index
6480
```
6581

6682
### Document Individual Items
6783

6884
Or document specific structs, enums, and functions:
6985

70-
```rust:struct calculator::Calculator
86+
```{eval-rst}
87+
.. rust:struct:: calculator::Calculator
88+
7189
```
7290

73-
```rust:struct calculator::ScientificCalculator
91+
```{eval-rst}
92+
.. rust:struct:: calculator::ScientificCalculator
93+
7494
```
7595

76-
```rust:enum calculator::Operation
96+
```{eval-rst}
97+
.. rust:enum:: calculator::Operation
98+
7799
```
78100

79-
```rust:enum calculator::CalculatorError
101+
```{eval-rst}
102+
.. rust:enum:: calculator::CalculatorError
103+
80104
```
81105

docs/src/configuration.md

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -470,3 +470,95 @@ Then in your documentation files, you can use breathe directives:
470470
\`\`\`{doxygenfunction} MyNamespace::myFunction
471471
\`\`\`
472472
```
473+
474+
## Sphinx-Rust Integration
475+
476+
Yardang provides integration with [sphinx-rust](https://sphinx-rust.readthedocs.io/) for documenting Rust code. To use this feature, install yardang with the sphinx-rust extra:
477+
478+
```bash
479+
pip install yardang[sphinx-rust]
480+
```
481+
482+
All sphinx-rust configuration is under `[tool.yardang.sphinx-rust]`.
483+
484+
### `crates`
485+
486+
A list of paths to Rust crates to document.
487+
488+
```toml
489+
[tool.yardang.sphinx-rust]
490+
crates = [
491+
"path/to/crate1",
492+
"path/to/crate2",
493+
]
494+
```
495+
496+
### `doc-formats`
497+
498+
A dictionary mapping crate names to their docstring format. Valid values are `"restructuredtext"` (default) or `"myst-nb"` (for markdown docstrings).
499+
500+
```toml
501+
[tool.yardang.sphinx-rust]
502+
doc-formats = { mycrate = "myst-nb" }
503+
```
504+
505+
**Note:** When using `myst_nb` as your Sphinx parser (which yardang uses by default), use `"myst-nb"` instead of `"markdown"` for markdown docstrings.
506+
507+
### `viewcode`
508+
509+
Enable links to the source code for documented items. Defaults to `true`.
510+
511+
```toml
512+
[tool.yardang.sphinx-rust]
513+
viewcode = true
514+
```
515+
516+
### Complete Example
517+
518+
Here's a complete example configuration for a Rust project:
519+
520+
```toml
521+
[tool.yardang]
522+
title = "My Rust Library"
523+
root = "docs/index.md"
524+
pages = ["docs/api.md", "docs/examples.md"]
525+
use-autoapi = false
526+
527+
[tool.yardang.sphinx-rust]
528+
crates = [
529+
"crates/mylib",
530+
"crates/mylib-utils",
531+
]
532+
doc-formats = { mylib = "myst-nb", "mylib-utils" = "restructuredtext" }
533+
viewcode = true
534+
```
535+
536+
Then in your documentation files, you can use sphinx-rust directives:
537+
538+
````markdown
539+
# API Reference
540+
541+
## Document a Crate
542+
543+
\`\`\`{eval-rst}
544+
.. rust:crate:: mylib
545+
546+
\`\`\`
547+
548+
## Document Individual Items
549+
550+
\`\`\`{eval-rst}
551+
.. rust:struct:: mylib::MyStruct
552+
553+
\`\`\`
554+
555+
\`\`\`{eval-rst}
556+
.. rust:enum:: mylib::MyEnum
557+
558+
\`\`\`
559+
560+
\`\`\`{eval-rst}
561+
.. rust:function:: mylib::my_function
562+
563+
\`\`\`
564+
````

pyproject.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,7 @@ projects = { calculator = "examples/cpp/xml" }
195195
default-project = "calculator"
196196

197197
[tool.yardang.sphinx-rust]
198-
rust_crates = [
198+
crates = [
199199
"examples/rust",
200200
]
201+
doc-formats = { calculator = "myst-nb" }

yardang/build.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -319,7 +319,6 @@ def customize(args):
319319
"toctree",
320320
"literalinclude",
321321
"include",
322-
"eval-rst",
323322
],
324323
"nb_execution_mode": "off",
325324
"nb_execution_excludepatterns": [],

0 commit comments

Comments
 (0)