From d81a6fcebce56d6943c7a803b5eda4559351c7ce Mon Sep 17 00:00:00 2001 From: shm Date: Sun, 1 Dec 2024 17:02:39 +0900 Subject: [PATCH] =?UTF-8?q?=E3=83=AA=E3=83=95=E3=82=A1=E3=82=AF=E3=82=BF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src-tauri/src/services/wmi_service.rs | 38 ++++++++++++--------------- src/rspc/bindings.ts | 2 +- 2 files changed, 18 insertions(+), 22 deletions(-) diff --git a/src-tauri/src/services/wmi_service.rs b/src-tauri/src/services/wmi_service.rs index 305c9c5..51e497a 100644 --- a/src-tauri/src/services/wmi_service.rs +++ b/src-tauri/src/services/wmi_service.rs @@ -86,27 +86,23 @@ pub async fn get_gpu_usage_by_device_and_engine( // 正規表現で `engtype_xxx` の部分を抽出 let re = Regex::new(r"engtype_(\w+)").unwrap(); - for engine in results.iter() { - if let Some(captures) = re.captures(&engine.name) { - if let Some(engine_name) = captures.get(1) { - if engine_name.as_str() == engine_type { - if let Some(load) = engine.utilization_percentage { - return Ok(load as f32 / 100.0); - } else { - return Err(Box::new(std::io::Error::new( - std::io::ErrorKind::NotFound, - format!("No usage data available for engine type: {}", engine_type), - ))); - } - } - } - } - } - - Err(Box::new(std::io::Error::new( - std::io::ErrorKind::NotFound, - format!("No GPU engine found: engine_type: {}", engine_type,), - ))) + results + .iter() + .find_map(|engine| { + re.captures(&engine.name) + .and_then(|captures| captures.get(1)) + .filter(|engine_name| engine_name.as_str() == engine_type) + .and_then(|_| { + engine + .utilization_percentage + .map(|load| load as f32 / 100.0) + }) + }) + .ok_or_else(|| { + let message = format!("No usage data available for engine type: {}", engine_type); + Box::new(std::io::Error::new(std::io::ErrorKind::NotFound, message)) + as Box + }) } /// diff --git a/src/rspc/bindings.ts b/src/rspc/bindings.ts index 1e39c98..419bbeb 100644 --- a/src/rspc/bindings.ts +++ b/src/rspc/bindings.ts @@ -252,7 +252,7 @@ async getBackgroundImages() : Promise> { * * ### TODO * - JsImage https://docs.rs/tauri/2.1.1/tauri/image/enum.JsImage.html を使用する - * - + * - specta での型定義が難しかったため一旦 Base64 で実装 * * */