Skip to content

Commit

Permalink
Init of --force flag
Browse files Browse the repository at this point in the history
  • Loading branch information
xoac committed Feb 5, 2020
1 parent 0655131 commit c046050
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 10 deletions.
5 changes: 5 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,9 @@ fn main() {
.help("Do not add an extra level to headings.{n}\
By default, '#' headings become '##', so the first '#' can be the crate \
name. Use this option to prevent this behavior.")))
.arg(Arg::with_name("FORCE")
.long("force")
.help("Return warning instead of error whenever possible."))
.get_matches();

if let Some(m) = matches.subcommand_matches("readme") {
Expand All @@ -101,6 +104,7 @@ fn execute(m: &ArgMatches) -> Result<(), String> {
let add_license = !m.is_present("NO_LICENSE");
let no_template = m.is_present("NO_TEMPLATE");
let indent_headings = !m.is_present("NO_INDENT_HEADINGS");
let force = m.is_present("FORCE");

// get project root
let project_root = helper::get_project_root(m.value_of("ROOT"))?;
Expand All @@ -127,6 +131,7 @@ fn execute(m: &ArgMatches) -> Result<(), String> {
add_badges,
add_license,
indent_headings,
force,
)?;

helper::write_output(&mut dest, readme)
Expand Down
11 changes: 10 additions & 1 deletion src/readme/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ pub fn generate_readme<T: Read>(
add_badges: bool,
add_license: bool,
indent_headings: bool,
force: bool,
) -> Result<String, String> {
let lines = extract::extract_docs(source).map_err(|e| format!("{}", e))?;

Expand All @@ -33,7 +34,15 @@ pub fn generate_readme<T: Read>(
// get manifest from Cargo.toml
let cargo = config::get_manifest(project_root)?;

template::render(template, readme, &cargo, add_title, add_badges, add_license)
template::render(
template,
readme,
&cargo,
add_title,
add_badges,
add_license,
force,
)
}

/// Load a template String from a file
Expand Down
38 changes: 29 additions & 9 deletions src/readme/template.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ pub fn render(
add_title: bool,
add_badges: bool,
add_license: bool,
force: bool,
) -> Result<String, String> {
let title: &str = &cargo.name;

Expand All @@ -21,7 +22,7 @@ pub fn render(
let version: &str = cargo.version.as_ref();

if let Some(template) = template {
process_template(template, readme, title, badges, license, version)
process_template(template, readme, title, badges, license, version, force)
} else {
process_string(
readme,
Expand Down Expand Up @@ -50,6 +51,7 @@ fn process_template(
badges: &[&str],
license: Option<&str>,
version: &str,
force: bool,
) -> Result<String, String> {
template = template.trim_end_matches("\n").to_owned();

Expand All @@ -63,20 +65,29 @@ fn process_template(

if template.contains("{{badges}}") {
if badges.is_empty() {
return Err("`{{badges}}` was found in template but no badges were provided".to_owned());
let msg = "`{{badges}}` was found in template but no badges were provided".to_owned();
if force {
println!("Warn: {}", msg);
} else {
return Err(msg);
}
}
let badges = badges.join("\n");
template = template.replace("{{badges}}", &badges);
}

if template.contains("{{license}}") {
if let Some(license) = license {
template = template.replace("{{license}}", &license);
} else {
return Err(
"`{{license}}` was found in template but no license was provided".to_owned(),
);
if license.is_none() {
let msg = "`{{license}}` was found in template but no license was provided".to_owned();
if force {
println!("Warn: {}", msg);
} else {
return Err(msg);
}
}

let license_str = license.unwrap_or("");
template = template.replace("{{license}}", license_str);
}

template = template.replace("{{version}}", version);
Expand Down Expand Up @@ -159,7 +170,8 @@ mod tests {
// process template
#[test]
fn template_without_readme_should_fail() {
let result = super::process_template(String::new(), String::new(), "", &[], None, "");
let result =
super::process_template(String::new(), String::new(), "", &[], None, "", false);
assert!(result.is_err());
assert_eq!("Missing `{{readme}}` in template", result.unwrap_err());
}
Expand All @@ -173,6 +185,7 @@ mod tests {
&[],
None,
"",
false,
);
assert!(result.is_err());
assert_eq!(
Expand All @@ -190,6 +203,7 @@ mod tests {
&[],
None,
"",
false,
);
assert!(result.is_err());
assert_eq!(
Expand All @@ -207,6 +221,7 @@ mod tests {
&[],
None,
"",
false,
);
assert!(result.is_ok());
assert_eq!("readme", result.unwrap());
Expand All @@ -221,6 +236,7 @@ mod tests {
&[],
None,
"",
false,
);
assert!(result.is_ok());
assert_eq!("# title\n\nreadme", result.unwrap());
Expand All @@ -235,6 +251,7 @@ mod tests {
&["badge1", "badge2"],
None,
"",
false,
);
assert!(result.is_ok());
assert_eq!("badge1\nbadge2\n\nreadme", result.unwrap());
Expand All @@ -249,6 +266,7 @@ mod tests {
&[],
Some("license"),
"",
false,
);
assert!(result.is_ok());
assert_eq!("readme\n\nlicense", result.unwrap());
Expand All @@ -263,6 +281,7 @@ mod tests {
&[],
None,
"3.0.1",
false,
);
assert!(result.is_ok());
assert_eq!("readme\n\n3.0.1", result.unwrap());
Expand All @@ -277,6 +296,7 @@ mod tests {
&["badge1", "badge2"],
Some("license"),
"3.0.2",
false,
);
assert!(result.is_ok());
assert_eq!(
Expand Down

0 comments on commit c046050

Please sign in to comment.