1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
use std::io::Read;
use std::path::Path;

mod extract;
mod transform;
mod template;

use self::transform::DocTransform;
use cargo_info;

/// Generates readme data from `source` file
///
/// Optionally, a template can be used to render the output
pub fn generate_readme<T: Read>(
    project_root: &Path,
    source: &mut T,
    template: Option<&mut T>,
    add_title: bool,
    add_license: bool,
    indent_headings: bool,
) -> Result<String, String> {

    let readme = extract::extract_docs(source)
        .map_err(|e| format!("{}", e))?
        .into_iter()
        .transform_doc(indent_headings)
        .fold(String::new(), |mut acc, x| {
            if !acc.is_empty() {
                acc.push('\n');
            }
            acc.push_str(&x);
            acc
        });

    // get template from file
    let template = if let Some(template) = template {
        Some(get_template_string(template)?)
    } else {
        None
    };

    // get cargo info from Cargo.toml
    let cargo = cargo_info::get_cargo_info(project_root)?;
    if add_license && cargo.package.license.is_none() {
        return Err("License not found in Cargo.toml".to_owned());
    }

    template::render(template, readme, cargo, add_title, add_license)
}

/// Load a template String from a file
fn get_template_string<T: Read>(template: &mut T) -> Result<String, String> {
    let mut template_string = String::new();
    match template.read_to_string(&mut template_string) {
        Err(e) => return Err(format!("Error: {}", e)),
        _ => {}
    }

    Ok(template_string)
}