refactor: wip

This commit is contained in:
GyDi
2022-11-16 01:26:41 +08:00
parent ee68d80d0a
commit abdbf158d1
11 changed files with 247 additions and 90 deletions

View File

@@ -1,4 +1,4 @@
use super::{IProfiles, IVerge};
use super::{IClash, IClashTemp, IProfiles, IVerge};
use parking_lot::{MappedMutexGuard, Mutex, MutexGuard};
use serde_yaml::Mapping;
use std::sync::Arc;
@@ -15,6 +15,16 @@ macro_rules! draft_define {
MutexGuard::map(self.inner.lock(), |guard| &mut guard.0)
}
pub fn latest(&self) -> MappedMutexGuard<$id> {
MutexGuard::map(self.inner.lock(), |inner| {
if inner.1.is_none() {
&mut inner.0
} else {
inner.1.as_mut().unwrap()
}
})
}
pub fn draft(&self) -> MappedMutexGuard<$id> {
MutexGuard::map(self.inner.lock(), |mut inner| {
if inner.1.is_none() {
@@ -54,6 +64,8 @@ macro_rules! draft_define {
};
}
draft_define!(IClash);
draft_define!(IClashTemp);
draft_define!(IVerge);
draft_define!(Mapping);
draft_define!(IProfiles);
@@ -85,6 +97,9 @@ fn test_draft() {
assert_eq!(draft.draft().enable_auto_launch, Some(false));
assert_eq!(draft.draft().enable_tun_mode, Some(true));
assert_eq!(draft.latest().enable_auto_launch, Some(false));
assert_eq!(draft.latest().enable_tun_mode, Some(true));
assert!(draft.apply().is_some());
assert!(draft.apply().is_none());