From 0c238459708032116e5a3c83881e7d560b314709 Mon Sep 17 00:00:00 2001 From: GyDi Date: Wed, 15 Dec 2021 02:01:24 +0800 Subject: [PATCH] feat: add put_profiles cmd --- src-tauri/src/cmd.rs | 28 ++++++++++++++++++++++++++++ src-tauri/src/main.rs | 3 ++- 2 files changed, 30 insertions(+), 1 deletion(-) diff --git a/src-tauri/src/cmd.rs b/src-tauri/src/cmd.rs index 7fe8b43..d7c3475 100644 --- a/src-tauri/src/cmd.rs +++ b/src-tauri/src/cmd.rs @@ -84,6 +84,7 @@ pub fn get_profiles(lock: State<'_, ProfileLock>) -> Option { } #[tauri::command] +/// update the profile config pub fn set_profiles( current: usize, profile: ProfileItem, @@ -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(()) + } +} diff --git a/src-tauri/src/main.rs b/src-tauri/src/main.rs index 1a47948..920ce82 100644 --- a/src-tauri/src/main.rs +++ b/src-tauri/src/main.rs @@ -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");