oc2_hlapi_v2 0.2.0

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

use std::convert::Infallible;

use serde::{Deserialize, Serialize};
use uuid::Uuid;

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

/// The modes of a comparator
#[derive(Serialize, Deserialize, Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[serde(rename_all = "camelCase")]
pub enum ComparatorMode {
    /// Compare mode
    Compare,
    /// Subtract mode
    Subtract,
}

#[derive(Debug, Clone)]
/// A comparator device
pub struct Comparator {
    client: HlapiClient,
    uuid: Uuid,
}

impl Comparator {
    /// Gets the mode the comparator is in
    pub fn get_mode(&self) -> Result<ComparatorMode, HlapiClientError> {
        Ok(self.call("getMode", &[], &[])?.0)
    }

    /// Gets the comparator's current output signal strength
    pub fn get_output_signal(&self) -> Result<u8, HlapiClientError> {
        Ok(self.call("getOutputSignal", &[], &[])?.0)
    }

    /// Sets the mode the comparator is in
    pub fn set_mode(&self, mode: ComparatorMode) -> Result<(), HlapiClientError> {
        self.call::<()>("setMode", &[&mode as _], &[])?;
        Ok(())
    }
}

impl DeviceBase for Comparator {
    const TYPE: &'static str = "comparator";

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