use std::convert::Infallible;
use thiserror::Error;
use uuid::Uuid;
use crate::{
client::{DeviceBase, HlapiClient, HlapiClientError},
devices::ItemStack,
};
#[derive(Debug, Clone)]
pub struct Crafter {
client: HlapiClient,
uuid: Uuid,
}
#[non_exhaustive]
#[derive(Error, Debug)]
pub enum CrafterError {
#[error("HLAPI client error: {0}")]
HlapiClientError(#[from] HlapiClientError),
#[error("Slot out of range")]
SlotOutOfRange,
}
impl Crafter {
pub fn get_result(&self) -> Result<Option<ItemStack>, CrafterError> {
Ok(self.call("getResult", &[], &[])?.0)
}
pub fn is_slot_enabled(&self, slot: u8) -> Result<bool, CrafterError> {
if slot >= 9 {
return Err(CrafterError::SlotOutOfRange);
}
Ok(self.call("isSlotEnabled", &[&slot as _], &[])?.0)
}
pub fn set_slot_enabled(&self, slot: u8, enabled: bool) -> Result<(), CrafterError> {
if slot >= 9 {
return Err(CrafterError::SlotOutOfRange);
}
self.call::<()>("setSlotEnabled", &[&slot as _, &enabled as _], &[])?;
Ok(())
}
}
impl DeviceBase for Crafter {
const TYPE: &'static str = "crafter";
type Event = Infallible;
fn new(client: HlapiClient, uuid: Uuid) -> Result<Self, HlapiClientError> {
Ok(Self { client, uuid })
}
fn uuid_base(&self) -> Uuid {
self.uuid
}
fn client(&self) -> &HlapiClient {
&self.client
}
}