smalldocs.api_rust

The API reference of a Rust crate, made from rustdoc’s JSON: one page per module, as the Python pages are.

api: language: rust manifest: ../Cargo.toml # the crate, from the site’s folder; the root’s Cargo.toml by default toolchain: nightly # the toolchain that runs rustdoc, which needs -Z unstable-options private: false # document private items too; a crate with only a binary always does out: api source: https://github.com/me/mycrate/blob/main

rustdoc writes what it knows of the crate as JSON. It does not keep the comment after a parameter, so that comment is read from the source, by the lines that the JSON gives. Only the standard library is used.

cargo

source

cargo(args: list[str], s: Settings, env: dict | None = None, cwd: Path | None = None) -> str

Run cargo with the toolchain of the settings; stop with a clear message when cargo or the toolchain is missing, or cargo fails.

Parameter Type Default Description
args list[str] required The arguments after cargo +TOOLCHAIN
s Settings required The settings, for the toolchain
env dict | None None More environment variables
cwd Path | None None The folder cargo runs in
returns str What cargo wrote to its standard output

cache_folder

source

cache_folder() -> Path

Make the folder that holds rustdoc’s output and lock files: one per user, that only the user can write, so that no one else can plant files in it.

Parameter Type Default Description
returns Path The user’s own folder for rustdoc’s output, in the temporary folder

last_use

source

last_use(work: Path) -> float

Tell when a build last used a crate’s folder: rustdoc writes target/doc each time, and smalldocs 0.5.13 writes manifest.

Parameter Type Default Description
work Path required A crate’s folder in the cache
returns float The newest time of its parts that each build writes; 0 when none is there

gone

source

gone(work: Path) -> bool

Tell whether the crate of a folder in the cache is gone, as with a removed worktree.

Parameter Type Default Description
work Path required A crate’s folder in the cache
returns bool Whether its mark names a Cargo.toml that is not there; no mark, or one that cannot be read, is never gone

prune_cache

source

prune_cache(folder: Path, keep: Path) -> None

Once a day, remove the folders of the cache whose Cargo.toml is gone, and those that no build has used for UNUSED_DAYS.

Parameter Type Default Description
folder Path required The cache, as cache_folder gives it
keep Path required The folder of the crate that is being built, which is never removed

rustdoc

source

rustdoc(s: Settings, private: bool = False) -> tuple[dict, Path]

Run rustdoc on the crate, with its output and its lock file in a temporary folder, so that nothing is written into the project.

Parameter Type Default Description
s Settings required The settings
private bool False Document private items too
returns tuple[dict, Path] rustdoc’s JSON of the crate, and the folder its file names start from (the workspace’s root)

path_text

source

path_text(p: dict) -> str

Write a path as the source writes it, so that type Result<T> = std::result::Result<T, E> does not read as itself.

Parameter Type Default Description
p dict required A path of rustdoc’s JSON: path, id and args
returns str The path as the source writes it, with its generic arguments, such as io::Error or Option<T>

written_path

source

written_path(path: str, source: str) -> str

Find how the source writes a path: rustdoc gives std::path::Path for a Path that a use brings in, and io::Error as it is.

Parameter Type Default Description
path str required A path as rustdoc gives it, which may be longer than the source’s, such as std::path::Path for Path
source str required The source of the entry that shows it; empty when there is none
returns str The longest end of the path that the source writes, such as io::Error; the last name when the source writes none

args_text

source

args_text(args: dict | str | None) -> str

Write the generic arguments of a path.

Parameter Type Default Description
args dict | str | None required The generic arguments of a path
returns str They as Rust writes them, such as <T, Item = u8> or (u8) -> bool

bounds_text

source

bounds_text(bounds: list[dict]) -> str

Write a list of bounds, such as Clone + 'a.

Parameter Type Default Description
bounds list[dict] required The bounds of a generic, a trait or an impl Trait
returns str They joined by +

type_text

source

type_text(t: dict | None) -> str

Write a type as Rust does: &mut [u8], Option<T>, impl Fn(u8) -> bool.

Parameter Type Default Description
t dict | None required A type of rustdoc’s JSON
returns str The type as Rust writes it, each path as the entry’s source writes it, or by its last name

generic_params_text

source

generic_params_text(params: list[dict]) -> str

Write the generic parameters of an item, without the ones that an impl Trait argument makes.

Parameter Type Default Description
params list[dict] required The generic parameters of an item
returns str They in angle brackets, such as <'a, T: Clone>; empty when there are none

where_text

source

where_text(generics: dict) -> str

Write the where clause of an item.

Parameter Type Default Description
generics dict required The generics of an item
returns str Its where clause, such as where T: Debug; empty when it has none

visibility

source

visibility(item: dict, here: str = '', at_root: bool = False) -> str

Write an item’s visibility, as its source does; rustdoc writes a private item as one restricted to its own module, which at the root is crate.

Parameter Type Default Description
item dict required An item of rustdoc’s JSON
here str '' The id of the module the item is in
at_root bool False Whether that module is the crate’s root
returns str pub , pub(crate) and so on; empty for a private item, or one in a trait

self_text

source

self_text(t: dict) -> str

Write a method’s self parameter as its source does.

Parameter Type Default Description
t dict required The type of a self parameter
returns str self, &self, &mut self or self: Box<Self>

fn_signature

source

fn_signature(item: dict, name: str, here: str = '', at_root: bool = False, lines: list[str] | None = None) -> str

Write a function’s signature: its visibility, its qualifiers, its generics, its parameters, its return and its where clause.

Parameter Type Default Description
item dict required A function or method of rustdoc’s JSON
name str required Its name
here str '' The id of the module it is in
at_root bool False Whether that module is the crate’s root
lines list[str] | None None The lines of its source file, to show a pattern parameter as written
returns str Its signature as Rust, on one line, or with a parameter per line when that is long

split_comment

source

split_comment(line: str) -> tuple[str, str]

Split a line of Rust into its code and its // comment, outside strings, character literals and /* */ comments.

Parameter Type Default Description
line str required One line of Rust
returns tuple[str, str] The code before a // comment, and the comment; a doc comment, /// or //!, is no comment here

signature_lines

source

signature_lines(lines: list[str], item: dict, name: str) -> list[tuple[str, str, bool, bool]]

Find the lines of a function’s signature, from fn NAME to the { that opens its body, or the ; that ends it; the -> of the return is the one outside brackets, generics and the where clause.

Parameter Type Default Description
lines list[str] required The lines of the source file
item dict required A function or method of rustdoc’s JSON, with its span
name str required Its name
returns list[tuple[str, str, bool, bool]] For each line of the signature: its code, its comment, whether it has the ->, and whether it opens the body

source_params

source

source_params(lines: list[str], item: dict, name: str) -> list[str]

Read each parameter as the source writes it, such as State(state), which rustdoc’s JSON names __arg0 in an async fn and State in another.

Parameter Type Default Description
lines list[str] required The lines of the source file
item dict required A function or method of rustdoc’s JSON, with its span
name str required Its name
returns list[str] The text before the : of each parameter but self, as written; empty when the source does not match the JSON

without_attributes

source

without_attributes(text: str) -> str

Take out the attributes of a parameter, following brackets and strings to the ] that closes each.

Parameter Type Default Description
text str required A parameter as written
returns str It without its attributes, such as #[cfg_attr(all(), doc = "[a]")]

depth_at

source

depth_at(text: str, k: int) -> int

Count the brackets open before a place in a parameter, so that a : inside a pattern such as S { a: x } is not its type’s.

Parameter Type Default Description
text str required A parameter as written
k int required A place in it
returns int How many brackets are open there

param_names

source

param_names(lines: list[str], item: dict, name: str) -> list[tuple[str, dict, list[str], str]]

Name each parameter: rustdoc’s name, unless rustdoc lost the pattern (__arg0, or W for W(x)); then the source’s text. List the names it binds.

Parameter Type Default Description
lines list[str] required The lines of the source file
item dict required A function or method of rustdoc’s JSON
name str required Its name in the source
returns list[tuple[str, dict, list[str], str]] For each parameter but self: the name to show, its type, the names it binds, and its text in the source

described

source

described(shown: str, binds: list[str], said: dict[str, str], args: dict[str, str]) -> str

Find a parameter’s description: the comment after it, or its # Arguments entry under its name, or the entries of the names its pattern binds.

Parameter Type Default Description
shown str required A parameter’s name as the page shows it, such as x or State(state)
binds list[str] required The names its pattern binds, such as state
said dict[str, str] required The comment after each parameter, by its shown name
args dict[str, str] required The descriptions of the # Arguments list
returns str Its description; empty when there is none

param_pattern

source

param_pattern(name: str) -> str

Make the regular expression that finds a parameter in a signature: name:, mut name:, r#name:, or a pattern with any spacing.

Parameter Type Default Description
name str required A parameter as rustdoc names it: a, or a pattern such as (b, c)
returns str A regular expression that finds it before its : in the source

param_comments

source

param_comments(lines: list[str], item: dict, name: str) -> tuple[dict[str, str], str]

Read the docments of a function: the // comment on a parameter’s line, and the one on the line of -> or of the {.

Parameter Type Default Description
lines list[str] required The lines of the source file
item dict required A function or method of rustdoc’s JSON
name str required Its name
returns tuple[dict[str, str], str] The comment after each parameter, under its name, and the comment on the return

line_comment

source

line_comment(lines: list[str], item: dict, shared: set[int] = frozenset()) -> str

Read the comment after a field or a variant, such as pub y: f64, // Up, on a line that is its own.

Parameter Type Default Description
lines list[str] required The lines of the source file
item dict required A field or a variant of rustdoc’s JSON, with its span
shared set[int] frozenset() Lines that hold more than this field, such as pub struct P { pub x: u8, pub y: u8 } // a point
returns str The // comment on its first line or its last; empty when there is none

section

source

section(text: str, name: str) -> tuple[list[str], str] | None

Find a section of a doc comment by its heading, outside code blocks; it runs to the next heading of its level or above.

Parameter Type Default Description
text str required A doc comment
name str required A heading’s text, such as Returns
returns tuple[list[str], str] | None The section’s lines, and the doc comment without it; None when it has no such section

returns_section

source

returns_section(text: str) -> tuple[str, str]

Read a # Returns section, the Rust convention for what a function gives back; one paragraph of prose moves into the table, and a longer section stays.

Parameter Type Default Description
text str required A doc comment
returns tuple[str, str] What its # Returns section says, on one line for the table, and the doc comment, without the section when the table holds all of it

arguments_section

source

arguments_section(text: str) -> tuple[dict[str, str], str]

Read a # Arguments list, with lines such as * `a` - the first number, and take it out of the doc comment.

Parameter Type Default Description
text str required A doc comment
returns tuple[dict[str, str], str] The description of each parameter in its # Arguments list, and the doc comment without that section

class Entry

source

Entry(id: str, name: str, anchor: str, level: int, module: str)

One entry on a page: an item, its heading, its anchor and its level.

Parameter Type Default Description
id str required The item’s id in rustdoc’s JSON
name str required The name in its heading, such as Point or Point::distance
anchor str required The id of its heading, such as Point.distance
level int required The heading level: 2 for an item of the module, 3 for a method
module str required The full name of its module, such as mycrate::parse

class Page

source

Page(id: str, path: str, file: str = '', entries: list[Entry] = <factory>, reexports: list[tuple[str, str]] = <factory>, modules: list[str] = <factory>)

One module’s page: the module, its file name and its entries.

Parameter Type Default Description
id str required The module’s id in rustdoc’s JSON
path str required Its full name, such as mycrate::parse
file str '' The page’s file, from the folder of pages, such as cli/commands/index.qmd
entries list[Entry] <factory> Its entries, in the order of the page
reexports list[tuple[str, str]] <factory> The names it re-exports from elsewhere, with the id of each
modules list[str] <factory> The ids of its own modules

page_file

source

page_file(page: Page, taken: set[str] = frozenset()) -> str

Place a module’s page: the crate’s root at mycrate.qmd, a module with modules in a/b/index.qmd, and one without at a/b.qmd.

Parameter Type Default Description
page Page required A module’s page, with its modules found
taken set[str] frozenset() Pages that someone wrote, which a build never writes over
returns str Its file, from the folder of pages: the folders follow the modules

unique

source

unique(file: str, used: set[str]) -> str

Give a page a file that no other page has, such as a module mycrate::mycrate beside the root mycrate.qmd.

Parameter Type Default Description
file str required A page’s file, from the folder of pages
used set[str] required The files that other pages have, in lower case, as a disk may not tell A.qmd from a.qmd; the new file joins it
returns str The file, or name-module.qmd, name-module-2.qmd and so on when another page has it

class Crate

source

Crate(data: dict, root: Path, out: str = 'api', taken: set[str] = frozenset())

A crate as rustdoc’s JSON describes it, with a page for each module and an entry for each item.

Parameter Type Default Description
data dict required rustdoc’s JSON
root Path required The folder that the file names of the spans start from
out str 'api' The folder of the pages, inside the site
taken set[str] frozenset() Pages in that folder that someone wrote, from the folder, such as cli/index.qmd

Crate.field_ids

source

Crate.field_ids(item: dict) -> list[int]

List the ids of the fields of a struct or a union: named, in a tuple, or none.

Parameter Type Default Description
item dict required A struct or a union
returns list[int] The ids of the fields that the JSON keeps, in order

Crate.shared_lines

source

Crate.shared_lines(id: str | int) -> set[int]

Find the lines whose comment belongs to no single field, such as pub struct P { pub x: u8, pub y: u8 } // a point.

Parameter Type Default Description
id str | int required The id of a field or a variant
returns set[int] The lines that hold more than one field or variant of its owner, or the owner’s own first line

Crate.item

source

Crate.item(id: str | int) -> dict | None

Find an item by its id.

Parameter Type Default Description
id str | int required An item’s id
returns dict | None The item, or None when the JSON leaves it out

Crate.kind

source

Crate.kind(id: str | int) -> str

Say what kind of item an id is.

Parameter Type Default Description
id str | int required An item’s id
returns str Its kind, such as function or struct; empty when it is not in the JSON

Crate.walk

source

Crate.walk(id: str, parents: list[str]) -> None

Make the page of a module and of each module in it, and give each item its page; a re-exported item from a hidden module joins the page that re-exports it.

Parameter Type Default Description
id str required A module’s id
parents list[str] required The names of the modules it is in

Crate.claim

source

Crate.claim(page: Page, id: str) -> bool

Make a module, that has no page yet, a module of this page, to be walked after the page’s items.

Parameter Type Default Description
page Page required A module’s page
id str required The id of a module in it, or that it re-exports
returns bool Whether the module is now one of the page’s modules

Crate.adopt

source

Crate.adopt(page: Page, id: str, seen: set[str]) -> None

Give a re-exported item an entry on this page when it has no page of its own yet; otherwise list it as a re-export.

Parameter Type Default Description
page Page required The page of the module that has the pub use
id str required The id of the use item
seen set[str] required The hidden modules whose items a glob has already brought here, so that a cycle of globs ends

Crate.public_home

source

Crate.public_home(id: str) -> str | None

Say whether an item will have an entry where it is defined, so that a re-export of it is only a link.

Parameter Type Default Description
id str required An item’s id
returns str | None The module it is defined in, when that module has a page of its own; otherwise None

Crate.place

source

Crate.place(page: Page, id: str, name: str | None = None) -> None

Give an item its entry on a page, with the entries of its methods under it.

Parameter Type Default Description
page Page required The page the item goes on
id str required The item’s id
name str | None None The name it has there, when a pub use ... as renames it

Crate.members

source

Crate.members(id: str) -> list[tuple[str, str]]

List the methods of a type’s own impl blocks, or the items of a trait.

Parameter Type Default Description
id str required The id of a struct, an enum, a union or a trait
returns list[tuple[str, str]] The id and the name of each method and associated item that gets an entry under it

Crate.trait_impls

source

Crate.trait_impls(id: str) -> list[dict]

List the traits a type implements in its own code, or derives.

Parameter Type Default Description
id str required The id of a struct, an enum or a union
returns list[dict] The paths of the traits it implements; not the auto traits, such as Send, nor the blanket impls, such as From<T>

Crate.source

source

Crate.source(item: dict) -> list[str]

Read the lines of an item’s source file, once per file.

Parameter Type Default Description
item dict required An item with a span
returns list[str] The lines of its source file; empty when the file cannot be read

Crate.url

source

Crate.url(id: str | int | None) -> str | None

Find where an item is documented: on a page of this site, or in the docs of another crate.

Parameter Type Default Description
id str | int | None required An item’s id, from a link or a path
returns str | None A link to its entry, to its page, or to the docs of the crate it is from; None when it has none

Crate.external_url

source

Crate.external_url(id: str) -> str | None

Link an item of another crate to its docs, as rustdoc’s pages do.

Parameter Type Default Description
id str required The id of an item of another crate
returns str | None A link to its page in that crate’s docs: its html_root_url, or docs.rs

Crate.external_kind

source

Crate.external_kind(path: list[str], crate_id: int) -> str

Find the kind of another crate’s item by its path, to link a method on its type’s page.

Parameter Type Default Description
path list[str] required The full path of an item of another crate
crate_id int required The crate’s id
returns str The item’s kind; empty when the JSON does not list it

fence_run

source

fence_run(stripped: str) -> str

Find the fence mark a line starts with.

Parameter Type Default Description
stripped str required A line of Markdown, without its indent
returns str The run of three or more backticks or tildes it starts with, such as “````”; empty when it starts no fence

closes

source

closes(stripped: str, fence: str) -> bool

Tell whether a line closes a fenced code block, as CommonMark says.

Parameter Type Default Description
stripped str required A line of Markdown, without its indent, inside a fence
fence str required The mark that opened the fence
returns bool Whether the line closes it: as many marks of the same kind or more, and nothing after

tidy_docs

source

tidy_docs(text: str, shift: int) -> str

Make a doc comment fit a page: its headings under the entry’s, a code block with no language as Rust, and no lines hidden with # .

Parameter Type Default Description
text str required A doc comment, as Markdown
shift int required How many levels to move its headings down, so that they sit under the entry’s heading
returns str The same Markdown, with its headings moved down and its Rust examples as rustdoc shows them

summary

source

summary(text: str) -> str

Read the summary of a doc comment: its first paragraph.

Parameter Type Default Description
text str required A doc comment
returns str Its first paragraph on one line, with links as their text; empty when it starts with a heading or code

code

source

code(text: str) -> str

Put Rust code in a code block.

Parameter Type Default Description
text str required Rust code
returns str It in a rust code block

fn_table

source

fn_table(crate: Crate, item: dict, args: dict[str, str], gives: str = '') -> str

Describe a function by its parameters and its return: the type and the comment of each.

Parameter Type Default Description
crate Crate required The crate
item dict required A function or method
args dict[str, str] required The descriptions of its # Arguments list
gives str '' The text of its # Returns section
returns str A Markdown table of its parameters and its return: type, description

fields_of

source

fields_of(crate: Crate, item: dict) -> list[tuple[str, dict]]

List the fields of a struct or a union.

Parameter Type Default Description
crate Crate required The crate
item dict required A struct or a union
returns list[tuple[str, dict]] The name and the item of each field that the JSON keeps, in order

describe

source

describe(crate: Crate, item: dict) -> str

Describe a field or a variant: its doc comment, with its links, or the comment after it, on a line that holds only it.

Parameter Type Default Description
crate Crate required The crate
item dict required A field or a variant
returns str Its doc comment on one line, or else the comment after it

variant_text

source

variant_text(crate: Crate, item: dict) -> str

Write a variant of an enum.

Parameter Type Default Description
crate Crate required The crate
item dict required A variant
returns str It as Rust writes it, such as Rgb(u8, u8, u8) or Named { name: String }

item_signature

source

item_signature(crate: Crate, item: dict, name: str, here: str = '') -> str

Write the declaration of a struct, an enum, a trait, a type, a constant, a static or a macro.

Parameter Type Default Description
crate Crate required The crate
item dict required An item that is not a function
name str required The name it has on the page
here str '' The id of the module it is in
returns str Its declaration as Rust, without its body

written_value

source

written_value(lines: list[str], item: dict) -> str

Read the value of a constant or a static from its source, when rustdoc gives _, as it does for an array such as ["a", "b"].

Parameter Type Default Description
lines list[str] required The lines of the source file
item dict required A constant or a static, with its span
returns str Its value as the source writes it, without comments; empty when it cannot be read

impl_line

source

impl_line(crate: Crate, id: str) -> str

List the traits a type implements, each by its name, linked to its docs.

Parameter Type Default Description
crate Crate required The crate
id str required The id of a struct, an enum or a union
returns str A line such as “Implements Display, Clone.”, with links; empty when it implements no trait itself

implementors_line

source

implementors_line(crate: Crate, item: dict) -> str

List the types of the crate that implement a trait.

Parameter Type Default Description
crate Crate required The crate
item dict required A trait
returns str A line such as “Implemented for Circle.”, with links; empty when nothing in the crate implements it

item_source

source

item_source(crate: Crate, item: dict) -> str

Read the code of an item, to show its paths as it writes them; a function’s body is left out, as its paths are not its signature’s.

Parameter Type Default Description
crate Crate required The crate
item dict required An item with a span
returns str Its source without comments: a function’s signature, or the whole of any other item; empty when it cannot be read

entry_markdown

source

entry_markdown(s: Settings, crate: Crate, e: Entry) -> str

Write one entry, with each path in it as the entry’s source writes it.

Parameter Type Default Description
s Settings required The settings
crate Crate required The crate
e Entry required One entry
returns str Its Markdown: heading, source link, signature, doc comment and tables

entry_parts

source

entry_parts(s: Settings, crate: Crate, e: Entry) -> str

Write one entry: a heading with its id, a source link, the signature, the doc comment and the table of its parameters, fields or variants.

Parameter Type Default Description
s Settings required The settings
crate Crate required The crate
e Entry required One entry
returns str Its Markdown: heading, source link, signature, doc comment and tables

page_markdown

source

page_markdown(s: Settings, crate: Crate, page: Page) -> str

Write one module’s page: front matter, the module’s doc comment, its modules and re-exports, then an entry for each item.

Parameter Type Default Description
s Settings required The settings
crate Crate required The crate
page Page required One module’s page
returns str The text of its .qmd page

modules_table

source

modules_table(crate: Crate, pages: list[Page]) -> str

List some modules, each by its own name, linked to its page, with its summary.

Parameter Type Default Description
crate Crate required The crate
pages list[Page] required The pages of some modules
returns str A Markdown table with a link to each, by its name, and its summary

symbols

source

symbols(crate: Crate) -> dict[str, str]

Make the index of names that links use: Point, Point::distance, Point.distance and mycrate::Point::distance.

Parameter Type Default Description
crate Crate required The crate
returns dict[str, str] Each name, under its short form and its full path, with the link to its entry

load

source

load(s: Settings, private: bool = False, taken: set[str] = frozenset()) -> Crate

Run rustdoc and read its JSON.

Parameter Type Default Description
s Settings required The settings
private bool False Document private items too
taken set[str] frozenset() Pages that someone wrote in the folder of pages
returns Crate The crate, read from rustdoc’s JSON

build

source

build(s: Settings) -> list[Path]

Write a reference page per module of the crate, in folders that follow the modules, an index.qmd for each folder with none, and _symbols.json.

Parameter Type Default Description
s Settings required The settings
returns list[Path] The pages written

entry_gaps

source

entry_gaps(crate: Crate, e: Entry) -> list[str]

Find what one entry is missing: a doc comment, a docment for a parameter or the return, or a comment for a field or a variant.

Parameter Type Default Description
crate Crate required The crate
e Entry required One entry
returns list[str] One message per gap; empty when the entry is documented

check

source

check(s: Settings, shown: bool = False) -> int

Print how much of each module is documented, then every gap, then the total.

Parameter Type Default Description
s Settings required The settings
shown bool False Hold the crate only to what its pages show; otherwise its private items too
returns int The exit code: 0 when nothing is missing, 1 otherwise