oc2_hlapi_v2 0.2.0

A client for the new version of OpenComputers II's HLAPI
Documentation
//! The crafter device and its associated types
//!
//! [OC2 Docs](https://fnuecke.github.io/oc2/manual/en_us/device/crafter.html)
use std::convert::Infallible;

use thiserror::Error;
use uuid::Uuid;

use crate::{
    client::{DeviceBase, HlapiClient, HlapiClientError},
    devices::ItemStack,
};

#[derive(Debug, Clone)]
/// A crafter device
pub struct Crafter {
    client: HlapiClient,
    uuid: Uuid,
}
/// The possible errors for device functions
#[non_exhaustive]
#[derive(Error, Debug)]
pub enum CrafterError {
    #[error("HLAPI client error: {0}")]
    /// HLAPI client error
    HlapiClientError(#[from] HlapiClientError),
    #[error("Slot out of range")]
    /// Slot out of range
    SlotOutOfRange,
}

impl Crafter {
    /// Gets what the current items in the crafter would craft. [`None`] if they do not form a valid
    /// recipe.
    pub fn get_result(&self) -> Result<Option<ItemStack>, CrafterError> {
        Ok(self.call("getResult", &[], &[])?.0)
    }

    /// Gets whether a grid slot is enabled. Slots are 0-indexed.
    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)
    }

    /// Sets whether a grid slot is enabled. A slot must be empty to disable it. Slots are
    /// 0-indexed.
    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
    }
}