fn(Code) -> Docs
Overview
Doku is a framework for documenting Rust data structures - it allows to generate aesthetic, human-friendly descriptions of configuration types, requests / responses, and so on.
Say goodbye to stale, hand-written documentation - with Doku, code is the documentation!
Example
Say, you're writing an application that requires some TOML configuration to work:
use Deserialize;
Usually you'll want to create a config.example.toml
, describing the
configuration's format for your users to copy-paste and adjust:
= "pgsql" # or mysql
= "localhost"
= 5432
But writing such config.example.toml
by hand is both tedious to maintain
and error-prone, since there's no guarantee that e.g. someone won't rename a
field, forgetting to update the documentation.
Now, with Doku, all you need to do is add a few #[derive(Document)]
:
# use Deserialize;
use Document;
... and call doku::to_json()
/ doku::to_toml()
, which will generate the
docs for you!
# use Document;
# use Deserialize;
#
#
#
#
#
#
#
println!;
/*
# doku::assert_doc!(r#"
db_engine = "pgsql" | "mysql"
db_host = "string"
db_port = 123
# "#, doku::to_toml::<Config>());
*/
This automatically-generated documentation can be then fine-tuned e.g. by providing examples:
# use Document;
# use Deserialize;
#
#
#
#
println!;
/*
# doku::assert_doc!(r#"
## Database's engine
db_engine = "pgsql" | "mysql"
## Database's host
db_host = "localhost"
## Database's port
db_port = 5432
# "#, doku::to_toml::<Config>());
*/
And voilĂ , ready to deploy!
What's more -- because doku::to_json()
returns a good-old String
, it's
possible to create a test to make sure your docs always stay up-to-date:
use std::fs;
#[test]
fn docs() {
let expected_docs = doku::to_toml::<Config>();
let actual_docs = fs::read_to_string("config.example.toml").unwrap();
if actual_docs != expected_docs {
fs::write("config.example.toml.new", actual_docs);
panic!("`config.example.toml` is stale");
}
}
Let go & let the pipelines worry about your docs!
Plug and Play
Doku has been made with plug-and-play approach in mind - it understands most
of the Serde's annotations and comes with a predefined, curated formatting
settings, so that just adding #[derive(Document)]
should get you started
quickly & painlessly.
At the same time, Doku is extensible - if the formatting settings don't
match your taste, you can tune them; if the derive macro doesn't work
because you've got a custom impl Serialize
, you can write impl Document
by hand as well.
So - come join the doc side!
Limitations
Formats
At the moment Doku provides functions for rendering JSON-like and TOML-like documents.
All models used by Doku are public though, so if you wanted, you could very easily roll your own pretty-printer, for you own custom format:
println!;
Annotations
Doku understands most of Serde's annotations, so e.g. the following will work as expected:
# use Document;
# use Serialize;
#
If you're not using Serde, but you'd like to pass Serde-like attributes for Doku to understand, there's also:
# use Document;
#
Language features
Doku supports most of Rust language's & standard library's features (such as strings, vectors, maps or generic types); the only exceptions are recursive types (which will cause the pretty-printers to panic, since they don't support those).
Some external crates (such as chrono
or url
) are supported behind
feature-flags.
How does it work?
When you wrap a type with #[derive(Document)]
:
# use Document;
#
... the macro will generate an impl doku::Document
:
# ;
#
... that will be invoked later, when you call doku::to_*()
:
#
#
#
There's no magic, no RTTI hacks, no unsafety - it's all just-Rust.