Skip to main content

Crate sioc_macros

Crate sioc_macros 

Source
Expand description

Derive macros for Sioc: generate typed Socket.IO event and acknowledgement types.

The four core traits are intentionally separate so you only derive what each use-site needs:

TraitDeriveNeeded for
EventType#[derive(EventType)]event name constant + ack/binary policy
AckType#[derive(AckType)]ack binary policy
SerializePayload#[derive(SerializePayload)]emitting (outbound)
DeserializePayload#[derive(DeserializePayload)]receiving (inbound)

#[derive(EventRouter)] is a convenience that generates TryFrom<DynEvent> for a dispatcher enum whose variants each wrap an Event<E>.

§Struct-level attributes (#[sioc(...)])

AttributeApplies toEffect
event(name = "str")EventTypeOverride the event name (default: snake_case of struct name).
event(ack = Type)EventTypeSet ack policy to HasAck<Type>; default is NoAck.
event(binary)EventTypeSet binary policy to HasBinary; default is NoBinary.
ack(binary)AckTypeSet binary policy to HasBinary; default is NoBinary.
strictDeserializePayloadReject payloads with more elements than fields (error on trailing data).

§Field-level attributes (#[sioc(...)])

AttributeApplies toEffect
flattenSerializePayload, DeserializePayloadSerialize each element of this collection field as a separate array element; deserialize all remaining elements into it. Place last.

§Example

// ignore: sioc-macros cannot dev-depend on sioc (circular); see sioc crate for runnable examples.
use sioc::prelude::*;

// Emit-only: only SerializePayload is required.
#[derive(EventType, SerializePayload)]
struct Ping;

// Recv-only: only DeserializePayload is required.
#[derive(EventType, DeserializePayload)]
struct Pong;

// Bidirectional with an explicit name.
#[derive(EventType, SerializePayload, DeserializePayload)]
#[sioc(event(name = "chat_message"))]
struct ChatMessage {
    text: String,
}

// Ack receive-only.
#[derive(AckType, DeserializePayload)]
struct ChatAck {
    ok: bool,
}

Derive Macros§

AckType
Derives the AckType trait, providing the binary policy associated type.
DeserializePayload
Derives the DeserializePayload trait.
EventRouter
Derives TryFrom<DynEvent> for a dispatcher enum.
EventType
Derives the EventType trait, providing the event name constant and ack/binary policy associated types.
SerializePayload
Derives the SerializePayload trait.