syncdoc
syncdoc is a procedural macro that automatically injects documentation from external files into your Rust code, eliminating the need to manually maintain inline doc comments.
Use syncdoc when you want to keep documentation separate from implementation.
Stick with inline docs when you prefer co-location of docs and code.
Motivation
When writing extensive documentation, keeping it inline can make code harder to read:
/// This is a very long doc comment
/// that spans many lines and makes
/// the actual function hard to see...
/// [more lines]
/// Another long doc comment
/// [many more lines]
syncdoc solves this by automatically pulling documentation from external files:
use omnidoc;
Installation
Add syncdoc to your Cargo.toml:
[]
= "0.1"
Usage
Note: In a future release, you'll be able to configure a default documentation path, eliminating the need to specify
pathin every attribute.
Basic Usage
Apply the #[omnidoc] attribute to any module:
use omnidoc;
This will look for documentation in:
../docs/my_functions/foo.md../docs/my_functions/bar.md
Note: you cannot use a proc macro on an external module, see this tracking issue.
A workaround to document an entire module is to inline the entire module (
mod mymodule { ... }) then re-export it withpub use mymodule::*;. If you do, note that the name of the inner module is the name the macro will look for at the path.If that isn't to your liking, then just use it on impl blocks etc. and use a regular
syncdocattribute for individual items.
Documenting Impl Blocks
syncdoc also works on impl blocks:
use omnidoc;
;
Documentation files:
../docs/Calculator/new.md../docs/Calculator/add.md
Single Function Documentation
You can also document individual functions:
use syncdoc;
Documenting Structs and Enums
syncdoc automatically documents struct fields and enum variants:
use omnidoc;
Documentation files:
../docs/types/Config.md- struct documentation../docs/types/Config/port.md- field documentation../docs/types/Config/host.md- field documentation../docs/types/Status.md- enum documentation../docs/types/Status/Active.md- variant documentation../docs/types/Status/Inactive.md- variant documentation../docs/types/Status/Error.md- variant documentation
How It Works
syncdoc uses a procedural macro to inject #[doc = include_str!("path")] attributes before function definitions.
It uses proc-macro2 (it's free of syn!) to parse tokens rather than doing full AST creation.
Implementation Details
The macro:
- Parses tokens to find function definitions
- Constructs doc paths based on module hierarchy and function names
- Injects doc attributes using
include_str!for compile-time validation - Preserves existing attributes and doesn't interfere with other macros
For examples of the generated output, see the test snapshots which show the exact documentation attributes injected for various code patterns.
What Gets Documented
- Regular functions:
fn foo() { ... } - Generic functions:
fn foo<T>(x: T) { ... } - Methods in impl blocks:
impl MyStruct { fn method(&self) { ... } } - Trait default methods:
trait MyTrait { fn method() { ... } } - Struct fields:
struct Foo { field: i32 } - Enum variants:
enum Bar { Variant1, Variant2(i32) } - Type aliases:
type MyType = String; - Constants:
const X: i32 = 42; - Statics:
static Y: i32 = 42;
File Organization
my-project/
├── src/
│ ├── lib.rs
│ └── parser/
│ └── mod.rs #[omnidoc(path = "../../docs")]
└── docs/
└── parser/
├── parse_expr.md
└── parse_stmt.md
License
This project is licensed under either of:
- Apache License, Version 2.0, (LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0)
- MIT license (LICENSE-MIT or http://opensource.org/licenses/MIT)
at your option.