Fix 'aiken new', now require project name in specific format.

This commit is contained in:
KtorZ 2022-12-21 00:19:24 +01:00
parent 796ac28044
commit a3591cc7dc
No known key found for this signature in database
GPG Key ID: 33173CB6F77F4277
2 changed files with 11 additions and 13 deletions

View File

@ -4,7 +4,7 @@ use thiserror::Error;
#[derive(Debug, Error)]
pub enum Error {
#[error("{} is not a valid project name. {}", name, reason.to_string())]
#[error("'{}' is not a valid project name: {}", name, reason.to_string())]
InvalidProjectName {
name: String,
reason: InvalidProjectNameReason,
@ -21,15 +21,16 @@ pub enum InvalidProjectNameReason {
impl fmt::Display for InvalidProjectNameReason {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
InvalidProjectNameReason::AikenPrefix => write!(f, "It is a reserved word in Aiken."),
InvalidProjectNameReason::AikenPrefix => write!(f, "it's a reserved word in Aiken."),
InvalidProjectNameReason::AikenReservedModule => {
write!(f, "It is a reserved module name in Aiken.")
write!(f, "it's a reserved module name in Aiken.")
}
InvalidProjectNameReason::Format => write!(
f,
"It does not have the correct format. Project names \
must start with a lowercase letter and may only contain lowercase letters, \
numbers and underscores."
"it is malformed.\n\nProjects must be named as:\n\n\t\
{{repository}}/{{project}}\n\nEach part must start with a lowercase letter \
and may only contain lowercase letters, numbers, hyphens or underscores.\
\nFor example,\n\n\taiken-lang/stdlib"
),
}
}

View File

@ -23,7 +23,7 @@ pub struct Creator {
impl Creator {
fn new(args: Args, project_name: String) -> Self {
let root = args.name;
let root = PathBuf::from(args.name.file_name().unwrap());
let lib = root.join("lib");
let validators = root.join("validators");
let project_name = project_name;
@ -124,9 +124,7 @@ Find more on the [Aiken's user manual](https://aiken-lang.org).
version = "0.0.0"
licences = ["Apache-2.0"]
description = "Aiken contracts for project '{name}'"
[dependencies]
dependencies = []
"#,
name = self.project_name,
},
@ -136,7 +134,6 @@ Find more on the [Aiken's user manual](https://aiken-lang.org).
fn write(path: PathBuf, contents: &str) -> miette::Result<()> {
let mut f = fs::File::create(path).into_diagnostic()?;
f.write_all(contents.as_bytes()).into_diagnostic()?;
Ok(())
}
@ -152,8 +149,8 @@ fn validate_name(name: &str) -> Result<(), Error> {
name: name.to_string(),
reason: InvalidProjectNameReason::AikenReservedModule,
})
} else if !regex::Regex::new("^[a-z][a-z0-9_]*$")
.expect("new name regex could not be compiled")
} else if !regex::Regex::new("^[a-z0-9_-]+/[a-z0-9_-]+$")
.expect("regex could not be compiled")
.is_match(name)
{
Err(Error::InvalidProjectName {