use crate::device::{Device, Librem, Model, Pro, Status, Storage};
use crate::error::Error;
use crate::otp::GenerateOtp;
#[derive(Debug)]
#[non_exhaustive]
pub enum DeviceWrapper<'a> {
Librem(Librem<'a>),
Storage(Storage<'a>),
Pro(Pro<'a>),
}
impl<'a> DeviceWrapper<'a> {
fn device(&self) -> &dyn Device<'a> {
match *self {
DeviceWrapper::Librem(ref librem) => librem,
DeviceWrapper::Storage(ref storage) => storage,
DeviceWrapper::Pro(ref pro) => pro,
}
}
fn device_mut(&mut self) -> &mut dyn Device<'a> {
match *self {
DeviceWrapper::Librem(ref mut librem) => librem,
DeviceWrapper::Storage(ref mut storage) => storage,
DeviceWrapper::Pro(ref mut pro) => pro,
}
}
}
impl<'a> From<Librem<'a>> for DeviceWrapper<'a> {
fn from(device: Librem<'a>) -> Self {
DeviceWrapper::Librem(device)
}
}
impl<'a> From<Pro<'a>> for DeviceWrapper<'a> {
fn from(device: Pro<'a>) -> Self {
DeviceWrapper::Pro(device)
}
}
impl<'a> From<Storage<'a>> for DeviceWrapper<'a> {
fn from(device: Storage<'a>) -> Self {
DeviceWrapper::Storage(device)
}
}
impl<'a> GenerateOtp for DeviceWrapper<'a> {
fn get_hotp_slot_name(&self, slot: u8) -> Result<String, Error> {
self.device().get_hotp_slot_name(slot)
}
fn get_totp_slot_name(&self, slot: u8) -> Result<String, Error> {
self.device().get_totp_slot_name(slot)
}
fn get_hotp_code(&mut self, slot: u8) -> Result<String, Error> {
self.device_mut().get_hotp_code(slot)
}
fn get_totp_code(&self, slot: u8) -> Result<String, Error> {
self.device().get_totp_code(slot)
}
}
impl<'a> Device<'a> for DeviceWrapper<'a> {
fn into_manager(self) -> &'a mut crate::Manager {
match self {
DeviceWrapper::Librem(dev) => dev.into_manager(),
DeviceWrapper::Pro(dev) => dev.into_manager(),
DeviceWrapper::Storage(dev) => dev.into_manager(),
}
}
fn get_model(&self) -> Model {
match *self {
DeviceWrapper::Librem(_) => Model::Librem,
DeviceWrapper::Pro(_) => Model::Pro,
DeviceWrapper::Storage(_) => Model::Storage,
}
}
fn get_status(&self) -> Result<Status, Error> {
match self {
DeviceWrapper::Librem(dev) => dev.get_status(),
DeviceWrapper::Pro(dev) => dev.get_status(),
DeviceWrapper::Storage(dev) => dev.get_status(),
}
}
}