use std::fmt;
use crate::error::{Error, Result};
use crate::option::{Buffer, OptionDescriptor, OptionId, SetInfo, Value, ValueType};
use crate::raw::Handle;
use crate::{Frame, Parameters, Scan};
pub struct Device<'s> {
pub(crate) handle: Handle<'s>,
name: String,
options: Vec<OptionDescriptor>,
}
impl<'s> Device<'s> {
pub(crate) fn new(name: String, handle: Handle<'s>) -> Result<Self> {
let mut device = Self {
handle,
name,
options: Vec::new(),
};
device.reload_options()?;
Ok(device)
}
pub fn name(&self) -> &str {
&self.name
}
pub fn options(&self) -> &[OptionDescriptor] {
&self.options
}
pub fn option(&self, id: impl OptionId) -> Option<&OptionDescriptor> {
id.find_in(&self.options)
}
pub fn reload_options(&mut self) -> Result<()> {
let (_, count) = self.handle.get_value(0)?;
let count = match count {
Buffer::Words(words) => words.first().copied().unwrap_or(0),
Buffer::Bytes(_) => return Err(Error::Protocol("option 0 is not an integer")),
};
let count =
usize::try_from(count).map_err(|_| Error::Protocol("negative number of options"))?;
self.options = (1..count)
.map_while(|index| self.handle.descriptor(index))
.collect();
Ok(())
}
fn resolve(&self, id: &impl OptionId) -> Result<&OptionDescriptor> {
id.find_in(&self.options)
.ok_or_else(|| Error::UnknownOption(id.label()))
}
pub fn value(&self, id: impl OptionId) -> Result<Value> {
let option = self.resolve(&id)?;
if !option.is_active() {
return Err(Error::InactiveOption(option.label()));
}
if !option.is_readable()
|| matches!(option.value_type, ValueType::Button | ValueType::Group)
{
return Err(Error::WriteOnlyOption(option.label()));
}
let (current, buffer) = self.handle.get_value(option.index)?;
buffer.decode(¤t)
}
pub fn set_value(&mut self, id: impl OptionId, value: impl Into<Value>) -> Result<SetInfo> {
let option = self.settable(&id)?;
let buffer = Buffer::encode(option, &value.into())?;
let index = option.index;
let info = self.handle.set_value(index, buffer)?;
self.after_set(info)
}
pub fn set_auto(&mut self, id: impl OptionId) -> Result<SetInfo> {
let index = self.settable(&id)?.index;
let info = self.handle.set_auto(index)?;
self.after_set(info)
}
pub fn press(&mut self, id: impl OptionId) -> Result<SetInfo> {
let option = self.settable(&id)?;
if option.value_type != ValueType::Button {
return Err(Error::TypeMismatch {
option: option.label(),
expected: option.value_type,
});
}
let index = option.index;
let info = self.handle.press(index)?;
self.after_set(info)
}
fn settable(&self, id: &impl OptionId) -> Result<&OptionDescriptor> {
let option = self.resolve(id)?;
if !option.is_active() {
return Err(Error::InactiveOption(option.label()));
}
if !option.is_settable() {
return Err(Error::ReadOnlyOption(option.label()));
}
Ok(option)
}
fn after_set(&mut self, info: SetInfo) -> Result<SetInfo> {
if info.reload_options() {
self.reload_options()?;
}
Ok(info)
}
pub fn parameters(&self) -> Result<Parameters> {
self.handle.parameters()
}
pub fn start(&mut self) -> Result<Scan<'_, 's>> {
self.handle.start()?;
Scan::new(self)
}
pub fn scan(&mut self) -> Result<Frame> {
self.start()?.read_image()
}
}
impl fmt::Debug for Device<'_> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("Device")
.field("name", &self.name)
.field("options", &self.options.len())
.finish_non_exhaustive()
}
}