From 8753f8a86f7366482dda51e84c797c4ccce2c147 Mon Sep 17 00:00:00 2001 From: hi-rustin Date: Wed, 8 Nov 2023 20:35:28 +0800 Subject: [PATCH] Fix the validation for optional dep Signed-off-by: hi-rustin --- src/models/krate.rs | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/src/models/krate.rs b/src/models/krate.rs index 19300e14fb6..aa4f913cd18 100644 --- a/src/models/krate.rs +++ b/src/models/krate.rs @@ -290,13 +290,14 @@ impl Crate { /// Validates a whole feature string, `features = ["THIS", "and/THIS", "dep:THIS", "dep?/THIS"]`. pub fn valid_feature(name: &str) -> AppResult<()> { - match name.split_once('/') { - Some((dep, dep_feat)) => { - let dep = dep.strip_suffix('?').unwrap_or(dep); - Crate::valid_dependency_name(dep)?; - Crate::valid_feature_name(dep_feat) - } - None => Crate::valid_feature_name(name.strip_prefix("dep:").unwrap_or(name)), + if let Some((dep, dep_feat)) = name.split_once('/') { + let dep = dep.strip_suffix('?').unwrap_or(dep); + Crate::valid_dependency_name(dep)?; + Crate::valid_feature_name(dep_feat) + } else if let Some((_, dep)) = name.split_once("dep:") { + Crate::valid_dependency_name(dep) + } else { + Crate::valid_feature_name(name) } } @@ -587,5 +588,7 @@ mod tests { assert!(Crate::valid_feature("foo?bar").is_err()); assert!(Crate::valid_feature("bar.web").is_ok()); assert!(Crate::valid_feature("foo/bar.web").is_ok()); + assert!(Crate::valid_feature("dep:0foo").is_err()); + assert!(Crate::valid_feature("0foo?/bar.web").is_err()); } }