#macro-rules #clean #internal #hide #impl #docs #macro-export

macro clean-macro-docs

Hide internal rules when documenting macro_rules! macros

3 stable releases

1.0.2 Feb 18, 2021
1.0.1 Feb 17, 2021

#28 in #hide

Download history 7/week @ 2023-12-03 45/week @ 2023-12-10 3/week @ 2023-12-31 1/week @ 2024-01-14 73/week @ 2024-01-21 27/week @ 2024-01-28 2/week @ 2024-02-04 31/week @ 2024-02-18 14/week @ 2024-02-25 18/week @ 2024-03-03 20/week @ 2024-03-10

83 downloads per month
Used in comprende

MIT license

28KB
604 lines

Hide Internal Rules in macro_rules! Docs

When generating docs for macro_rules! macros, rustdoc will include every rule, including internal rules that are only supposed to be called from within your macro. The clean_docs attribute will hide your internal rules from rustdoc.

Example:

#[macro_export]
macro_rules! messy {
    (@impl $e:expr) => {
        format!("{}", $e)
    };
    ($e:expr) => {
        messy!(@impl $e)
    };
}

#[clean_docs]
#[macro_export]
macro_rules! clean {
    (@impl $e:expr) => {
        format!("{}", $e)
    };
    ($e:expr) => {
        clean!(@impl $e)
    };
}

would be documented as

macro_rules! messy {
    (@impl $e:expr) => { ... };
    ($e:expr) => { ... };
}

macro_rules! clean {
    ($e:expr) => { ... };
}

How does it work?

The clean! macro above is transformed into

#[macro_export]
macro_rules! clean {
    ($e:expr) => {
        $crate::__clean!(@impl $e)
    };
}

#[macro_export]
macro_rules! __clean {
    (@impl $e:expr) => {
        format!("{}", $e)
    };
}

macro_rules! clean {
    (@impl $e:expr) => {
        format!("{}", $e)
    };
    ($e:expr) => {
        clean!(@impl $e)
    };
}

The last, non-macro_exported macro is there becuase Rust doesn't allow macro-expanded macros to be invoked by absolute path (i.e. $crate::__clean).

The solution is to shadow the macro_exported macro with a local version that doesn't use absolute paths.

Arguments

You can use these optional arguments to configure clean_macro.

#[clean_docs(impl = "#internal", internal = "__internal_mac")]

impl

A string representing the "flag" at the begining of an internal rule. Defaults to "@".

#[clean_docs(impl = "#internal")]
#[macro_export]
macro_rules! mac {
    (#internal $e:expr) => {
        format!("{}", $e)
    };
    ($e:expr) => {
        mac!(#internal $e)
    };
}

internal

A string representing the identifier to use for the internal version of your macro. By default clean_docs prepends __ (two underscores) to the main macro's identifier.

#[clean_docs(internal = "__internal_mac")]
#[macro_export]
macro_rules! mac {
    (@impl $e:expr) => {
        format!("{}", $e)
    };
    ($e:expr) => {
        mac!(@impl $e)
    };
}

Dependencies

~1.5MB
~34K SLoC