documented_macros

Derive Macro DocumentedFields

source
#[derive(DocumentedFields)]
Expand description

Derive proc-macro for DocumentedFields trait.

§Example

use documented::DocumentedFields;

#[derive(DocumentedFields)]
struct BornIn69 {
    /// Cry like a grandmaster.
    rawr: String,
    /// Before what?
    explosive: usize,
};

assert_eq!(
    BornIn69::FIELD_DOCS,
    ["Cry like a grandmaster.", "Before what?"]
);

You can also use get_field_docs to access the fields’ documentation using their names.

assert_eq!(
    BornIn69::get_field_docs("rawr"),
    Ok("Cry like a grandmaster.")
);
assert_eq!(BornIn69::get_field_docs("explosive"), Ok("Before what?"));
assert_eq!(
    BornIn69::get_field_docs("gotcha"),
    Err(Error::NoSuchField("gotcha".to_string()))
);

§Configuration

With the customise feature enabled, you can customise this macro’s behaviour using the #[documented_fields(...)] attribute. Note that this attribute works on both the container and each individual field, with the per-field configurations overriding container configurations, which override the default.

Currently, you can (selectively) disable line-trimming like so:

#[derive(DocumentedFields)]
#[documented_fields(trim = false)]
struct Frankly {
    ///     Delicious.
    perrier: usize,
    ///     I'm vegan.
    #[documented_fields(trim = true)]
    fried_liver: bool,
}

assert_eq!(Frankly::FIELD_DOCS, ["     Delicious.", "I'm vegan."]);

If there are other configuration options you wish to have, please submit an issue or a PR.