diff --git a/src/cmd/new.rs b/src/cmd/new.rs index 5c23574b7ae43290e4e2587dc74add0707525851..3c88c5b25ffee2aa2b441d6797e0d25534a93ba8 100644 --- a/src/cmd/new.rs +++ b/src/cmd/new.rs @@ -31,6 +31,15 @@ where anyhow::bail!("Contract names can only contain alphanumeric characters and underscores"); } + if !name + .chars() + .next() + .map(|c| c.is_alphabetic()) + .unwrap_or(false) + { + anyhow::bail!("Contract names must begin with an alphabetic character"); + } + let out_dir = dir .map_or(env::current_dir()?, |p| p.as_ref().to_path_buf()) .join(name); @@ -127,6 +136,19 @@ mod tests { }) } + #[test] + fn rejects_name_beginning_with_number() { + with_tmp_dir(|path| { + let result = execute("1xxx", Some(path)); + assert!(result.is_err(), "Should fail"); + assert_eq!( + result.err().unwrap().to_string(), + "Contract names must begin with an alphabetic character" + ); + Ok(()) + }) + } + #[test] fn contract_cargo_project_already_exists() { with_tmp_dir(|path| {