From c0460509ba823531f768ff726b7ddd1fd8c85ff0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sylwester=20R=C4=85pa=C5=82a?= Date: Wed, 5 Feb 2020 16:24:37 +0100 Subject: [PATCH] Init of --force flag --- src/main.rs | 5 +++++ src/readme/mod.rs | 11 ++++++++++- src/readme/template.rs | 38 +++++++++++++++++++++++++++++--------- 3 files changed, 44 insertions(+), 10 deletions(-) diff --git a/src/main.rs b/src/main.rs index f8cf816..b63041a 100644 --- a/src/main.rs +++ b/src/main.rs @@ -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") { @@ -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"))?; @@ -127,6 +131,7 @@ fn execute(m: &ArgMatches) -> Result<(), String> { add_badges, add_license, indent_headings, + force, )?; helper::write_output(&mut dest, readme) diff --git a/src/readme/mod.rs b/src/readme/mod.rs index a435cde..50afe84 100644 --- a/src/readme/mod.rs +++ b/src/readme/mod.rs @@ -18,6 +18,7 @@ pub fn generate_readme( add_badges: bool, add_license: bool, indent_headings: bool, + force: bool, ) -> Result { let lines = extract::extract_docs(source).map_err(|e| format!("{}", e))?; @@ -33,7 +34,15 @@ pub fn generate_readme( // 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 diff --git a/src/readme/template.rs b/src/readme/template.rs index 332d525..cd1945d 100644 --- a/src/readme/template.rs +++ b/src/readme/template.rs @@ -10,6 +10,7 @@ pub fn render( add_title: bool, add_badges: bool, add_license: bool, + force: bool, ) -> Result { let title: &str = &cargo.name; @@ -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, @@ -50,6 +51,7 @@ fn process_template( badges: &[&str], license: Option<&str>, version: &str, + force: bool, ) -> Result { template = template.trim_end_matches("\n").to_owned(); @@ -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); @@ -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()); } @@ -173,6 +185,7 @@ mod tests { &[], None, "", + false, ); assert!(result.is_err()); assert_eq!( @@ -190,6 +203,7 @@ mod tests { &[], None, "", + false, ); assert!(result.is_err()); assert_eq!( @@ -207,6 +221,7 @@ mod tests { &[], None, "", + false, ); assert!(result.is_ok()); assert_eq!("readme", result.unwrap()); @@ -221,6 +236,7 @@ mod tests { &[], None, "", + false, ); assert!(result.is_ok()); assert_eq!("# title\n\nreadme", result.unwrap()); @@ -235,6 +251,7 @@ mod tests { &["badge1", "badge2"], None, "", + false, ); assert!(result.is_ok()); assert_eq!("badge1\nbadge2\n\nreadme", result.unwrap()); @@ -249,6 +266,7 @@ mod tests { &[], Some("license"), "", + false, ); assert!(result.is_ok()); assert_eq!("readme\n\nlicense", result.unwrap()); @@ -263,6 +281,7 @@ mod tests { &[], None, "3.0.1", + false, ); assert!(result.is_ok()); assert_eq!("readme\n\n3.0.1", result.unwrap()); @@ -277,6 +296,7 @@ mod tests { &["badge1", "badge2"], Some("license"), "3.0.2", + false, ); assert!(result.is_ok()); assert_eq!(