rogue-runtime 0.1.0

Async RPC Runtime
Documentation
use serde::{Deserialize, Serialize};

use crate::{MessageId, RuntimeId, TypeId};

/// Stores data required to correctly route the messages between runtimes.
///
/// A `Message` contains:
/// - `target_id`: the ID of the destination runtime.
/// - `source_id`: the ID of the sending runtime.
/// - `message_id`: a unique identifier for the message.
/// - `is_answer`: `false` for requests, `true` for responses.
/// - `is_closed`: indicates end-of-stream for channels.
#[derive(Deserialize, Serialize)]
pub(crate) struct Message {
    /// id of the runtime the message should be handled by
    pub(crate) target_id: RuntimeId,

    /// id of the runtime initially sending the message
    pub(crate) source_id: RuntimeId,

    /// id of the message
    pub(crate) message_id: MessageId,

    /// used to identify which direction a message should be passed to since all message are
    /// handled by the same worker
    pub(crate) is_answer: bool,

    /// used to identify whether a message is the last one for a stream
    pub(crate) is_closed: bool, // TODO needed for channels?

    /// used to select the correct parser and handler
    pub(crate) data: MessageBody,
}

/// Represents a handshake message with data needed to set up routing.
#[derive(Deserialize, Serialize)]
pub(crate) struct HandshakeMessage {
    pub(crate) runtime_id: RuntimeId,
}

/// Represents an RPC message exchanged between runtimes.
///
/// Am `RpcMessage` contains:
/// - `type`: the `TypeId` used to select the correct handler.
/// - `data`: serialized payload or an error string.
#[derive(Deserialize, Serialize)]
pub(crate) struct RpcMessage {
    /// used to select the correct parser and handler
    pub(crate) r#type: TypeId,

    /// the actual data passed to the handler
    pub(crate) data: Result<Vec<u8>, String>, // TODO custom error types
}

#[derive(Deserialize, Serialize)]
pub(crate) enum MessageBody {
    Handshake(HandshakeMessage),
    Rpc(RpcMessage),
}