generate

Attribute Macro generate 

Source
#[generate]
Available on crate feature macros only.
Expand description

Add on for #[derive(Deserialize)] to add serde_args-specific information.

This attribute modifies an existing derived Deserialize implementation to include extra information specific to serde_args. Specifically, it can generate help messages from doc comments and version information from the crate’s metadata.

Note that #[serde_args::generate] must be placed above #[derive(Deserialize)]

serde_args::generate can take any of the following parameters:

  • doc_help
  • version

doc_help will generate help messages for the container, along with its fields/variants, using the item’s doc comments. For example, using doc help on the following struct:

use serde::Deserialize;
use std::path::PathBuf;

/// An example program.
#[serde_args::generate(doc_help)]
#[derive(Deserialize)]
struct Args {
    /// The file to be operated on.
    file: PathBuf,
    /// Whether the program's behavior should be forced.
    #[serde(alias = "f")]
    force: bool,
}

will generate help messages (to be displayed when --help is requested) for the container and each of the fields with the messages “An example program.”, “The file to be operated on.”, and “Whether the program’s behavior should be forced.”

version will activate the --version optional flag and include your crate’s version, extracted from your Cargo.toml’s version field. For example, it can be enabled by:

use serde::Deserialize;
use std::path::PathBuf;

#[serde_args_macros::generate(version)]
#[derive(Deserialize)]
struct Args {
    file: PathBuf,
    #[serde(alias = "f")]
    force: bool,
}

These parameters can also be combined. #[serde_args::generate(version, doc_help)] will generate both results on the same container.

Note that this attribute will wrap the serialized/deserialized type in a newtype. This has no effect on serde_args, but it could affect other formats if the same type is used across multiple formats.