deepslate-protocol 0.1.0

Minecraft protocol primitives for the Deepslate proxy.
Documentation
//! Minecraft packet definitions.
//!
//! Only packets needed for the proxy's connection lifecycle are fully parsed.
//! All other packets are treated as opaque [`RawPacket`] values and forwarded
//! without inspection.

pub mod handshake;
pub mod login;
pub mod play;
pub mod status;

use bytes::{Buf, BufMut, Bytes};

use crate::types::ProtocolError;

/// A packet that the proxy does not need to inspect.
/// Contains the raw packet ID and undecoded payload bytes.
#[derive(Debug, Clone)]
pub struct RawPacket {
    /// The Minecraft packet ID.
    pub id: i32,
    /// The raw payload bytes (everything after the packet ID).
    pub data: Bytes,
}

/// Trait implemented by all typed packets.
pub trait Packet: Sized {
    /// The packet ID for this packet type.
    const PACKET_ID: i32;

    /// Decode this packet from the payload buffer (after the packet ID has been read).
    ///
    /// # Errors
    ///
    /// Returns `ProtocolError` if the data is malformed.
    fn decode(buf: &mut impl Buf) -> Result<Self, ProtocolError>;

    /// Encode this packet's fields into the buffer (without the packet ID).
    fn encode(&self, buf: &mut impl BufMut);
}