Skip to content

Commit

Permalink
Do not allow create name starts with _
Browse files Browse the repository at this point in the history
Signed-off-by: hi-rustin <[email protected]>
  • Loading branch information
Rustin170506 committed Nov 10, 2023
1 parent b23fe07 commit f47425f
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 14 deletions.
64 changes: 52 additions & 12 deletions src/models/krate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,47 @@ impl Crate {
name, MAX_NAME_LENGTH
)));
}
Crate::valid_ident(name, "crate name")
Crate::valid_create_ident(name)
}

// Checks that the name is a valid crate name.
// 1. The name must be non-empty.
// 2. The first character must be an ASCII character.
// 3. The remaining characters must be ASCII alphanumerics or `-` or `_`.
// Note: This differs from `valid_dependency_name`, which allows `_` as the first character.
fn valid_create_ident(name: &str) -> AppResult<()> {
if name.is_empty() {
return Err(cargo_err("the crate name cannot be an empty"));
}
let mut chars = name.chars();
if let Some(ch) = chars.next() {
if ch.is_ascii_digit() {
return Err(cargo_err(&format!(
"the name `{}` cannot be used as a crate name, \
the name cannot start with a digit",
name,
)));
}
if !ch.is_ascii_alphabetic() {
return Err(cargo_err(&format!(
"invalid character `{}` in crate name: `{}`, \
the first character must be an ASCII character",
ch, name
)));
}
}

for ch in chars {
if !(ch.is_ascii_alphanumeric() || ch == '-' || ch == '_') {
return Err(cargo_err(&format!(
"invalid character `{}` in crate name: `{}`, \
characters must be an ASCII alphanumeric characters, `-`, or `_`",
ch, name
)));
}
}

Ok(())
}

pub fn valid_dependency_name(name: &str) -> AppResult<()> {
Expand All @@ -209,41 +249,41 @@ impl Crate {
name, MAX_NAME_LENGTH
)));
}
Crate::valid_ident(name, "dependency name")
Crate::valid_dependency_ident(name)
}

// Checks that the name is a valid identifier.
// Checks that the name is a valid dependency name.
// 1. The name must be non-empty.
// 2. The first character must be an ASCII character or `_`.
// 3. The remaining characters must be ASCII alphanumerics or `-` or `_`.
fn valid_ident(name: &str, what: &str) -> AppResult<()> {
fn valid_dependency_ident(name: &str) -> AppResult<()> {
if name.is_empty() {
return Err(cargo_err(&format!("the {} cannot be an empty", what)));
return Err(cargo_err("the dependency name cannot be an empty"));
}
let mut chars = name.chars();
if let Some(ch) = chars.next() {
if ch.is_ascii_digit() {
return Err(cargo_err(&format!(
"the name `{}` cannot be used as a {}, \
"the name `{}` cannot be used as a dependency name, \
the name cannot start with a digit",
name, what,
name,
)));
}
if !(ch.is_ascii_alphabetic() || ch == '_') {
return Err(cargo_err(&format!(
"invalid character `{}` in {}: `{}`, \
"invalid character `{}` in dependency name: `{}`, \
the first character must be an ASCII character, or `_`",
ch, what, name
ch, name
)));
}
}

for ch in chars {
if !(ch.is_ascii_alphanumeric() || ch == '-' || ch == '_') {
return Err(cargo_err(&format!(
"invalid character `{}` in {}: `{}`, \
"invalid character `{}` in dependency name: `{}`, \
characters must be an ASCII alphanumeric characters, `-`, or `_`",
ch, what, name
ch, name
)));
}
}
Expand Down Expand Up @@ -549,7 +589,7 @@ mod tests {
assert!(Crate::valid_name("foo_underscore").is_ok());
assert!(Crate::valid_name("foo-dash").is_ok());
assert!(Crate::valid_name("foo+plus").is_err());
assert!(Crate::valid_name("_foo").is_ok());
assert!(Crate::valid_name("_foo").is_err());
assert!(Crate::valid_name("-foo").is_err());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ expression: response.into_json()
{
"errors": [
{
"detail": "invalid character `🦀` in crate name: `🦀`, the first character must be an ASCII character, or `_`"
"detail": "invalid character `🦀` in crate name: `🦀`, the first character must be an ASCII character"
}
]
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ expression: response.into_json()
{
"errors": [
{
"detail": "invalid character `á` in crate name: `áccênts`, the first character must be an ASCII character, or `_`"
"detail": "invalid character `á` in crate name: `áccênts`, the first character must be an ASCII character"
}
]
}

0 comments on commit f47425f

Please sign in to comment.