diff --git a/src-tauri/rustfmt.toml b/src-tauri/rustfmt.toml index c00563b..4d442bb 100644 --- a/src-tauri/rustfmt.toml +++ b/src-tauri/rustfmt.toml @@ -1,13 +1,21 @@ -max_width = 100 -hard_tabs = false -tab_spaces = 4 -newline_style = "Auto" edition = "2024" -use_small_heuristics = "Default" +tab_spaces = 4 +unstable_features = true + +# imports +group_imports = "StdExternalCrate" +imports_granularity = "Module" reorder_imports = true -reorder_modules = true -remove_nested_parens = true -merge_derives = true -use_try_shorthand = false -use_field_init_shorthand = false -force_explicit_abi = true + +# comments +format_code_in_doc_comments = true +normalize_comments = true +wrap_comments = true + +# others +condense_wildcard_suffixes = true +format_strings = true +format_macro_matchers = true +merge_derives = false +reorder_impl_items = true +use_field_init_shorthand = true diff --git a/src-tauri/src/cmd/config.rs b/src-tauri/src/cmd/config.rs index 4f689ae..f98d99f 100644 --- a/src-tauri/src/cmd/config.rs +++ b/src-tauri/src/cmd/config.rs @@ -1,4 +1,5 @@ use std::fs; + use tauri::State; use crate::cmd::http_api::{get_process_list, start_process, stop_process}; diff --git a/src-tauri/src/cmd/custom_updater.rs b/src-tauri/src/cmd/custom_updater.rs index 47bb347..9548602 100644 --- a/src-tauri/src/cmd/custom_updater.rs +++ b/src-tauri/src/cmd/custom_updater.rs @@ -1,13 +1,16 @@ -use crate::{cmd::config::save_settings, object::structs::AppState}; -use reqwest::Client; -use serde::{Deserialize, Serialize}; use std::env; use std::path::PathBuf; use std::process::Command; use std::time::Duration; + +use reqwest::Client; +use serde::{Deserialize, Serialize}; use tauri::{AppHandle, Emitter, State}; use tokio::io::AsyncWriteExt; +use crate::cmd::config::save_settings; +use crate::object::structs::AppState; + #[derive(Debug, Serialize, Deserialize)] pub struct GitHubRelease { tag_name: String, diff --git a/src-tauri/src/cmd/http_api.rs b/src-tauri/src/cmd/http_api.rs index c71cb9b..6636d80 100644 --- a/src-tauri/src/cmd/http_api.rs +++ b/src-tauri/src/cmd/http_api.rs @@ -1,11 +1,12 @@ -use crate::object::structs::AppState; -use crate::utils::api::{ListProcessResponse, ProcessStatus, get_api_key, get_server_port}; -use reqwest; - use std::collections::HashMap; use std::str::FromStr; + +use reqwest; use tauri::State; +use crate::object::structs::AppState; +use crate::utils::api::{ListProcessResponse, ProcessStatus, get_api_key, get_server_port}; + #[tauri::command] pub async fn get_process_list(_state: State<'_, AppState>) -> Result, String> { let api_key = get_api_key(); diff --git a/src-tauri/src/cmd/logs.rs b/src-tauri/src/cmd/logs.rs index 6d2a2f3..2735058 100644 --- a/src-tauri/src/cmd/logs.rs +++ b/src-tauri/src/cmd/logs.rs @@ -1,6 +1,7 @@ -use regex::Regex; use std::env; +use regex::Regex; + #[tauri::command] pub async fn get_admin_password() -> Result { let app_dir = env::current_exe().unwrap().parent().unwrap().to_path_buf(); @@ -14,10 +15,10 @@ pub async fn get_admin_password() -> Result { let mut last_password = None; for line in logs_content.lines() { - if let Some(captures) = re.captures(line) { - if let Some(password) = captures.get(1) { - last_password = Some(password.as_str().to_string()); - } + if let Some(captures) = re.captures(line) + && let Some(password) = captures.get(1) + { + last_password = Some(password.as_str().to_string()); } } diff --git a/src-tauri/src/cmd/openlist_core.rs b/src-tauri/src/cmd/openlist_core.rs index 72ba5de..9e6600a 100644 --- a/src-tauri/src/cmd/openlist_core.rs +++ b/src-tauri/src/cmd/openlist_core.rs @@ -1,11 +1,10 @@ +use reqwest; +use tauri::State; +use url::Url; + use crate::object::structs::{AppState, ServiceStatus}; use crate::utils::api::{CreateProcessResponse, ProcessConfig, get_api_key, get_server_port}; use crate::utils::path::{get_app_logs_dir, get_openlist_binary_path}; -use reqwest; - -use tauri::State; - -use url::Url; #[tauri::command] pub async fn create_openlist_core_process( diff --git a/src-tauri/src/cmd/os_operate.rs b/src-tauri/src/cmd/os_operate.rs index a94f08a..fe91782 100644 --- a/src-tauri/src/cmd/os_operate.rs +++ b/src-tauri/src/cmd/os_operate.rs @@ -1,5 +1,6 @@ use std::fs; use std::path::{Path, PathBuf}; + use tauri::{AppHandle, State}; use crate::cmd::http_api::{get_process_list, start_process, stop_process}; @@ -176,19 +177,17 @@ pub async fn update_tool_version( let was_running = running_process.map(|p| p.is_running).unwrap_or(false); let process_id = running_process.map(|p| p.config.id.clone()); - if was_running { - if let Some(pid) = &process_id { - log::info!("Stopping {tool} process with ID: {pid}"); - match tool.as_str() { - "openlist" | "rclone" => { - stop_process(pid.clone(), state.clone()) - .await - .map_err(|e| format!("Failed to stop process: {e}"))?; - } - _ => return Err("Unsupported tool".to_string()), + if was_running && let Some(pid) = &process_id { + log::info!("Stopping {tool} process with ID: {pid}"); + match tool.as_str() { + "openlist" | "rclone" => { + stop_process(pid.clone(), state.clone()) + .await + .map_err(|e| format!("Failed to stop process: {e}"))?; } - log::info!("Successfully stopped {tool} process"); + _ => return Err("Unsupported tool".to_string()), } + log::info!("Successfully stopped {tool} process"); } let result = download_and_replace_binary(&tool, &version).await; @@ -197,19 +196,17 @@ pub async fn update_tool_version( Ok(_) => { log::info!("Successfully downloaded and replaced {tool} binary"); - if was_running { - if let Some(pid) = &process_id { - log::info!("Starting {tool} process with ID: {pid}"); - match tool.as_str() { - "openlist" | "rclone" => { - start_process(pid.clone(), state.clone()) - .await - .map_err(|e| format!("Failed to start {tool} process: {e}"))?; - } - _ => return Err("Unsupported tool".to_string()), + if was_running && let Some(pid) = &process_id { + log::info!("Starting {tool} process with ID: {pid}"); + match tool.as_str() { + "openlist" | "rclone" => { + start_process(pid.clone(), state.clone()) + .await + .map_err(|e| format!("Failed to start {tool} process: {e}"))?; } - log::info!("Successfully restarted {tool} process"); + _ => return Err("Unsupported tool".to_string()), } + log::info!("Successfully restarted {tool} process"); } Ok(format!("Successfully updated {tool} to {version}")) @@ -217,17 +214,15 @@ pub async fn update_tool_version( Err(e) => { log::error!("Failed to update {tool} binary: {e}"); - if was_running { - if let Some(pid) = &process_id { - log::info!( - "Attempting to restart {tool} with previous binary after update failure" - ); - match tool.as_str() { - "openlist" | "rclone" => { - let _ = start_process(pid.clone(), state.clone()).await; - } - _ => {} + if was_running && let Some(pid) = &process_id { + log::info!( + "Attempting to restart {tool} with previous binary after update failure" + ); + match tool.as_str() { + "openlist" | "rclone" => { + let _ = start_process(pid.clone(), state.clone()).await; } + _ => {} } } @@ -535,18 +530,18 @@ fn extract_tar_gz( .path() .map_err(|e| format!("Failed to get entry path: {e}"))?; - if let Some(file_name) = path.file_name() { - if file_name == executable_name { - let output_path = extract_dir.join(executable_name); - let mut output_file = fs::File::create(&output_path) - .map_err(|e| format!("Failed to create output file: {e}"))?; + if let Some(file_name) = path.file_name() + && file_name == executable_name + { + let output_path = extract_dir.join(executable_name); + let mut output_file = fs::File::create(&output_path) + .map_err(|e| format!("Failed to create output file: {e}"))?; - std::io::copy(&mut entry, &mut output_file) - .map_err(|e| format!("Failed to extract file: {e}"))?; + std::io::copy(&mut entry, &mut output_file) + .map_err(|e| format!("Failed to extract file: {e}"))?; - executable_path = Some(output_path); - break; - } + executable_path = Some(output_path); + break; } } diff --git a/src-tauri/src/cmd/rclone_core.rs b/src-tauri/src/cmd/rclone_core.rs index b953eac..25b51cf 100644 --- a/src-tauri/src/cmd/rclone_core.rs +++ b/src-tauri/src/cmd/rclone_core.rs @@ -1,13 +1,13 @@ +use std::time::Duration; + +use reqwest::{self, Client}; +use tauri::State; + use crate::cmd::http_api::{get_process_list, start_process}; use crate::object::structs::AppState; -use crate::utils::api::ProcessConfig; +use crate::utils::api::{CreateProcessResponse, ProcessConfig, get_api_key, get_server_port}; use crate::utils::path::{get_app_logs_dir, get_rclone_binary_path}; -use crate::utils::api::{CreateProcessResponse, get_api_key, get_server_port}; -use reqwest::{self, Client}; -use std::time::Duration; -use tauri::State; - // use 45572 due to the reserved port on Windows pub const RCLONE_API_BASE: &str = "http://127.0.0.1:45572"; // admin:admin base64 encoded diff --git a/src-tauri/src/cmd/rclone_mount.rs b/src-tauri/src/cmd/rclone_mount.rs index 2732490..2edd7e4 100644 --- a/src-tauri/src/cmd/rclone_mount.rs +++ b/src-tauri/src/cmd/rclone_mount.rs @@ -1,3 +1,10 @@ +use std::fs; +use std::path::Path; + +use reqwest::Client; +use serde_json::json; +use tauri::State; + use super::http_api::get_process_list; use super::rclone_core::{RCLONE_API_BASE, RCLONE_AUTH}; use crate::conf::rclone::{RcloneCreateRemoteRequest, RcloneMountRequest, RcloneWebdavConfig}; @@ -6,12 +13,6 @@ use crate::object::structs::{ }; use crate::utils::api::{CreateProcessResponse, ProcessConfig, get_api_key, get_server_port}; use crate::utils::path::{get_app_logs_dir, get_rclone_binary_path}; -use reqwest::Client; -use serde_json::json; -use std::fs; -use std::path::Path; - -use tauri::State; #[tauri::command] pub async fn rclone_list_config( @@ -37,14 +38,12 @@ pub async fn rclone_list_config( } else if let Some(obj) = json.as_object() { let mut filtered_map = serde_json::Map::new(); for (remote_name, remote_config) in obj { - if let Some(config_obj) = remote_config.as_object() { - if let Some(remote_type_value) = config_obj.get("type") { - if let Some(type_str) = remote_type_value.as_str() { - if type_str == remote_type { - filtered_map.insert(remote_name.clone(), remote_config.clone()); - } - } - } + if let Some(config_obj) = remote_config.as_object() + && let Some(remote_type_value) = config_obj.get("type") + && let Some(type_str) = remote_type_value.as_str() + && type_str == remote_type + { + filtered_map.insert(remote_name.clone(), remote_config.clone()); } } serde_json::Value::Object(filtered_map) diff --git a/src-tauri/src/cmd/service.rs b/src-tauri/src/cmd/service.rs index 27b3e09..afd000c 100644 --- a/src-tauri/src/cmd/service.rs +++ b/src-tauri/src/cmd/service.rs @@ -1,13 +1,13 @@ -use crate::core::service::check_service_status as check_service_status_impl; -use crate::core::service::install_service as install_service_impl; -use crate::core::service::start_service as start_service_impl; - -use crate::core::service::uninstall_service as uninstall_service_impl; -use crate::object::structs::AppState; -use crate::utils::api::{get_api_key, get_server_port}; use reqwest; use tauri::State; +use crate::core::service::{ + check_service_status as check_service_status_impl, install_service as install_service_impl, + start_service as start_service_impl, uninstall_service as uninstall_service_impl, +}; +use crate::object::structs::AppState; +use crate::utils::api::{get_api_key, get_server_port}; + #[tauri::command] pub async fn check_service_status() -> Result { check_service_status_impl().await.map_err(|e| e.to_string()) diff --git a/src-tauri/src/conf/config.rs b/src-tauri/src/conf/config.rs index 40eb9cf..c07c63a 100644 --- a/src-tauri/src/conf/config.rs +++ b/src-tauri/src/conf/config.rs @@ -1,10 +1,12 @@ -use crate::conf::rclone::RcloneConfig; -use crate::{conf::core::OpenListCoreConfig, utils::path::app_config_file_path}; use std::path::PathBuf; -use super::app::AppConfig; use serde::{Deserialize, Serialize}; +use super::app::AppConfig; +use crate::conf::core::OpenListCoreConfig; +use crate::conf::rclone::RcloneConfig; +use crate::utils::path::app_config_file_path; + #[allow(unused)] pub static OPENLIST_CORE_CONFIG: &str = "data/config.json"; #[allow(unused)] @@ -78,11 +80,11 @@ impl MergedSettings { serde_json::from_str(&config).map_err(|e| e.to_string())? }; - if let Ok(Some(port)) = Self::get_port_from_data_config() { - if merged_settings.openlist.port != port { - merged_settings.openlist.port = port; - merged_settings.save()?; - } + if let Ok(Some(port)) = Self::get_port_from_data_config() + && merged_settings.openlist.port != port + { + merged_settings.openlist.port = port; + merged_settings.save()?; } Ok(merged_settings) diff --git a/src-tauri/src/core/service.rs b/src-tauri/src/core/service.rs index 14ecf10..e5cc147 100644 --- a/src-tauri/src/core/service.rs +++ b/src-tauri/src/core/service.rs @@ -1,13 +1,14 @@ -use log::error; - use std::env; use std::process::Command as StdCommand; +use log::error; + #[cfg(target_os = "windows")] pub async fn install_service() -> Result> { + use std::os::windows::process::CommandExt; + use deelevate::{PrivilegeLevel, Token}; use runas::Command as RunasCommand; - use std::os::windows::process::CommandExt; let app_dir = env::current_exe().unwrap().parent().unwrap().to_path_buf(); let install_path = app_dir.join("install-openlist-service.exe"); @@ -38,9 +39,10 @@ pub async fn install_service() -> Result> { #[cfg(target_os = "windows")] pub async fn uninstall_service() -> Result> { + use std::os::windows::process::CommandExt; + use deelevate::{PrivilegeLevel, Token}; use runas::Command as RunasCommand; - use std::os::windows::process::CommandExt; let app_dir = env::current_exe().unwrap().parent().unwrap().to_path_buf(); let uninstall_path = app_dir.join("uninstall-openlist-service.exe"); @@ -160,9 +162,10 @@ pub fn linux_elevator() -> String { #[cfg(target_os = "windows")] fn start_service_with_elevation(service_name: &str) -> Result> { + use std::os::windows::process::CommandExt; + use deelevate::{PrivilegeLevel, Token}; use runas::Command as RunasCommand; - use std::os::windows::process::CommandExt; let token = Token::with_current_process()?; let level = token.privilege_level()?; @@ -366,7 +369,7 @@ async fn start_openrc_service_with_check( return start_openrc_service(service_name).await; } else if stderr_str.contains("does not exist") { log::error!("Service {service_name} does not exist"); - return Ok(false); + Ok(false) } else { log::warn!("Unknown service status, attempting to start"); return start_openrc_service(service_name).await; @@ -502,16 +505,18 @@ fn detect_linux_init_system() -> String { return "openrc".to_string(); } - if let Ok(output) = StdCommand::new("which").arg("systemctl").output() { - if output.status.success() && !output.stdout.is_empty() { - return "systemd".to_string(); - } + if let Ok(output) = StdCommand::new("which").arg("systemctl").output() + && output.status.success() + && !output.stdout.is_empty() + { + return "systemd".to_string(); } - if let Ok(output) = StdCommand::new("which").arg("rc-service").output() { - if output.status.success() && !output.stdout.is_empty() { - return "openrc".to_string(); - } + if let Ok(output) = StdCommand::new("which").arg("rc-service").output() + && output.status.success() + && !output.stdout.is_empty() + { + return "openrc".to_string(); } "systemd".to_string() @@ -739,7 +744,8 @@ pub async fn start_service() -> Result> { if let Ok(status) = exit_status.parse::() { if status == 0 { log::info!( - "Service is loaded but not running (clean exit), attempting to start" + "Service is loaded but not running (clean exit), attempting to \ + start" ); return start_macos_service(SERVICE_IDENTIFIER).await; } else { diff --git a/src-tauri/src/lib.rs b/src-tauri/src/lib.rs index 4257cf4..f2f1680 100644 --- a/src-tauri/src/lib.rs +++ b/src-tauri/src/lib.rs @@ -13,6 +13,9 @@ use cmd::custom_updater::{ check_for_updates, download_update, get_current_version, install_update_and_restart, is_auto_check_enabled, restart_app, set_auto_check_enabled, }; +use cmd::http_api::{ + get_process_list, restart_process, start_process, stop_process, update_process, +}; use cmd::logs::{clear_logs, get_admin_password, get_logs}; use cmd::openlist_core::{create_openlist_core_process, get_openlist_core_status}; use cmd::os_operate::{ @@ -22,21 +25,14 @@ use cmd::os_operate::{ use cmd::rclone_core::{ create_and_start_rclone_backend, create_rclone_backend_process, get_rclone_backend_status, }; - use cmd::rclone_mount::{ check_mount_status, create_rclone_mount_remote_process, get_mount_info_list, rclone_create_remote, rclone_delete_remote, rclone_list_config, rclone_list_mounts, rclone_list_remotes, rclone_mount_remote, rclone_unmount_remote, rclone_update_remote, }; - -use cmd::http_api::{ - get_process_list, restart_process, start_process, stop_process, update_process, -}; - use cmd::service::{ check_service_status, install_service, start_service, stop_service, uninstall_service, }; - use object::structs::*; #[tauri::command] diff --git a/src-tauri/src/object/methods.rs b/src-tauri/src/object/methods.rs index 98a9359..2431e09 100644 --- a/src-tauri/src/object/methods.rs +++ b/src-tauri/src/object/methods.rs @@ -1,5 +1,6 @@ -use parking_lot::RwLock; use std::sync::Arc; + +use parking_lot::RwLock; use tauri::AppHandle; use crate::conf::config::MergedSettings; diff --git a/src-tauri/src/object/structs.rs b/src-tauri/src/object/structs.rs index f87814a..9f2fb9e 100644 --- a/src-tauri/src/object/structs.rs +++ b/src-tauri/src/object/structs.rs @@ -1,9 +1,11 @@ -use crate::conf::config::MergedSettings; +use std::sync::Arc; + use parking_lot::RwLock; use serde::{Deserialize, Serialize}; -use std::sync::Arc; use tauri::AppHandle; +use crate::conf::config::MergedSettings; + #[derive(Debug, Serialize, Clone)] pub struct ServiceStatus { pub running: bool, diff --git a/src-tauri/src/tray.rs b/src-tauri/src/tray.rs index 29b1f9b..dad90b8 100644 --- a/src-tauri/src/tray.rs +++ b/src-tauri/src/tray.rs @@ -1,10 +1,9 @@ use std::sync::Mutex; use std::time::{Duration, Instant}; -use tauri::{ - AppHandle, Emitter, Manager, - menu::{Menu, MenuItem, PredefinedMenuItem, Submenu}, - tray::{MouseButton, MouseButtonState, TrayIconBuilder, TrayIconEvent}, -}; + +use tauri::menu::{Menu, MenuItem, PredefinedMenuItem, Submenu}; +use tauri::tray::{MouseButton, MouseButtonState, TrayIconBuilder, TrayIconEvent}; +use tauri::{AppHandle, Emitter, Manager}; static LAST_MENU_UPDATE: Mutex> = Mutex::new(None); const MIN_UPDATE_INTERVAL: Duration = Duration::from_millis(5000); @@ -141,11 +140,11 @@ fn handle_menu_event(app_handle: &AppHandle, event: tauri::menu::MenuEvent) { pub fn update_tray_menu(app_handle: &AppHandle, service_running: bool) -> tauri::Result<()> { if let Ok(mut last_update) = LAST_MENU_UPDATE.lock() { - if let Some(last_time) = *last_update { - if last_time.elapsed() < MIN_UPDATE_INTERVAL { - log::debug!("Skipping tray menu update - too soon since last update"); - return Ok(()); - } + if let Some(last_time) = *last_update + && last_time.elapsed() < MIN_UPDATE_INTERVAL + { + log::debug!("Skipping tray menu update - too soon since last update"); + return Ok(()); } *last_update = Some(Instant::now()); } diff --git a/src-tauri/src/utils/api.rs b/src-tauri/src/utils/api.rs index 5565408..9af05f7 100644 --- a/src-tauri/src/utils/api.rs +++ b/src-tauri/src/utils/api.rs @@ -1,4 +1,5 @@ -use std::{collections::HashMap, env}; +use std::collections::HashMap; +use std::env; use serde::{Deserialize, Serialize}; diff --git a/src-tauri/src/utils/init_log.rs b/src-tauri/src/utils/init_log.rs index 6a61847..d6f3a8a 100644 --- a/src-tauri/src/utils/init_log.rs +++ b/src-tauri/src/utils/init_log.rs @@ -1,11 +1,9 @@ use log::LevelFilter; -use log4rs::{ - append::rolling_file::policy::compound::{ - CompoundPolicy, roll::fixed_window::FixedWindowRoller, trigger::size::SizeTrigger, - }, - config::{Appender, Config, Root}, - encode::pattern::PatternEncoder, -}; +use log4rs::append::rolling_file::policy::compound::CompoundPolicy; +use log4rs::append::rolling_file::policy::compound::roll::fixed_window::FixedWindowRoller; +use log4rs::append::rolling_file::policy::compound::trigger::size::SizeTrigger; +use log4rs::config::{Appender, Config, Root}; +use log4rs::encode::pattern::PatternEncoder; pub fn init_log() -> Result<(), Box> { let trigger = SizeTrigger::new(10 * 1024 * 1024); // 10 MB