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: Require package names to be non-empty #2293

Merged
merged 7 commits into from
Aug 14, 2023
Merged
Show file tree
Hide file tree
Changes from 2 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
4 changes: 3 additions & 1 deletion crates/nargo_cli/src/cli/init_cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,15 @@ use acvm::Backend;
use clap::Args;
use nargo::constants::{PKG_FILE, SRC_DIR};
use nargo::package::PackageType;
use noirc_frontend::graph::CrateName;
use std::path::PathBuf;
use std::str::FromStr;

/// Create a Noir project in the current directory.
#[derive(Debug, Clone, Args)]
pub(crate) struct InitCommand {
/// Name of the package [default: current directory name]
#[clap(long)]
#[clap(long, value_parser = CrateName::from_str)]
TomAFrench marked this conversation as resolved.
Show resolved Hide resolved
name: Option<String>,

/// Use a library template
Expand Down
5 changes: 3 additions & 2 deletions crates/nargo_cli/src/cli/new_cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ use super::{init_cmd::initialize_project, NargoConfig};
use acvm::Backend;
use clap::Args;
use nargo::package::PackageType;
use std::path::PathBuf;
use noirc_frontend::graph::CrateName;
use std::{path::PathBuf, str::FromStr};

/// Create a Noir project in a new directory.
#[derive(Debug, Clone, Args)]
Expand All @@ -13,7 +14,7 @@ pub(crate) struct NewCommand {
path: PathBuf,

/// Name of the package [default: package directory name]
#[clap(long)]
#[clap(long, value_parser = CrateName::from_str)]
TomAFrench marked this conversation as resolved.
Show resolved Hide resolved
name: Option<String>,

/// Use a library template
Expand Down
31 changes: 27 additions & 4 deletions crates/noirc_frontend/src/graph/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,12 @@ impl CrateId {
#[derive(Debug, Clone, PartialEq, Eq, Hash, Ord, PartialOrd)]
pub struct CrateName(SmolStr);

impl CrateName {
pub fn is_valid_name(name: &str) -> bool {
TomAFrench marked this conversation as resolved.
Show resolved Hide resolved
!name.is_empty() && name.chars().all(|n| !CHARACTER_BLACK_LIST.contains(&n))
}
}

impl Display for CrateName {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
self.0.fmt(f)
Expand All @@ -54,11 +60,28 @@ impl FromStr for CrateName {
type Err = String;

fn from_str(name: &str) -> Result<Self, Self::Err> {
let is_invalid = name.chars().any(|n| CHARACTER_BLACK_LIST.contains(&n));
if is_invalid {
Err(name.into())
} else {
if Self::is_valid_name(name) {
Ok(Self(SmolStr::new(name)))
} else {
Err("Package names must be non-empty and cannot contain hyphens".into())
phated marked this conversation as resolved.
Show resolved Hide resolved
}
}
}

#[cfg(test)]
mod crate_name {
use super::{CrateName, CHARACTER_BLACK_LIST};

#[test]
fn it_rejects_empty_string() {
assert!(!CrateName::is_valid_name(""));
}

#[test]
fn it_rejects_blacklisted_chars() {
for bad_char in CHARACTER_BLACK_LIST {
let bad_char_string = bad_char.to_string();
assert!(!CrateName::is_valid_name(&bad_char_string));
}
}
}
Expand Down