benthic_protocol 0.3.2

Benthic protocol, an intermediate layer between metaverse protocols and client programs.
Documentation
use crate::messages::{
    errors::ParseError,
    ui::{
        agent_update::AgentUpdate, camera_position::CameraPosition,
        chat_from_simulator::ChatFromSimulator, chat_from_viewer::ChatFromUI,
        coarse_location_update::CoarseLocationUpdate, disable_simulator::DisableSimulator,
        errors::SessionError, land_update::LandUpdate, login_event::Login,
        login_response::LoginResponse, logout::Logout, mesh_update::MeshUpdate,
        play_animation::PlayAnimation, skybox_update::SkyboxUpdate, water_update::WaterUpdate,
    },
};
use serde::{Deserialize, Serialize};

#[derive(Serialize, Deserialize, Debug, Clone)]
#[serde(tag = "type", content = "data")] // for json simplicity
/// The type of the message used to notify the UI .
/// Some of these also implement PacketType, as they are part of the spec, but not all of them are.
pub enum UIResponse {
    /// Custom login event for sending from the UI to the core
    Login(Login),
    /// Message for sending chat events from the UI to the core to the server.
    ChatFromViewer(ChatFromUI),
    /// Message for sending logout events from the UI to the core to the server.
    Logout(Logout),

    /// Message for informing the server of updates to the agent, like head position, motion, and
    /// FOV.
    AgentUpdate(AgentUpdate),
}
impl UIResponse {
    /// converts a UiEvent object to bytes using serde.
    /// Bytes can be decoded directly as JSON objects
    pub fn to_bytes(&self) -> Vec<u8> {
        serde_json::to_vec(self).expect("Failed to serialize UIResponse")
    }

    /// Converts bytes to a UIEvent.
    pub fn from_bytes(bytes: &[u8]) -> Result<Self, ParseError> {
        serde_json::from_slice(bytes).map_err(ParseError::SerdeError)
    }
}

#[derive(Serialize, Deserialize, Debug, Clone)]
#[serde(tag = "type", content = "data")] // for json simplicity
/// Message types for sending between the core and the UI. These are truncated types that do not
/// contain all of the values that their packet types contain.
pub enum UIMessage {
    /// Message for sending chat events from the server, to the core, to the UI
    ChatFromSimulator(ChatFromSimulator),
    /// Message for sending location update events from the server to the core to the UI.
    CoarseLocationUpdate(CoarseLocationUpdate),
    /// Custom LoginResponse event for notifying the UI of login
    LoginResponse(LoginResponse),
    /// Custom MeshUpdate event for notifying the UI of a mesh to render
    MeshUpdate(MeshUpdate),
    /// Custom SessionError event for notifying the UI of core errors
    Error(SessionError),
    /// Message for informing the core and UI of a server disconnect
    DisableSimulator(DisableSimulator),
    /// Message for informing the UI of a land packet
    LandUpdate(LandUpdate),
    /// Message for informing the UI of updated water information
    WaterUpdate(WaterUpdate),
    /// Message for informing the UI of an update to the skybox
    SkyboxUpdate(SkyboxUpdate),
    /// Message for setting the position of the avatar's camera
    CameraPosition(CameraPosition),
    /// message for informing the UI of an animation that is currently playing
    PlayAnimation(PlayAnimation),
}

impl UIMessage {
    /// converts a UiEvent object to bytes using serde.
    /// Bytes can be decoded directly as JSON objects
    pub fn to_bytes(&self) -> Vec<u8> {
        serde_json::to_vec(self).expect("Failed to serialize UIMessage")
    }

    /// Converts bytes to a UIEvent.
    pub fn from_bytes(bytes: &[u8]) -> Result<Self, ParseError> {
        serde_json::from_slice(bytes).map_err(ParseError::SerdeError)
    }
}