use anyhow::Result;
use std::str::FromStr;
custom_derive! {
#[allow(non_camel_case_types)]
#[derive(Debug, EnumFromStr, PartialEq)]
pub enum DeviceID {
LOGIC_4_DEVICE,
LOGIC_8_DEVICE,
LOGIC_PRO_8_DEVICE,
LOGIC_PRO_16_DEVICE,
}
}
#[derive(Debug, PartialEq)]
pub struct ConnectedDevice {
pub d_type: String,
pub name: String,
pub device_id: DeviceID,
pub index: String,
pub is_active: bool,
}
impl FromStr for ConnectedDevice {
type Err = std::num::ParseIntError;
fn from_str(response: &str) -> Result<Self, Self::Err> {
let v: Vec<&str> = response.split(',').map(str::trim_start).collect();
let device_id: DeviceID = v[2].parse().unwrap();
let is_active = v.len() == 5 && v[4] == "ACTIVE";
Ok(Self {
d_type: v[0].to_string(),
name: v[1].to_string(),
device_id,
index: v[3].to_string(),
is_active,
})
}
}