Trait Parse

Source
pub trait Parse: Default + Copy {
    // Required methods
    fn encode(self, packet: Packet) -> Value;
    fn decode_str(
        self,
        state: &ParserState,
        data: Str,
    ) -> Result<Packet, ParseError>;
    fn decode_bin(
        self,
        state: &ParserState,
        bin: Bytes,
    ) -> Result<Packet, ParseError>;
    fn encode_value<T: ?Sized + Serialize>(
        self,
        data: &T,
        event: Option<&str>,
    ) -> Result<Value, ParserError>;
    fn decode_value<'de, T: Deserialize<'de>>(
        self,
        value: &'de mut Value,
        with_event: bool,
    ) -> Result<T, ParserError>;
    fn decode_default<'de, T: Deserialize<'de>>(
        self,
        value: Option<&'de Value>,
    ) -> Result<T, ParserError>;
    fn encode_default<T: ?Sized + Serialize>(
        self,
        data: &T,
    ) -> Result<Value, ParserError>;
    fn read_event(self, value: &Value) -> Result<&str, ParserError>;
}
Expand description

All socket.io parser should implement this trait. Parsers should be stateless.

Required Methods§

Source

fn encode(self, packet: Packet) -> Value

Convert a packet into multiple payloads to be sent.

Source

fn decode_str( self, state: &ParserState, data: Str, ) -> Result<Packet, ParseError>

Parse a given input string. If the payload needs more adjacent binary packet, the partial packet will be kept and a ParseError::NeedsMoreBinaryData will be returned.

Source

fn decode_bin( self, state: &ParserState, bin: Bytes, ) -> Result<Packet, ParseError>

Parse a given input binary. If the payload needs more adjacent binary packet, the partial packet is still kept and a ParseError::NeedsMoreBinaryData will be returned.

Source

fn encode_value<T: ?Sized + Serialize>( self, data: &T, event: Option<&str>, ) -> Result<Value, ParserError>

Convert any serializable data to a generic Value to be later included as a payload in the packet.

  • Any data serialized will be wrapped in an array to match the socket.io format ([data]).
  • If the data is a tuple-like type the tuple will be expanded in the array ([...data]).
  • If provided the event name will be serialized as the first element of the array ([event, ...data]) or ([event, data]).
Source

fn decode_value<'de, T: Deserialize<'de>>( self, value: &'de mut Value, with_event: bool, ) -> Result<T, ParserError>

Convert any generic Value to a deserializable type. It should always be an array (according to the serde model).

  • If with_event is true, we expect an event str as the first element and we will skip it when deserializing data.
  • If T is a tuple-like type, all the remaining elements of the array will be deserialized ([...data]).
  • If T is not a tuple-like type, the first element of the array will be deserialized ([data]).
Source

fn decode_default<'de, T: Deserialize<'de>>( self, value: Option<&'de Value>, ) -> Result<T, ParserError>

Convert any generic Value to a type with the default serde impl without binary + event tricks. This is mainly used for connect payloads.

Source

fn encode_default<T: ?Sized + Serialize>( self, data: &T, ) -> Result<Value, ParserError>

Convert any type to a generic Value with the default serde impl without binary + event tricks. This is mainly used for connect payloads.

Source

fn read_event(self, value: &Value) -> Result<&str, ParserError>

Try to read the event name from the given payload data. The event name should be the first element of the provided array according to the serde model.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementors§