style(v0.3): rustfmt power FSM/service/integration

Прогон cargo fmt (гейт lint): многострочное форматирование match-веток FSM,
PowerMock/PowerService-литералов, integration-вызовов. Без изменений логики.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Signed-off-by: Alexander <akotenev2003@gmail.com>
This commit is contained in:
2026-06-24 23:22:12 +03:00
parent 586ba29821
commit 92a11c3c72
3 changed files with 86 additions and 19 deletions
+70 -15
View File
@@ -13,7 +13,10 @@ pub enum State {
Off,
Accessory,
Running,
ShuttingDown { phase: Phase, reason: ShutdownReason },
ShuttingDown {
phase: Phase,
reason: ShutdownReason,
},
Sleep, // зарезервировано (полные sleep/wake — v1/v2)
BatteryCutoff, // зарезервировано (long-park — v1/v2)
}
@@ -44,7 +47,9 @@ pub struct PowerFsm {
impl Default for PowerFsm {
fn default() -> Self {
Self { state: State::Running }
Self {
state: State::Running,
}
}
}
@@ -78,7 +83,10 @@ impl PowerFsm {
/// Источник питания — сигнал потребителям «времени мало» при shutdown.
pub fn source(&self) -> PowerSource {
match self.state {
State::ShuttingDown { reason: ShutdownReason::UnderVoltage, .. } => PowerSource::LowBattery,
State::ShuttingDown {
reason: ShutdownReason::UnderVoltage,
..
} => PowerSource::LowBattery,
State::ShuttingDown { .. } => PowerSource::HoldupCap,
_ => PowerSource::Vehicle12v,
}
@@ -104,7 +112,10 @@ impl PowerFsm {
}
// ACC-off: линия ACC сменилась (AccChanged) + старт shutdown.
(Accessory | Running, E::AccOff) => {
self.state = ShuttingDown { phase: Abortable, reason: ShutdownReason::AccOff };
self.state = ShuttingDown {
phase: Abortable,
reason: ShutdownReason::AccOff,
};
vec![
Action::AccChanged(false),
Action::ShutdownImminent(ShutdownReason::AccOff),
@@ -112,14 +123,30 @@ impl PowerFsm {
]
}
// under-voltage/thermal: ACC не менялся → без AccChanged.
(Accessory | Running, E::UnderVoltage) => self.begin_shutdown(ShutdownReason::UnderVoltage),
(Accessory | Running, E::UnderVoltage) => {
self.begin_shutdown(ShutdownReason::UnderVoltage)
}
(Accessory | Running, E::ThermalTrip) => self.begin_shutdown(ShutdownReason::Thermal),
(ShuttingDown { phase: Abortable, .. }, E::AccOn) => {
(
ShuttingDown {
phase: Abortable, ..
},
E::AccOn,
) => {
self.state = Running;
vec![Action::ShutdownAborted, Action::AccChanged(true)]
}
(ShuttingDown { phase: Abortable, reason }, E::GraceExpired) => {
self.state = ShuttingDown { phase: Committed, reason };
(
ShuttingDown {
phase: Abortable,
reason,
},
E::GraceExpired,
) => {
self.state = ShuttingDown {
phase: Committed,
reason,
};
vec![Action::Commit]
}
// committed/off/sleep/battery_cutoff + всё прочее — no-op (committed не abort-ится)
@@ -128,7 +155,10 @@ impl PowerFsm {
}
fn begin_shutdown(&mut self, reason: ShutdownReason) -> Vec<Action> {
self.state = State::ShuttingDown { phase: Phase::Abortable, reason };
self.state = State::ShuttingDown {
phase: Phase::Abortable,
reason,
};
vec![Action::ShutdownImminent(reason), Action::StartGrace]
}
}
@@ -146,7 +176,9 @@ mod tests {
#[test]
fn accessory_engine_on_to_running_and_back() {
let mut f = PowerFsm { state: State::Accessory };
let mut f = PowerFsm {
state: State::Accessory,
};
assert_eq!(f.step(Event::EngineOn), vec![]);
assert_eq!(f.state(), State::Running);
assert_eq!(f.step(Event::EngineOff), vec![]);
@@ -193,20 +225,43 @@ mod tests {
f.step(Event::AccOff);
assert_eq!(f.step(Event::GraceExpired), vec![Action::Commit]);
assert_eq!(f.step(Event::AccOn), vec![]); // committed: abort игнорируется
assert!(matches!(f.state(), State::ShuttingDown { phase: Phase::Committed, .. }));
assert!(matches!(
f.state(),
State::ShuttingDown {
phase: Phase::Committed,
..
}
));
}
#[test]
fn reserved_states_noop() {
let mut f = PowerFsm { state: State::Sleep };
let mut f = PowerFsm {
state: State::Sleep,
};
assert_eq!(f.step(Event::AccOn), vec![]);
assert_eq!(f.state(), State::Sleep);
}
#[test]
fn ignition_projection() {
assert_eq!(PowerFsm { state: State::Running }.ignition(), IgnitionState::Running);
assert_eq!(PowerFsm { state: State::Accessory }.ignition(), IgnitionState::Accessory);
assert_eq!(PowerFsm { state: State::Off }.ignition(), IgnitionState::Off);
assert_eq!(
PowerFsm {
state: State::Running
}
.ignition(),
IgnitionState::Running
);
assert_eq!(
PowerFsm {
state: State::Accessory
}
.ignition(),
IgnitionState::Accessory
);
assert_eq!(
PowerFsm { state: State::Off }.ignition(),
IgnitionState::Off
);
}
}
+6 -2
View File
@@ -18,7 +18,9 @@ pub struct PowerService {
impl Default for PowerService {
fn default() -> Self {
Self { fsm: Arc::new(Mutex::new(PowerFsm::new())) }
Self {
fsm: Arc::new(Mutex::new(PowerFsm::new())),
}
}
}
@@ -38,7 +40,9 @@ impl PowerService {
#[cfg(feature = "dev-mocks")]
pub fn mock(&self) -> PowerMock {
PowerMock { fsm: Arc::clone(&self.fsm) }
PowerMock {
fsm: Arc::clone(&self.fsm),
}
}
}
@@ -55,8 +55,16 @@ async fn shutdown_imminent_then_abort() {
let svc = PowerService::new();
let mock = svc.mock();
let server = zbus::Connection::session().await.unwrap();
server.object_server().at(names::power::PATH, svc).await.unwrap();
server.object_server().at(names::power::PATH, mock).await.unwrap();
server
.object_server()
.at(names::power::PATH, svc)
.await
.unwrap();
server
.object_server()
.at(names::power::PATH, mock)
.await
.unwrap();
server.request_name(names::power::NAME).await.unwrap();
let client = zbus::Connection::session().await.unwrap();