diff --git a/yazi-cli/src/package/package.rs b/yazi-cli/src/package/package.rs index bee1db87..c8e41034 100644 --- a/yazi-cli/src/package/package.rs +++ b/yazi-cli/src/package/package.rs @@ -12,6 +12,8 @@ pub(crate) struct Package { } impl Package { + /// Create a new Package when adding a new package to the config. + /// Note that the package must end in `".yazi"`. pub(super) fn new(url: &str, commit: Option<&str>) -> Self { let mut parts = url.splitn(2, '#'); @@ -26,6 +28,17 @@ impl Package { Self { repo, child, commit: commit.unwrap_or_default().to_owned(), is_flavor: false } } + /// Create a new Package when installing a package from the config. The + /// package is name considered valid and no `".yazi"` is prepended. + pub(super) fn new_literal(url: &str, commit: Option<&str>) -> Self { + let mut parts = url.splitn(2, '#'); + + let repo = parts.next().unwrap_or_default().to_owned(); + let child = if let Some(s) = parts.next() { format!("{s}.yazi") } else { String::new() }; + + Self { repo, child, commit: commit.unwrap_or_default().to_owned(), is_flavor: false } + } + #[inline] pub(super) fn use_(&self) -> Cow { if self.child.is_empty() { @@ -77,3 +90,22 @@ impl Package { Ok(()) } } + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_package_new() { + // should add ".yazi" to the end of the package name if it doesn't have one + let package = Package::new("user/test", None); + assert_eq!(package.repo, "user/test.yazi"); + } + + #[test] + fn test_package_new_literal() { + // should not add ".yazi" to the end of the package name + let package = Package::new_literal("user/test.yazi", None); + assert_eq!(package.repo, "user/test.yazi"); + } +} diff --git a/yazi-cli/src/package/parser.rs b/yazi-cli/src/package/parser.rs index 28c63981..65c1327e 100644 --- a/yazi-cli/src/package/parser.rs +++ b/yazi-cli/src/package/parser.rs @@ -49,7 +49,7 @@ impl Package { let use_ = dep.get("use").and_then(|d| d.as_str()).context("Missing `use` field")?; let commit = dep.get("commit").and_then(|d| d.as_str()); - let mut package = Package::new(use_, commit); + let mut package = Package::new_literal(use_, commit); if upgrade { package.upgrade().await?; } else {