oc2_hlapi_v2 0.2.0

A client for the new version of OpenComputers II's HLAPI
Documentation
use std::{convert::Infallible, time::Duration};

use uuid::Uuid;

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

#[derive(Debug, Clone)]
/// A furnace device
///
/// [OC2 Docs](https://fnuecke.github.io/oc2/manual/en_us/device/furnace.html)
pub struct Furnace {
    client: HlapiClient,
    uuid: Uuid,
}

impl Furnace {
    /// Gets how long the last consumed fuel burns in total. [`None`] if the furnace has never consumed
    /// fuel.
    pub fn get_burn_duration(&self) -> Result<Option<Duration>, HlapiClientError> {
        let raw: u32 = self.call("getBurnDuration", &[], &[])?.0;
        if raw == 0 {
            Ok(None)
        } else {
            Ok(Some(Duration::from_millis(raw as u64 * 50)))
        }
    }

    /// Gets how much longer the currently burning fuel will burn for. [`None`] if the furnace is not burning.
    pub fn get_burn_time(&self) -> Result<Option<Duration>, HlapiClientError> {
        let raw: u32 = self.call("getBurnTime", &[], &[])?.0;
        if raw == 0 {
            Ok(None)
        } else {
            Ok(Some(Duration::from_millis(raw as u64 * 50)))
        }
    }

    /// Gets how long the currently cooking item has been cooking.
    /// [`None`] if the furnace is not cooking an item, or the item just started cooking in the
    /// current game tick.
    pub fn get_cook_time(&self) -> Result<Option<Duration>, HlapiClientError> {
        let raw: u32 = self.call("getCookTime", &[], &[])?.0;
        if raw == 0 {
            Ok(None)
        } else {
            Ok(Some(Duration::from_millis(raw as u64 * 50)))
        }
    }

    /// Gets how long the currently cooking item needs to cook in total, or if no item is cooking, the last cooked item.
    /// [`None`] if the furnace has never cooked an item.
    pub fn get_cook_time_total(&self) -> Result<Option<Duration>, HlapiClientError> {
        let raw: u32 = self.call("getCookTimeTotal", &[], &[])?.0;
        if raw == 0 {
            Ok(None)
        } else {
            Ok(Some(Duration::from_millis(raw as u64 * 50)))
        }
    }

    /// Gets whether the furnace is currently burning fuel
    pub fn is_burning(&self) -> Result<bool, HlapiClientError> {
        Ok(self.call("isBurning", &[], &[])?.0)
    }
}

impl DeviceBase for Furnace {
    const TYPE: &'static str = "furnace";

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