cargo_readme/readme/
mod.rs

1use std::io::Read;
2use std::path::Path;
3
4mod extract;
5mod process;
6mod template;
7
8use crate::config;
9
10/// Generates readme data from `source` file
11///
12/// Optionally, a template can be used to render the output
13pub fn generate_readme<T: Read>(
14    project_root: &Path,
15    source: &mut T,
16    template: Option<&mut T>,
17    add_title: bool,
18    add_badges: bool,
19    add_license: bool,
20    indent_headings: bool,
21) -> Result<String, String> {
22    let lines = extract::extract_docs(source).map_err(|e| format!("{}", e))?;
23
24    let readme = process::process_docs(lines, indent_headings).join("\n");
25
26    // get template from file
27    let template = if let Some(template) = template {
28        Some(get_template_string(template)?)
29    } else {
30        None
31    };
32
33    // get manifest from Cargo.toml
34    let cargo = config::get_manifest(project_root)?;
35
36    template::render(template, readme, &cargo, add_title, add_badges, add_license)
37}
38
39/// Load a template String from a file
40fn get_template_string<T: Read>(template: &mut T) -> Result<String, String> {
41    let mut template_string = String::new();
42    match template.read_to_string(&mut template_string) {
43        Err(e) => return Err(format!("Error: {}", e)),
44        _ => {}
45    }
46
47    Ok(template_string)
48}