Skip to content

Allow feature names to begin with numbers #1331

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

Merged
merged 2 commits into from
Apr 9, 2018
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
19 changes: 12 additions & 7 deletions src/models/krate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -250,24 +250,29 @@ impl Crate {
}

fn valid_ident(name: &str) -> bool {
if name.is_empty() {
return false;
}
name.chars().next().unwrap().is_alphabetic()
Self::valid_feature_name(name)
&& name.chars()
.nth(0)
.map(char::is_alphabetic)
.unwrap_or(false)
}

pub fn valid_feature_name(name: &str) -> bool {
!name.is_empty()
&& name.chars()
.all(|c| c.is_alphanumeric() || c == '_' || c == '-')
&& name.chars().all(|c| c.is_ascii())
}

pub fn valid_feature_name(name: &str) -> bool {
pub fn valid_feature(name: &str) -> bool {
let mut parts = name.split('/');
match parts.next() {
Some(part) if !Crate::valid_ident(part) => return false,
Some(part) if !Crate::valid_feature_name(part) => return false,
None => return false,
_ => {}
}
match parts.next() {
Some(part) if !Crate::valid_ident(part) => return false,
Some(part) if !Crate::valid_feature_name(part) => return false,
_ => {}
}
parts.next().is_none()
Expand Down
11 changes: 6 additions & 5 deletions src/tests/krate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -753,11 +753,12 @@ fn new_krate_bad_name() {

#[test]
fn valid_feature_names() {
assert!(Crate::valid_feature_name("foo"));
assert!(!Crate::valid_feature_name(""));
assert!(!Crate::valid_feature_name("/"));
assert!(!Crate::valid_feature_name("%/%"));
assert!(Crate::valid_feature_name("a/a"));
assert!(Crate::valid_feature("foo"));
assert!(!Crate::valid_feature(""));
assert!(!Crate::valid_feature("/"));
assert!(!Crate::valid_feature("%/%"));
assert!(Crate::valid_feature("a/a"));
assert!(Crate::valid_feature("32-column-tables"));
}

#[test]
Expand Down
18 changes: 17 additions & 1 deletion src/views/krate_publish.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ pub struct NewCrate {
pub name: CrateName,
pub vers: CrateVersion,
pub deps: Vec<CrateDependency>,
pub features: HashMap<CrateName, Vec<Feature>>,
pub features: HashMap<FeatureName, Vec<Feature>>,
pub authors: Vec<String>,
pub description: Option<String>,
pub homepage: Option<String>,
Expand Down Expand Up @@ -51,6 +51,8 @@ pub struct CategoryList(pub Vec<Category>);
pub struct Category(pub String);
#[derive(Serialize, Debug, Deref)]
pub struct Feature(pub String);
#[derive(PartialEq, Eq, Hash, Serialize, Debug, Deref)]
pub struct FeatureName(pub String);

#[derive(Serialize, Deserialize, Debug)]
pub struct CrateDependency {
Expand Down Expand Up @@ -102,6 +104,20 @@ impl<'de> Deserialize<'de> for Keyword {
}
}

impl<'de> Deserialize<'de> for FeatureName {
fn deserialize<D: Deserializer<'de>>(d: D) -> Result<Self, D::Error> {
let s = String::deserialize(d)?;
if !Crate::valid_feature_name(&s) {
let value = de::Unexpected::Str(&s);
let expected = "a valid feature name containing only letters, \
numbers, hyphens, or underscores";
Err(de::Error::invalid_value(value, &expected))
} else {
Ok(FeatureName(s))
}
}
}

impl<'de> Deserialize<'de> for Feature {
fn deserialize<D: Deserializer<'de>>(d: D) -> Result<Feature, D::Error> {
let s = String::deserialize(d)?;
Expand Down