refactor(profile): build companion options in one place

This commit is contained in:
Tunglies
2026-12-03 19:38:28 +08:00
parent 83a520ea19
commit 3af8034e02
+27 -61
View File
@@ -51,29 +51,32 @@ async fn append_companion_item(item: &mut PrfItem) -> Result<String> {
.ok_or_else(|| anyhow::anyhow!("appended companion item has no uid"))
}
async fn ensure_companion_items(
merge: &mut Option<String>,
script: &mut Option<String>,
rules: &mut Option<String>,
proxies: &mut Option<String>,
groups: &mut Option<String>,
) -> Result<()> {
if merge.is_none() {
*merge = Some(append_companion_item(&mut build_merge_profile(None)).await?);
async fn prepare_profile_options(source: Option<&PrfOption>) -> Result<PrfOption> {
let mut options = PrfOption {
update_interval: source.and_then(|options| options.update_interval),
merge: source.and_then(|options| options.merge.clone()),
script: source.and_then(|options| options.script.clone()),
rules: source.and_then(|options| options.rules.clone()),
proxies: source.and_then(|options| options.proxies.clone()),
groups: source.and_then(|options| options.groups.clone()),
..PrfOption::default()
};
if options.merge.is_none() {
options.merge = Some(append_companion_item(&mut build_merge_profile(None)).await?);
}
if script.is_none() {
*script = Some(append_companion_item(&mut build_script_profile(None)).await?);
if options.script.is_none() {
options.script = Some(append_companion_item(&mut build_script_profile(None)).await?);
}
if rules.is_none() {
*rules = Some(append_companion_item(&mut from_rules()).await?);
if options.rules.is_none() {
options.rules = Some(append_companion_item(&mut from_rules()).await?);
}
if proxies.is_none() {
*proxies = Some(append_companion_item(&mut from_proxies()).await?);
if options.proxies.is_none() {
options.proxies = Some(append_companion_item(&mut from_proxies()).await?);
}
if groups.is_none() {
*groups = Some(append_companion_item(&mut from_groups()).await?);
if options.groups.is_none() {
options.groups = Some(append_companion_item(&mut from_groups()).await?);
}
Ok(())
Ok(options)
}
async fn from_local(
@@ -84,15 +87,7 @@ async fn from_local(
) -> Result<PrfItem> {
let uid = help::get_uid("L").into();
let file = format!("{uid}.yaml").into();
let opt_ref = option.as_ref();
let update_interval = opt_ref.and_then(|o| o.update_interval);
let mut merge = opt_ref.and_then(|o| o.merge.clone());
let mut script = opt_ref.and_then(|o| o.script.clone());
let mut rules = opt_ref.and_then(|o| o.rules.clone());
let mut proxies = opt_ref.and_then(|o| o.proxies.clone());
let mut groups = opt_ref.and_then(|o| o.groups.clone());
ensure_companion_items(&mut merge, &mut script, &mut rules, &mut proxies, &mut groups).await?;
let options = prepare_profile_options(option).await?;
Ok(PrfItem {
uid: Some(uid),
itype: Some("local".into()),
@@ -102,15 +97,7 @@ async fn from_local(
url: None,
selected: None,
extra: None,
option: Some(PrfOption {
update_interval,
merge,
script,
rules,
proxies,
groups,
..PrfOption::default()
}),
option: Some(options),
home: None,
updated: Some(chrono::Local::now().timestamp() as usize),
file_data: Some(file_data.unwrap_or_else(|| tmpl::ITEM_LOCAL.into())),
@@ -229,31 +216,14 @@ pub(crate) async fn prepare_remote_refresh(
downloaded: DownloadedSubscription,
option: Option<&PrfOption>,
) -> Result<RemoteProfileRefresh> {
let mut resolved = PrfOption {
update_interval: option
.and_then(|o| o.update_interval)
.or(downloaded.update_interval_minutes),
merge: option.and_then(|o| o.merge.clone()),
script: option.and_then(|o| o.script.clone()),
rules: option.and_then(|o| o.rules.clone()),
proxies: option.and_then(|o| o.proxies.clone()),
groups: option.and_then(|o| o.groups.clone()),
allow_auto_update: Some(allow_auto_update_enabled(option)),
..PrfOption::default()
};
ensure_companion_items(
&mut resolved.merge,
&mut resolved.script,
&mut resolved.rules,
&mut resolved.proxies,
&mut resolved.groups,
)
.await?;
let mut options = prepare_profile_options(option).await?;
options.update_interval = options.update_interval.or(downloaded.update_interval_minutes);
options.allow_auto_update = Some(option.and_then(|options| options.allow_auto_update).unwrap_or(true));
Ok(RemoteProfileRefresh {
extra: downloaded.extra,
home: downloaded.home,
updated: chrono::Local::now().timestamp() as usize,
option: resolved,
option: options,
body: downloaded.body,
})
}
@@ -328,10 +298,6 @@ pub(crate) async fn read_profile_item_file(file: Option<&str>) -> Result<String>
Ok(content.into())
}
fn allow_auto_update_enabled(option: Option<&PrfOption>) -> bool {
option.and_then(|o| o.allow_auto_update).unwrap_or(true)
}
/// Fix URLs where query parameters are incorrectly appended to the path segment
///
/// Incorrect Example: https://example.com/path&param1=value1