Improve to_string and add name

This commit is contained in:
Lennard Kittner
2025-03-22 12:22:32 +01:00
parent 3ed6b8510f
commit d5fcc8286c

View File

@@ -26,6 +26,7 @@ pub fn connect_compatible_device() -> Result<Box<dyn Device>, DeviceError> {
#[derive(Debug)] #[derive(Debug)]
pub struct DeviceState { pub struct DeviceState {
pub hid_device: HidDevice, pub hid_device: HidDevice,
pub device_name: Option<String>,
pub battery_level: Option<u8>, pub battery_level: Option<u8>,
pub charging: Option<ChargingStatus>, pub charging: Option<ChargingStatus>,
pub muted: Option<bool>, pub muted: Option<bool>,
@@ -44,19 +45,17 @@ impl Display for DeviceState {
let unknown = "Unknown".to_string(); let unknown = "Unknown".to_string();
write!( write!(
f, f,
" "Battery level: {}
Battery level: {} Carging status: {}
Carging status: {} Muted: {}
Muted: {} Mic connected: {}
Mic connected: {} Automatic shutdown after: {}
Automatic shutdown after: {} Pairing info: {}
Pairing info: {} Product color: {}
Product color: {} Side tone on: {}
Side tone on: {} Side tone volume: {}
Side tone volume: {} Voice prompt on: {}
Voice prompt on: {} Connected: {}",
Connected: {}
",
self.battery_level self.battery_level
.map_or(unknown.clone(), |l| format!("{l}%")), .map_or(unknown.clone(), |l| format!("{l}%")),
self.charging.map_or(unknown.clone(), |c| c.to_string()), self.charging.map_or(unknown.clone(), |c| c.to_string()),
@@ -93,8 +92,10 @@ impl DeviceState {
} }
}) })
.ok_or(DeviceError::NoDeviceFound())??; .ok_or(DeviceError::NoDeviceFound())??;
let device_name = hid_device.get_product_string()?;
Ok(DeviceState { Ok(DeviceState {
hid_device, hid_device,
device_name,
charging: None, charging: None,
battery_level: None, battery_level: None,
muted: None, muted: None,
@@ -109,6 +110,40 @@ impl DeviceState {
}) })
} }
pub fn to_better_string(&self) -> String {
let unknown = "Unknown".to_string();
format!(
"Battery level: {}
Carging status: {}
Muted: {}
Mic connected: {}
Automatic shutdown after: {}
Pairing info: {}
Product color: {}
Side tone on: {}
Side tone volume: {}
Voice prompt on: {}
Connected: {}",
self.battery_level
.map_or(unknown.clone(), |l| format!("{l}%")),
self.charging.map_or(unknown.clone(), |c| c.to_string()),
self.muted.map_or(unknown.clone(), |m| m.to_string()),
self.mic_connected
.map_or(unknown.clone(), |m| m.to_string()),
self.automatic_shutdown_after
.map_or(unknown.clone(), |a| format!("{} min", (a.as_secs() / 60))),
self.pairing_info.map_or(unknown.clone(), |p| p.to_string()),
self.product_color
.map_or(unknown.clone(), |c| c.to_string()),
self.side_tone_on.map_or(unknown.clone(), |s| s.to_string()),
self.side_tone_volume
.map_or(unknown.clone(), |s| s.to_string()),
self.voice_prompt_on
.map_or(unknown.clone(), |v| v.to_string()),
self.connected.map_or(unknown.clone(), |c| c.to_string()),
)
}
fn update_self_with_event(&mut self, event: &DeviceEvent) { fn update_self_with_event(&mut self, event: &DeviceEvent) {
match event { match event {
DeviceEvent::BatterLevel(level) => self.battery_level = Some(*level), DeviceEvent::BatterLevel(level) => self.battery_level = Some(*level),