From d040f51cb236eaf92e5f37c83f7ea345d1f76da8 Mon Sep 17 00:00:00 2001 From: XiuChengWu <732857315@qq.com> Date: Tue, 31 Mar 2026 23:25:47 +0800 Subject: [PATCH] fix: improve error handling and code robustness - state_machine.rs: if-let replaces is_some()+take().unwrap() - state_machine.rs: expect() replaces unwrap() with invariant docs - wifi/mod.rs: send_result uses self.id() instead of hardcoded string --- src/plugins/video/state_machine.rs | 6 +++--- src/plugins/wifi/mod.rs | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/plugins/video/state_machine.rs b/src/plugins/video/state_machine.rs index 062d929..01f9088 100644 --- a/src/plugins/video/state_machine.rs +++ b/src/plugins/video/state_machine.rs @@ -51,8 +51,7 @@ impl StateMachine { } // 有待执行的触发器时,跳过 sequence 剩余 step,立即切换状态 - if self.pending_trigger_target.is_some() { - let target_state = self.pending_trigger_target.take().unwrap(); + if let Some(target_state) = self.pending_trigger_target.take() { self.transition_to_state(&target_state)?; return Ok(self.current_state != old_state); } @@ -273,7 +272,8 @@ impl StateMachine { } } - Ok(free_states.last().unwrap().0.clone()) + // free_states 非空已由上方 bail! 保证 + Ok(free_states.last().expect("free_states is non-empty").0.clone()) } fn select_weighted_next_state(&self, entries: &[NextStateEntry]) -> Option { diff --git a/src/plugins/wifi/mod.rs b/src/plugins/wifi/mod.rs index b3582f8..0899f00 100644 --- a/src/plugins/wifi/mod.rs +++ b/src/plugins/wifi/mod.rs @@ -111,7 +111,7 @@ impl WifiPlugin { .context("wifi plugin context is not initialized")?; ctx.tx.send(Envelope { - from: "wifi".to_string(), + from: self.id().to_string(), to: Destination::Broadcast, message: Message::WifiResult(payload), })?;