refactor(core,storage): deduplicate code patterns (TODO-014)

Extract PttCapability event construction helper in events.rs.
Extract ensure_dir() helper in chanora_storage. Add TODO(refactor)
annotations for patterns requiring shared crate or API changes.
This commit is contained in:
Edison Jwa
2026-06-11 10:05:06 +09:00
parent 484522cbb6
commit 292617f8e8
7 changed files with 35 additions and 25 deletions
+11
View File
@@ -22,6 +22,17 @@ impl From<PttBackendDescriptor> for PttDescriptorSnapshot {
}
}
impl SessionEvent {
/// Construct a `PttCapability` event from an audio backend descriptor.
pub fn ptt_capability_from_descriptor(desc: &PttBackendDescriptor) -> Self {
Self::PttCapability {
level: desc.level.as_str().to_string(),
backend_id: desc.backend_id.to_string(),
bound_input_class: desc.bound_input_class.unwrap_or("").to_string(),
}
}
}
/// Persisted PTT binding state exposed to callers.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct PersistedPttBinding {
+4 -23
View File
@@ -924,21 +924,13 @@ impl ChanoraSession {
// dialog) re-publish through the controller's
// descriptor-watch.
let initial_desc = controller.descriptor().await;
let _ = self.events_tx.send(SessionEvent::PttCapability {
level: initial_desc.level.as_str().to_string(),
backend_id: initial_desc.backend_id.to_string(),
bound_input_class: initial_desc.bound_input_class.unwrap_or("").to_string(),
});
let _ = self.events_tx.send(SessionEvent::ptt_capability_from_descriptor(&initial_desc));
let mut watch_rx = controller.descriptor_watch();
let events_tx = self.events_tx.clone();
tokio::spawn(async move {
while watch_rx.changed().await.is_ok() {
let d = watch_rx.borrow_and_update().clone();
let _ = events_tx.send(SessionEvent::PttCapability {
level: d.level.as_str().to_string(),
backend_id: d.backend_id.to_string(),
bound_input_class: d.bound_input_class.unwrap_or("").to_string(),
});
let _ = events_tx.send(SessionEvent::ptt_capability_from_descriptor(&d));
}
});
Ok(())
@@ -1000,11 +992,7 @@ impl ChanoraSession {
if let Some(state) = guard.as_ref() {
if let Some(controller) = state.ptt_controller.as_ref() {
let desc = controller.set_binding(binding).await?;
let _ = self.events_tx.send(SessionEvent::PttCapability {
level: desc.level.as_str().to_string(),
backend_id: desc.backend_id.to_string(),
bound_input_class: desc.bound_input_class.unwrap_or("").to_string(),
});
let _ = self.events_tx.send(SessionEvent::ptt_capability_from_descriptor(&desc));
}
}
Ok(())
@@ -2269,14 +2257,7 @@ async fn supervisor_loop(ctx: SupervisorContext) {
// capability (SRS-196 / SDD-091).
let d = controller.descriptor().await;
let _ =
events_tx.send(SessionEvent::PttCapability {
level: d.level.as_str().to_string(),
backend_id: d.backend_id.to_string(),
bound_input_class: d
.bound_input_class
.unwrap_or("")
.to_string(),
});
events_tx.send(SessionEvent::ptt_capability_from_descriptor(&d));
}
Err(e) => {
warn!(