Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: gracefully handle number prefixed names #439

Merged
merged 3 commits into from
Dec 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading