cargo-readme 3.4.0

A cargo subcommand to generate README.md content from doc comments
Documentation
//! Generate README.md from doc comments.

use clap::Parser;
use std::io;
use std::io::Write;

mod helper;

fn main() {
    let args = Args::parse();
    let result = match &args.command {
        Command::Readme(readme_args) => execute(readme_args),
    };
    if let Err(e) = result {
        io::stderr()
            .write_fmt(format_args!("Error: {}\n", e))
            .expect("An error occurred while trying to show an error message");
        std::process::exit(1);
    }
}
/// The command line interface for setting up a Bottlerocket TestSys cluster and running tests.
#[derive(Debug, Parser)]
#[clap(author, version, about)]
struct Args {
    #[clap(subcommand)]
    command: Command,
}

#[derive(Debug, Parser)]
enum Command {
    /// Generate README.md from doc comments
    Readme(ReadmeArgs),
}

/// Generate README.md from doc comments
#[derive(Debug, Parser)]
#[clap(author, version, about)]
struct ReadmeArgs {
    /// Do not prepend badges line.
    /// By default, badges defined in Cargo.toml are prepended to the output.
    /// Ignored when using a template.
    #[clap(long)]
    no_badges: bool,

    /// Do not add an extra level to headings.
    /// By default, '#' headings become '##', so the first '#' can be the crate name. Use this
    /// option to prevent this behavior.
    #[clap(long)]
    no_indent_headings: bool,

    /// Do not append license line.
    /// By default, the license defined in `Cargo.toml` will be prepended to the output.
    /// Ignored when using a template.
    #[clap(long)]
    no_license: bool,

    /// Ignore template file when generating README.
    /// Only useful to ignore default template `README.tpl`.
    #[clap(long)]
    no_template: bool,

    /// Do not prepend title line.
    /// By default, the title ('# crate-name') is prepended to the output.
    #[clap(long)]
    no_title: bool,

    /// Do not extract docs from comment. Instead, just process the input file
    /// as it is.
    /// By default, this flag is `false`.
    #[clap(long)]
    no_comment_extraction: bool,

    /// List the badges that can be rendered from the `[badges]` section of `Cargo.toml`,
    /// along with the attributes each one reads, then exit.
    #[clap(long)]
    list_badges: bool,

    /// File to read from.
    /// If not provided, will try to use `src/lib.rs`, then `src/main.rs`. If neither file
    /// could be found, will look into `Cargo.toml` for a `[lib]`, then for a single `[[bin]]`.
    /// If multiple binaries are found, an error will be returned.
    #[clap(long, short = 'i')]
    input: Option<String>,

    /// File to write to. If not provided, will output to stdout.
    #[clap(long, short = 'o')]
    output: Option<String>,

    /// Directory to be set as project root (where `Cargo.toml` is)
    /// Defaults to the current directory.
    #[clap(long = "project-root", short = 'r')]
    root: Option<String>,

    /// Template used to render the output.
    /// Default behavior is to use `README.tpl` if it exists.
    #[clap(long, short = 't')]
    template: Option<String>,
}

// Takes the arguments matches from clap and outputs the result, either to stdout of a file
fn execute(args: &ReadmeArgs) -> Result<(), String> {
    if args.list_badges {
        return list_badges();
    }

    // get project root
    let project_root = helper::get_project_root(args.root.as_deref())?;

    // get source file
    let mut source = helper::get_source(&project_root, args.input.as_deref())?;

    // get destination file
    let mut dest = helper::get_dest(args.output.as_deref())?;

    // get template file
    let mut template_file = if args.no_template {
        None
    } else {
        helper::get_template_file(&project_root, args.template.as_deref())?
    };

    let options = cargo_readme::ReadmeOptions {
        add_title: !args.no_title,
        add_badges: !args.no_badges,
        add_license: !args.no_license,
        indent_headings: !args.no_indent_headings,
        extract_from_comment: !args.no_comment_extraction,
    };

    // generate output
    let readme =
        cargo_readme::generate_readme(&project_root, &mut source, template_file.as_mut(), options)?;

    helper::write_output(&mut dest, readme)
}

// Print the supported badges and the attributes each one reads. Format mirrors the
// `[badges]` table so the output can be copied into a Cargo.toml.
fn list_badges() -> Result<(), String> {
    let mut out = String::from("Supported badges (add under [badges] in Cargo.toml):\n\n");
    for badge in cargo_readme::supported_badges() {
        out.push_str("  ");
        out.push_str(badge.key);
        if !badge.official {
            out.push_str("  (cargo-readme extension)");
        }
        out.push('\n');
        if !badge.required.is_empty() {
            out.push_str(&format!("    required: {}\n", badge.required.join(", ")));
        }
        if !badge.optional.is_empty() {
            out.push_str(&format!("    optional: {}\n", badge.optional.join(", ")));
        }
    }
    print!("{}", out);
    Ok(())
}