oc2_hlapi_v2 0.2.0

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

use uuid::Uuid;

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

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

impl Lectern {
    /// Gets the page the book on the lectern is open at. 0 can mean the first page or an empty
    /// book, check if [`get_page_count`] is [`Some`] or not to confirm what a 0 return
    /// value means.
    ///
    /// [`get_page_count`]: Lectern::get_page_count
    pub fn get_page(&self) -> Result<u8, HlapiClientError> {
        Ok(self.call("getPage", &[], &[])?.0)
    }

    /// Gets how many pages the book on the lectern has. [`None`] if there is no book on the
    /// lectern.
    pub fn get_page_count(&self) -> Result<Option<NonZeroU8>, HlapiClientError> {
        let raw = self.call("getPageCount", &[], &[])?.0;
        Ok(NonZeroU8::new(raw))
    }
}

impl DeviceBase for Lectern {
    const TYPE: &'static str = "lectern";

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