Rename find_dep_in_package to identical

This commit is contained in:
sxyazi 2025-01-11 10:27:29 +08:00
parent 9af51ae20d
commit 72902f8d57
No known key found for this signature in database
2 changed files with 16 additions and 10 deletions

View file

@ -11,7 +11,7 @@ pub(crate) struct Dependency {
pub(crate) name: String, // child.yazi
pub(crate) parent: String, // owner/repo
pub(crate) child: String, // child
pub(crate) child: String, // child.yazi
pub(crate) rev: String,
pub(crate) hash: String,

View file

@ -25,10 +25,12 @@ impl Package {
pub(crate) async fn add(&mut self, use_: &str) -> Result<()> {
let mut dep = Dependency::from_str(use_)?;
if let Some(existing_dep) = self.find_dep_in_package(&dep) {
let package_type = if existing_dep.is_flavor { "Flavor" } else { "Plugin" };
bail!("{} `{}` already exists in package.toml", package_type, dep.name)
if let Some(d) = self.identical(&dep) {
bail!(
"{} `{}` already exists in package.toml",
if d.is_flavor { "Flavor" } else { "Plugin" },
dep.name
)
}
dep.add().await?;
@ -45,7 +47,7 @@ impl Package {
pub(crate) async fn delete(&mut self, use_: &str) -> Result<()> {
let dep_to_find = Dependency::from_str(use_)?;
let dep = match self.find_dep_in_package(&dep_to_find) {
let dep = match self.identical(&dep_to_find) {
Some(d) => d,
None => bail!("`{}` was not found in package.toml", use_),
};
@ -184,12 +186,16 @@ impl Package {
}
#[inline]
fn find_dep_in_package(&self, dep: &Dependency) -> Option<&Dependency> {
self.plugins.iter().chain(self.flavors.iter()).find(|d| d.use_ == dep.use_)
}
fn toml() -> PathBuf { Xdg::config_dir().join("package.toml") }
#[inline]
fn toml() -> PathBuf { Xdg::config_dir().join("package.toml") }
fn identical(&self, dep: &Dependency) -> Option<&Dependency> {
self
.plugins
.iter()
.chain(&self.flavors)
.find(|d| d.parent == dep.parent && d.child == dep.child)
}
}
impl<'de> Deserialize<'de> for Package {