Skip to content

Commit

Permalink
feat: add put_profiles cmd
Browse files Browse the repository at this point in the history
  • Loading branch information
zzzgydi committed Dec 14, 2021
1 parent 1c3ae5c commit 0c23845
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
28 changes: 28 additions & 0 deletions src-tauri/src/cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ pub fn get_profiles(lock: State<'_, ProfileLock>) -> Option<ProfilesConfig> {
}

#[tauri::command]
/// update the profile config
pub fn set_profiles(
current: usize,
profile: ProfileItem,
Expand Down Expand Up @@ -131,3 +132,30 @@ pub fn set_profiles(

Ok(())
}

#[tauri::command]
/// change to target profile
pub fn put_profiles(current: usize, lock: State<'_, ProfileLock>) -> Result<(), String> {
match lock.0.lock() {
Ok(_) => {}
Err(_) => return Err(format!("can not get file locked")),
};

let mut profiles = read_profiles();

let items_len = match &profiles.items {
Some(p) => p.len(),
None => 0,
};

if current >= items_len {
Err(format!(
"failed to change profile to the index `{}`",
current
))
} else {
profiles.current = Some(current as u32);
save_profiles(&profiles);
Ok(())
}
}
3 changes: 2 additions & 1 deletion src-tauri/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,8 @@ fn main() -> std::io::Result<()> {
cmd::get_clash_info,
cmd::import_profile,
cmd::get_profiles,
cmd::set_profiles
cmd::set_profiles,
cmd::put_profiles,
])
.build(tauri::generate_context!())
.expect("error while running tauri application");
Expand Down

0 comments on commit 0c23845

Please sign in to comment.