perf: simplify current_mapping and profiles_preview methods in IProfiles for improved readability

This commit is contained in:
Tunglies 2026-01-16 12:05:17 +08:00
parent c5233a9c2c
commit 09dcb072c3
No known key found for this signature in database
GPG Key ID: B9B01B389469B3E8

View File

@ -315,19 +315,20 @@ impl IProfiles {
/// 获取current指向的订阅内容 /// 获取current指向的订阅内容
pub async fn current_mapping(&self) -> Result<Mapping> { pub async fn current_mapping(&self) -> Result<Mapping> {
match (self.current.as_ref(), self.items.as_ref()) { let (Some(current), Some(items)) = (self.current.as_ref(), self.items.as_ref()) else {
(Some(current), Some(items)) => { return Ok(Mapping::new());
if let Some(item) = items.iter().find(|e| e.uid.as_ref() == Some(current)) { };
let file_path = match item.file.as_ref() {
Some(file) => dirs::app_profiles_dir()?.join(file.as_str()), let Some(target) = items.iter().find(|e| e.uid.as_ref() == Some(current)) else {
None => bail!("failed to get the file field"), bail!("failed to find the current profile \"uid:{current}\"");
}; };
return help::read_mapping(&file_path).await;
} let file = target
bail!("failed to find the current profile \"uid:{current}\""); .file
} .as_ref()
_ => Ok(Mapping::new()), .ok_or_else(|| anyhow::anyhow!("failed to get the file field"))?;
} let file_path = dirs::app_profiles_dir()?.join(file.as_str());
help::read_mapping(&file_path).await
} }
/// 判断profile是否是current指向的 /// 判断profile是否是current指向的
@ -337,32 +338,32 @@ impl IProfiles {
/// 获取所有的profiles(uid名称, 是否为 current) /// 获取所有的profiles(uid名称, 是否为 current)
pub fn profiles_preview(&self) -> Option<Vec<IProfilePreview<'_>>> { pub fn profiles_preview(&self) -> Option<Vec<IProfilePreview<'_>>> {
self.items.as_ref().map(|items| { let items = self.items.as_ref()?;
items let current_uid = self.current.as_ref();
.iter()
.filter_map(|e| { let previews = items
if let (Some(uid), Some(name)) = (e.uid.as_ref(), e.name.as_ref()) { .iter()
let is_current = self.is_current_profile_index(uid); .filter_map(|e| {
let preview = IProfilePreview { uid, name, is_current }; let uid = e.uid.as_ref()?;
Some(preview) let name = e.name.as_ref()?;
} else { Some(IProfilePreview {
None uid,
} name,
is_current: current_uid == Some(uid),
}) })
.collect() })
}) .collect();
Some(previews)
} }
/// 通过 uid 获取名称 /// 通过 uid 获取名称
pub fn get_name_by_uid(&self, uid: &String) -> Option<&String> { pub fn get_name_by_uid(&self, uid: &str) -> Option<&String> {
if let Some(items) = &self.items { self.items
for item in items { .as_ref()?
if item.uid.as_ref() == Some(uid) { .iter()
return item.name.as_ref(); .find(|item| item.uid.as_deref() == Some(uid))
} .and_then(|item| item.name.as_ref())
}
}
None
} }
/// 以 app 中的 profile 列表为准,删除不再需要的文件 /// 以 app 中的 profile 列表为准,删除不再需要的文件