use std::ops::Deref;
use super::list::DeviceInfo;
use super::Simctl;
#[derive(Clone, Debug)]
pub struct Device {
simctl: Simctl,
info: DeviceInfo,
}
impl Device {
pub(crate) fn new(simctl: Simctl, info: DeviceInfo) -> Device {
Device { simctl, info }
}
pub fn simctl(&self) -> &Simctl {
&self.simctl
}
pub fn info(&self) -> &DeviceInfo {
&self.info
}
}
impl Deref for Device {
type Target = DeviceInfo;
fn deref(&self) -> &Self::Target {
&self.info
}
}
pub trait DeviceQuery<'a>: Iterator<Item = &'a Device> {
fn available(self) -> Available<'a, Self>;
fn by_name<'b>(self, name: &'b str) -> ByName<'a, 'b, Self>;
}
pub struct Available<'a, I>(I)
where
I: Iterator<Item = &'a Device> + ?Sized;
impl<'a, I> Iterator for Available<'a, I>
where
I: Iterator<Item = &'a Device> + ?Sized,
{
type Item = &'a Device;
fn next(&mut self) -> Option<Self::Item> {
while let Some(next) = self.0.next() {
if next.is_available {
return Some(next);
}
}
None
}
}
pub struct ByName<'a, 'b, I>(&'b str, I)
where
I: Iterator<Item = &'a Device> + ?Sized;
impl<'a, I> Iterator for ByName<'a, '_, I>
where
I: Iterator<Item = &'a Device> + ?Sized,
{
type Item = &'a Device;
fn next(&mut self) -> Option<Self::Item> {
while let Some(next) = self.1.next() {
if next.name == self.0 {
return Some(next);
}
}
None
}
}
impl<'a, I> DeviceQuery<'a> for I
where
I: Iterator<Item = &'a Device>,
{
fn available(self) -> Available<'a, Self> {
Available(self)
}
fn by_name<'b>(self, name: &'b str) -> ByName<'a, 'b, Self> {
ByName(name, self)
}
}