use regex::bytes::Regex;
use std::{fmt::Display, str::FromStr, sync::LazyLock};
use crate::{RustADBError, server::DeviceState};
static DEVICES_REGEX: LazyLock<Regex> =
LazyLock::new(|| Regex::new("^(\\S+)\t(\\w+)\n?$").expect("Cannot build devices regex"));
#[derive(Debug, Clone)]
pub struct DeviceShort {
pub identifier: String,
pub state: DeviceState,
}
impl Display for DeviceShort {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}\t{}", self.identifier, self.state)
}
}
impl TryFrom<Vec<u8>> for DeviceShort {
type Error = RustADBError;
fn try_from(value: Vec<u8>) -> Result<Self, Self::Error> {
let groups = DEVICES_REGEX
.captures(&value)
.ok_or(RustADBError::RegexParsingError)?;
Ok(Self {
identifier: String::from_utf8(
groups
.get(1)
.ok_or(RustADBError::RegexParsingError)?
.as_bytes()
.to_vec(),
)?,
state: DeviceState::from_str(&String::from_utf8(
groups
.get(2)
.ok_or(RustADBError::RegexParsingError)?
.as_bytes()
.to_vec(),
)?)?,
})
}
}