oc2_hlapi_v2 0.2.0

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

use std::{convert::Infallible, num::NonZeroU8};

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

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

#[derive(Debug, Clone)]
/// The device for the crafting module
pub struct Crafting {
    client: HlapiClient,
    uuid: Uuid,
}

/// The possible errors for device functions
#[non_exhaustive]
#[derive(Error, Debug)]
pub enum CraftingError {
    #[error("HLAPI client error: {0}")]
    /// HLAPI client error
    HlapiClientError(#[from] HlapiClientError),
    #[error("Operation failed")]
    /// Operation failed
    OperationFailed,
}

impl Crafting {
    /// Tries to craft from the crafting grid. Returns the number of items crafted on success.
    pub fn craft(&self) -> Result<NonZeroU8, CraftingError> {
        let raw = self.call("craft", &[], &[])?.0;
        NonZeroU8::new(raw).ok_or(CraftingError::OperationFailed)
    }
}

impl DeviceBase for Crafting {
    const TYPE: &'static str = "crafting";

    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
    }
}