Skip to content

Commit

Permalink
fix: gracefully handle number prefixed names (#439)
Browse files Browse the repository at this point in the history
  • Loading branch information
c12i authored Dec 25, 2024
1 parent 8276f96 commit f1e317c
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 6 deletions.
2 changes: 1 addition & 1 deletion src/cli/link_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
Expand Down
4 changes: 2 additions & 2 deletions src/cli/web_app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down Expand Up @@ -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):")?,
Expand Down
16 changes: 13 additions & 3 deletions src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ pub fn input_no_whitespace(prompt: &str) -> ScaffoldResult<String> {
.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)
Expand All @@ -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(())
}

Expand Down

0 comments on commit f1e317c

Please sign in to comment.