Simplify the code a bit

This commit is contained in:
sxyazi 2025-01-11 10:39:34 +08:00
parent 72902f8d57
commit 6fc52713b5
No known key found for this signature in database
2 changed files with 12 additions and 14 deletions

View file

@ -5,7 +5,7 @@ use serde::{Deserialize, Deserializer, Serialize, Serializer};
use twox_hash::XxHash3_128;
use yazi_fs::Xdg;
#[derive(Default)]
#[derive(Clone, Default)]
pub(crate) struct Dependency {
pub(crate) use_: String, // owner/repo:child
pub(crate) name: String, // child.yazi
@ -39,6 +39,11 @@ impl Dependency {
}
}
#[inline]
pub(super) fn identical(&self, other: &Self) -> bool {
self.parent == other.parent && self.child == other.child
}
pub(super) fn header(&self, s: &str) -> Result<()> {
use crossterm::style::{Attribute, Print, SetAttributes};

View file

@ -45,18 +45,15 @@ impl Package {
}
pub(crate) async fn delete(&mut self, use_: &str) -> Result<()> {
let dep_to_find = Dependency::from_str(use_)?;
let dep = match self.identical(&dep_to_find) {
Some(d) => d,
None => bail!("`{}` was not found in package.toml", use_),
let Some(dep) = self.identical(&Dependency::from_str(use_)?).cloned() else {
bail!("`{}` was not found in package.toml", use_)
};
dep.delete().await?;
if dep.is_flavor {
self.flavors.retain(|f| f.use_ != use_);
self.flavors.retain(|d| !d.identical(&dep));
} else {
self.plugins.retain(|f| f.use_ != use_);
self.plugins.retain(|d| !d.identical(&dep));
}
let s = toml::to_string_pretty(self)?;
@ -189,12 +186,8 @@ impl Package {
fn toml() -> PathBuf { Xdg::config_dir().join("package.toml") }
#[inline]
fn identical(&self, dep: &Dependency) -> Option<&Dependency> {
self
.plugins
.iter()
.chain(&self.flavors)
.find(|d| d.parent == dep.parent && d.child == dep.child)
fn identical(&self, other: &Dependency) -> Option<&Dependency> {
self.plugins.iter().chain(&self.flavors).find(|d| d.identical(other))
}
}