From f1e317cf5e5d70bbc1892f1f3e5d2cc9d6499e69 Mon Sep 17 00:00:00 2001 From: Collins Muriuki Date: Wed, 25 Dec 2024 10:12:43 +0300 Subject: [PATCH] fix: gracefully handle number prefixed names (#439) --- src/cli/link_type.rs | 2 +- src/cli/web_app.rs | 4 ++-- src/utils.rs | 16 +++++++++++++--- 3 files changed, 16 insertions(+), 6 deletions(-) diff --git a/src/cli/link_type.rs b/src/cli/link_type.rs index 4bf7f0e6..639af40d 100644 --- a/src/cli/link_type.rs +++ b/src/cli/link_type.rs @@ -43,8 +43,8 @@ pub struct LinkType { #[structopt(long)] /// Skips UI generation for this link-type. pub no_ui: bool, - #[structopt(long)] + #[structopt(long)] /// Skips test generation for this link-type. pub no_spec: bool, } diff --git a/src/cli/web_app.rs b/src/cli/web_app.rs index ecc68a06..798c6b51 100644 --- a/src/cli/web_app.rs +++ b/src/cli/web_app.rs @@ -24,7 +24,7 @@ use crate::{ zome::scaffold_zome_pair, }, templates::ScaffoldedTemplate, - utils::{check_no_whitespace, input_no_whitespace, input_with_case, input_yes_or_no}, + utils::{input_no_whitespace, input_with_case, input_yes_or_no, validate_input}, }; #[derive(Debug, StructOpt)] @@ -57,7 +57,7 @@ impl WebApp { let current_dir = std::env::current_dir()?; let name = match self.name { Some(n) => { - check_no_whitespace(&n, "app name")?; + validate_input(&n, "app name")?; n } None => input_no_whitespace("App name (no whitespaces):")?, diff --git a/src/utils.rs b/src/utils.rs index 020ee717..bb6a00a0 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -149,7 +149,7 @@ pub fn input_no_whitespace(prompt: &str) -> ScaffoldResult { .with_prompt(prompt) .interact_text()?; - while let Err(e) = check_no_whitespace(&input, "Input") { + while let Err(e) = validate_input(&input, "Input") { println!("{}", e.to_string().red()); input = Input::with_theme(&ColorfulTheme::default()) .with_prompt(prompt) @@ -167,17 +167,27 @@ pub fn check_case(input: &str, identifier: &str, case: Case) -> ScaffoldResult<( "{identifier} must be {case:?} Case", ))); } + if input.chars().next().map_or(false, char::is_numeric) { + return Err(ScaffoldError::InvalidStringFormat(format!( + "{identifier} must not start with a number" + ))); + } Ok(()) } #[inline] -/// Raises an error if input is contains white spaces -pub fn check_no_whitespace(input: &str, identifier: &str) -> ScaffoldResult<()> { +/// Raises an error if input is contains white spaces or starts with a numeric +pub fn validate_input(input: &str, identifier: &str) -> ScaffoldResult<()> { if input.contains(char::is_whitespace) { return Err(ScaffoldError::InvalidStringFormat(format!( "{identifier} must *not* contain whitespaces.", ))); } + if input.chars().next().map_or(false, char::is_numeric) { + return Err(ScaffoldError::InvalidStringFormat(format!( + "{identifier} must not start with a numeric" + ))); + } Ok(()) }