Skip to content

Commit

Permalink
fix: 修复直接安装路径包时匹配出错
Browse files Browse the repository at this point in the history
  • Loading branch information
Cnotech committed Jul 13, 2024
1 parent e93d145 commit 61508be
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 9 deletions.
56 changes: 51 additions & 5 deletions src/types/matcher.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
use std::path::Path;

use regex::Regex;
use semver::VersionReq;

use anyhow::{anyhow, Result};
use anyhow::{anyhow, Ok, Result};

use crate::utils::is_url;
use crate::utils::{format_path, is_url};

lazy_static! {
static ref PACKAGE_MATCHER_REGEX: Regex =
Expand Down Expand Up @@ -86,17 +88,26 @@ pub enum PackageInputEnum {

impl PackageInputEnum {
pub fn parse(text: String, deny_mirror: bool, deny_version_matcher: bool) -> Result<Self> {
// 使用正则匹配
// 判断是否为 URL
if is_url(&text) {
return Ok(PackageInputEnum::Url(text));
}

// 如果本地存在该路径,则作为路径处理
let p = Path::new(&text);
if p.exists() {
return Ok(PackageInputEnum::LocalPath(format_path(&text)));
}

// 使用正则匹配 PackageMatcher
if PACKAGE_MATCHER_REGEX.is_match(&text) {
let m = PackageMatcher::parse(&text, deny_mirror, deny_version_matcher)?;
return Ok(PackageInputEnum::PackageMatcher(m));
}

// 兜底,作为本地路径
Ok(PackageInputEnum::LocalPath(text))
Err(anyhow!(
"Error:Failed to parse '{text}' as valid package input"
))
}
}

Expand Down Expand Up @@ -148,3 +159,38 @@ fn test_parse_package_matcher() {
assert!(PackageMatcher::parse(&"Official/Microsoft/VSCode".to_string(), true, false).is_err());
assert!(PackageMatcher::parse(&"VSCode@\">=0.1.0\"".to_string(), false, true).is_err());
}

#[test]
fn test_parse_package_input_enum() {
assert_eq!(
PackageInputEnum::parse(
"https://nep.edgeless.top/static/test.nep".to_string(),
false,
false
)
.unwrap(),
PackageInputEnum::Url("https://nep.edgeless.top/static/test.nep".to_string())
);
assert_eq!(
PackageInputEnum::parse(".\\Cargo.lock".to_string(), false, false).unwrap(),
PackageInputEnum::LocalPath("Cargo.lock".to_string())
);
assert_eq!(
PackageInputEnum::parse("VSCode".to_string(), false, false).unwrap(),
PackageInputEnum::PackageMatcher(PackageMatcher {
name: "VSCode".to_string(),
scope: None,
mirror: None,
version_req: None
})
);
assert_eq!(
PackageInputEnum::parse("[email protected]".to_string(), false, false).unwrap(),
PackageInputEnum::PackageMatcher(PackageMatcher {
name: "VSCode".to_string(),
scope: None,
mirror: None,
version_req: Some(VersionReq::parse("1.1.4").unwrap())
})
);
}
8 changes: 4 additions & 4 deletions src/utils/path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,9 @@ fn find_scope_with_name_locally(name: &String, scope: Option<String>) -> Result<
}

Err(if let Some(s) = scope {
anyhow!("Error:Can't locate '{name}' with scope '{s}'")
anyhow!("Error:Can't locate '{name}' with scope '{s}' locally")
} else {
anyhow!("Error:Can't find scope for '{name}'")
anyhow!("Error:Can't find scope for '{name}' locally")
})
}

Expand All @@ -102,9 +102,9 @@ fn find_scope_with_name_online(name: &String, scope: Option<String>) -> Result<(
}
}
Err(if let Some(s) = scope {
anyhow!("Error:Can't locate '{name}' with scope '{s}'")
anyhow!("Error:Can't locate '{name}' with scope '{s}' online")
} else {
anyhow!("Error:Can't find scope for '{name}'")
anyhow!("Error:Can't find scope for '{name}' online")
})
}

Expand Down

0 comments on commit 61508be

Please sign in to comment.