oc2_hlapi_v2 0.2.0

A client for the new version of OpenComputers II's HLAPI
Documentation
use erased_serde::Serialize;
use serde::de::DeserializeOwned;
use serde_json::Value as JsonValue;
use uuid::Uuid;

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

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

impl GenericDevice {
    /// Call a method on this device
    ///
    /// [`BlobMarker`] should be used to denote blobs in the method arguments and the return
    /// value.
    ///
    /// [`BlobMarker`]: crate::messages::BlobMarker
    #[allow(clippy::type_complexity)]
    pub fn call<R: DeserializeOwned>(
        &self,
        method: &str,
        args: &[&dyn Serialize],
        blobs: &[&[u8]],
    ) -> Result<(R, Vec<Vec<u8>>), HlapiClientError> {
        <Self as DeviceBase>::call(self, method, args, blobs)
    }
}

impl DeviceBase for GenericDevice {
    /// Generic wrapper, no single device type.
    const TYPE: &'static str = "";

    type Event = (String, JsonValue);

    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
    }

    fn parse_event(
        typ: &str,
        data: &JsonValue,
    ) -> Result<Self::Event, Box<dyn std::error::Error + Send + Sync>> {
        Ok((typ.to_owned(), data.clone()))
    }
}