diff --git a/src-tauri/src/config/prfitem.rs b/src-tauri/src/config/prfitem.rs index da8728d21..eb9ddb325 100644 --- a/src-tauri/src/config/prfitem.rs +++ b/src-tauri/src/config/prfitem.rs @@ -181,6 +181,36 @@ impl PrfItem { } } + async fn append_companion_item(item: &mut Self) -> Result> { + profiles::profiles_append_item_safe(item).await?; + Ok(item.uid.clone()) + } + + async fn ensure_companion_items( + merge: &mut Option, + script: &mut Option, + rules: &mut Option, + proxies: &mut Option, + groups: &mut Option, + ) -> Result<()> { + if merge.is_none() { + *merge = Self::append_companion_item(&mut Self::from_merge(None)).await?; + } + if script.is_none() { + *script = Self::append_companion_item(&mut Self::from_script(None)).await?; + } + if rules.is_none() { + *rules = Self::append_companion_item(&mut Self::from_rules()).await?; + } + if proxies.is_none() { + *proxies = Self::append_companion_item(&mut Self::from_proxies()).await?; + } + if groups.is_none() { + *groups = Self::append_companion_item(&mut Self::from_groups()).await?; + } + Ok(()) + } + async fn from_local( name: String, desc: String, @@ -197,31 +227,7 @@ impl PrfItem { let mut proxies = opt_ref.and_then(|o| o.proxies.clone()); let mut groups = opt_ref.and_then(|o| o.groups.clone()); - if merge.is_none() { - let merge_item = &mut Self::from_merge(None); - profiles::profiles_append_item_safe(merge_item).await?; - merge = merge_item.uid.clone(); - } - if script.is_none() { - let script_item = &mut Self::from_script(None); - profiles::profiles_append_item_safe(script_item).await?; - script = script_item.uid.clone(); - } - if rules.is_none() { - let rules_item = &mut Self::from_rules(); - profiles::profiles_append_item_safe(rules_item).await?; - rules = rules_item.uid.clone(); - } - if proxies.is_none() { - let proxies_item = &mut Self::from_proxies(); - profiles::profiles_append_item_safe(proxies_item).await?; - proxies = proxies_item.uid.clone(); - } - if groups.is_none() { - let groups_item = &mut Self::from_groups(); - profiles::profiles_append_item_safe(groups_item).await?; - groups = groups_item.uid.clone(); - } + Self::ensure_companion_items(&mut merge, &mut script, &mut rules, &mut proxies, &mut groups).await?; Ok(Self { uid: Some(uid), itype: Some("local".into()), @@ -375,31 +381,7 @@ impl PrfItem { bail!("profile does not contain `proxies` or `proxy-providers`"); } - if merge.is_none() { - let merge_item = &mut Self::from_merge(None); - profiles::profiles_append_item_safe(merge_item).await?; - merge = merge_item.uid.clone(); - } - if script.is_none() { - let script_item = &mut Self::from_script(None); - profiles::profiles_append_item_safe(script_item).await?; - script = script_item.uid.clone(); - } - if rules.is_none() { - let rules_item = &mut Self::from_rules(); - profiles::profiles_append_item_safe(rules_item).await?; - rules = rules_item.uid.clone(); - } - if proxies.is_none() { - let proxies_item = &mut Self::from_proxies(); - profiles::profiles_append_item_safe(proxies_item).await?; - proxies = proxies_item.uid.clone(); - } - if groups.is_none() { - let groups_item = &mut Self::from_groups(); - profiles::profiles_append_item_safe(groups_item).await?; - groups = groups_item.uid.clone(); - } + Self::ensure_companion_items(&mut merge, &mut script, &mut rules, &mut proxies, &mut groups).await?; Ok(Self { uid: Some(uid), @@ -461,46 +443,30 @@ impl PrfItem { } } - fn from_rules() -> Self { - let uid = help::get_uid("r").into(); + fn from_seq_item(prefix: &str, itype: &str, template: &str) -> Self { + let uid = help::get_uid(prefix).into(); let file = format!("{uid}.yaml").into(); // yaml ext Self { uid: Some(uid), - itype: Some("rules".into()), + itype: Some(itype.into()), file: Some(file), updated: Some(chrono::Local::now().timestamp() as usize), - file_data: Some(tmpl::ITEM_RULES.into()), + file_data: Some(template.into()), ..Default::default() } } + fn from_rules() -> Self { + Self::from_seq_item("r", "rules", tmpl::ITEM_RULES) + } + fn from_proxies() -> Self { - let uid = help::get_uid("p").into(); - let file = format!("{uid}.yaml").into(); // yaml ext - - Self { - uid: Some(uid), - itype: Some("proxies".into()), - file: Some(file), - updated: Some(chrono::Local::now().timestamp() as usize), - file_data: Some(tmpl::ITEM_PROXIES.into()), - ..Default::default() - } + Self::from_seq_item("p", "proxies", tmpl::ITEM_PROXIES) } fn from_groups() -> Self { - let uid = help::get_uid("g").into(); - let file = format!("{uid}.yaml").into(); // yaml ext - - Self { - uid: Some(uid), - itype: Some("groups".into()), - file: Some(file), - updated: Some(chrono::Local::now().timestamp() as usize), - file_data: Some(tmpl::ITEM_GROUPS.into()), - ..Default::default() - } + Self::from_seq_item("g", "groups", tmpl::ITEM_GROUPS) } pub(crate) async fn read_file(&self) -> Result { @@ -516,26 +482,20 @@ impl PrfItem { } } +macro_rules! current_field { + ($name:ident, $field:ident) => { + pub(crate) fn $name(&self) -> Option<&String> { + self.option.as_ref().and_then(|o| o.$field.as_ref()) + } + }; +} + impl PrfItem { - pub(crate) fn current_merge(&self) -> Option<&String> { - self.option.as_ref().and_then(|o| o.merge.as_ref()) - } - - pub(crate) fn current_script(&self) -> Option<&String> { - self.option.as_ref().and_then(|o| o.script.as_ref()) - } - - pub(crate) fn current_rules(&self) -> Option<&String> { - self.option.as_ref().and_then(|o| o.rules.as_ref()) - } - - pub(crate) fn current_proxies(&self) -> Option<&String> { - self.option.as_ref().and_then(|o| o.proxies.as_ref()) - } - - pub(crate) fn current_groups(&self) -> Option<&String> { - self.option.as_ref().and_then(|o| o.groups.as_ref()) - } + current_field!(current_merge, merge); + current_field!(current_script, script); + current_field!(current_rules, rules); + current_field!(current_proxies, proxies); + current_field!(current_groups, groups); } fn allow_auto_update_enabled(option: Option<&PrfOption>) -> bool {