pub struct Printer<'a> { /* private fields */ }Expand description
Printer is responsible for generating Markdown documentation from a rustdoc_types::Crate.
It uses a builder pattern for configuration. The typical workflow is:
- Create a
PrinterwithPrinter::new(&manifest, &krate). - Configure it using builder methods like
paths(),crate_extra(), etc. - Call
print()to generate the Markdown string.
§Features
- Path Filtering: Use
paths()to specify which items (and their dependencies) should be included in the documentation. - README and Examples: Include the crate’s README and examples using
crate_extra()with data fromCrateExtraReader. - “Other” Items: Control the inclusion of items not fitting standard categories
with
include_other(). - Template Mode: Generate template markers instead of documentation content
using
template_mode(), useful for identifying missing documentation. - Common Traits Summarization: By default, traits frequently implemented by types
are summarized. This can be disabled with
no_common_traits().
§Example
For a complete example of using Printer along with other parts of this crate
to generate documentation, see the crate-level documentation.
Implementations§
Source§impl<'a> Printer<'a>
impl<'a> Printer<'a>
Sourcepub fn new(manifest: &'a CargoManifest, krate: &'a Crate) -> Self
pub fn new(manifest: &'a CargoManifest, krate: &'a Crate) -> Self
Creates a new Printer instance.
§Arguments
manifest: The parsedCargo.tomldata for the crate, as acargo_manifest::Manifest.krate: Therustdoc_types::Cratedata produced byrustdoc.
Sourcepub fn paths(self, paths: &[String]) -> Self
pub fn paths(self, paths: &[String]) -> Self
Sets the item path filters for documentation generation.
Items matching these paths (and their dependencies) will be included.
§Path Matching Rules:
- Paths starting with
::(e.g.,::my_module::MyStruct) are treated as absolute within the current crate. - Paths without
::(e.g.,my_module::MyStructorMyStruct) are assumed to be relative to the crate root and will be prefixed with the crate name (e.g.,crate_name::my_module::MyStruct). - Matches are prefix-based. For example,
"::style"will match"::style::TextStyle"and"::style::Color".
If no paths are provided (the default), all items in the crate are considered for selection.
Sourcepub fn crate_extra(self, extra: CrateExtra) -> Self
pub fn crate_extra(self, extra: CrateExtra) -> Self
Adds CrateExtra data (README, examples) to be included in the documentation.
Use CrateExtraReader to obtain the CrateExtra instance.
Sourcepub fn include_other(self) -> Self
pub fn include_other(self) -> Self
Includes items that don’t fit standard categories in a final “Other” section.
By default, such items (e.g., unprinted selected items that are not modules, impls, or struct fields) are logged as warnings and not included in the output. If this method is called, these items will appear at the end of the documentation, potentially with their source location and dependency graph context.
Sourcepub fn template_mode(self) -> Self
pub fn template_mode(self) -> Self
Enables template mode for documentation output.
In template mode, instead of the actual documentation content for an item,
Mustache-like markers (e.g., {{MISSING_DOCS_1_2_1}}) are inserted.
This is useful for identifying where documentation is present or missing
in the source crate when generating documentation for LLM training or analysis.
The default is false (template mode disabled).
Sourcepub fn no_common_traits(self) -> Self
pub fn no_common_traits(self) -> Self
Disables the “Common Traits” summarization sections.
By default, traits that are frequently implemented by types within the crate or specific modules are summarized in “Common Traits” sections at the crate and module levels. This helps to reduce redundancy in the documentation.
If this method is called, these summary sections are omitted, and all implemented traits for each item will be listed directly with that item’s documentation.
Sourcepub fn print(self) -> Result<String>
pub fn print(self) -> Result<String>
Generates the Markdown documentation based on the configured options.
This method consumes the Printer and returns the generated Markdown as a String.
It performs several steps:
- Resolves module items (handling
usestatements). - Selects items based on path filters and builds a dependency graph.
- Calculates common traits for the crate.
- Prints the crate header, README (if any), and common traits.
- Recursively prints modules and their contents.
- Prints any remaining “other” items if configured.
- Appends examples if configured.
§Returns
A Result containing the generated Markdown String, or an error if
any step fails.