oc2_hlapi_v2 0.2.0

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

use uuid::Uuid;

use crate::{
    client::{DeviceBase, HlapiClient, HlapiClientError},
    devices::{TankContentInfo, robot::RobotSide},
};

#[derive(Debug, Clone)]
/// The device for the tank operations module
///
/// [OC2 Docs](https://fnuecke.github.io/oc2/manual/en_us/device/tank_operations.html)
pub struct TankOperations {
    client: HlapiClient,
    uuid: Uuid,
}

impl TankOperations {
    /// Tries to take up to the given number of millibuckets from the specified side, defaulting to
    /// [`RobotSide::Front`]. Fluids are taken from the world if there is no container to that side.
    /// Returns the actual number of millibuckets taken.
    pub fn drain(&self, max_count: u32, side: Option<RobotSide>) -> Result<u32, HlapiClientError> {
        Ok(self.call("drain", &[&max_count as _, &side as _], &[])?.0)
    }

    /// Tries to fill the tank to the specified side with the given number of millibuckets,
    /// defaulting to [`RobotSide::Front`]. Fluids are drained into the world if there is no tank
    /// to that side. Returns the actual number of millibuckets filled.
    pub fn fill(&self, count: u32, side: Option<RobotSide>) -> Result<u32, HlapiClientError> {
        Ok(self.call("fill", &[&count as _, &side as _], &[])?.0)
    }

    /// Gets the contents of the given tank in the container to the specified side, defaulting to
    /// [`RobotSide::Front`]. Returns [`None`] if the tank is empty.
    pub fn get_fluid_in_tank(
        &self,
        tank: u32,
        side: Option<RobotSide>,
    ) -> Result<Option<TankContentInfo>, HlapiClientError> {
        Ok(self
            .call("getFluidInTank", &[&tank as _, &side as _], &[])?
            .0)
    }

    /// Gets the number of millibuckets the given tank in the container to the specified side,
    /// defaulting to [`RobotSide::Front`], can hold.
    pub fn get_fluid_tank_capacity(
        &self,
        tank: u32,
        side: Option<RobotSide>,
    ) -> Result<u32, HlapiClientError> {
        Ok(self
            .call("getFluidTankCapacity", &[&tank as _, &side as _], &[])?
            .0)
    }

    /// Gets the number of tanks in the fluid container to the specified side, defaulting to [`RobotSide::Front`].
    /// Returns [`None`] if there is no container to that side.
    pub fn get_fluid_tank_count(
        &self,
        side: Option<RobotSide>,
    ) -> Result<Option<NonZeroU32>, HlapiClientError> {
        let raw = self.call("getFluidTankCount", &[&side as _], &[])?.0;
        Ok(NonZeroU32::new(raw))
    }

    /// Moves up to the given number of millibuckets from the container in the selected slot to the
    /// robot's internal tank, the actual number of millibuckets moved.
    pub fn move_from(&self, max_count: u32) -> Result<u32, HlapiClientError> {
        Ok(self.call("moveFrom", &[&max_count as _], &[])?.0)
    }

    /// Moves up to the given number of millibuckets from the robot's internal tank to the container
    /// in the selected slot, returning the actual number of millibuckets moved.
    pub fn move_into(&self, max_count: u32) -> Result<u32, HlapiClientError> {
        Ok(self.call("moveInto", &[&max_count as _], &[])?.0)
    }
}

impl DeviceBase for TankOperations {
    const TYPE: &'static str = "tank_operations";

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