oc2_hlapi_v2 0.2.0

A client for the new version of OpenComputers II's HLAPI
Documentation
use std::convert::Infallible;

use uuid::Uuid;

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

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

impl Serial {
    /// Gets the address the card is set to
    pub fn get_address(&self) -> Result<u8, HlapiClientError> {
        Ok(self.call("getAddress", &[], &[])?.0)
    }

    /// Gets the base address of the card's registers in memory
    pub fn get_base_address(&self) -> Result<u64, HlapiClientError> {
        Ok(self.call("getBaseAddress", &[], &[])?.0)
    }

    /// Gets the baud rate the card is set to
    pub fn get_baud_rate(&self) -> Result<u32, HlapiClientError> {
        Ok(self.call("getBaudRate", &[], &[])?.0)
    }

    /// Gets the number of data errors since the computer started (how many times traffic arrived at
    /// the wrong baud rate)
    pub fn get_noise_count(&self) -> Result<u32, HlapiClientError> {
        Ok(self.call("getNoiseCount", &[], &[])?.0)
    }

    /// Gets the number of received bytes lost since the computer started
    pub fn get_overrun_count(&self) -> Result<u32, HlapiClientError> {
        Ok(self.call("getOverrunCount", &[], &[])?.0)
    }

    /// Gets how many receive collisions occurred since the computer started
    pub fn get_rx_error_count(&self) -> Result<u32, HlapiClientError> {
        Ok(self.call("getRxErrorCount", &[], &[])?.0)
    }

    /// Gets how many transmit collisions occurred since the computer started
    pub fn get_tx_error_count(&self) -> Result<u32, HlapiClientError> {
        Ok(self.call("getTxErrorCount", &[], &[])?.0)
    }
}

impl DeviceBase for Serial {
    const TYPE: &'static str = "serial";

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