Skip to main content

MessageHandler

Struct MessageHandler 

Source
pub struct MessageHandler;
Expand description

A handler for serializing and deserializing messages.

Provides utility functions to handle message encoding and decoding in JSON and CBOR formats.

Implementations§

Source§

impl MessageHandler

Source

pub fn serialize<T: Serialize>( data: &T, format: MessageFormat, ) -> Result<Vec<u8>, String>

Serializes the given data into the specified format.

§Arguments
  • data - The data to serialize.
  • format - The format to serialize the data into (MessageFormat::Json or MessageFormat::Cbor).
§Returns

A Result containing the serialized data as a Vec<u8> on success, or an error message as a String on failure.

§Examples
use websocket_toolkit::messages::{MessageHandler, MessageFormat};

let message = "Hello, WebSocket!";
let serialized = MessageHandler::serialize(&message, MessageFormat::Json).unwrap();
assert!(!serialized.is_empty());
Source

pub fn deserialize<'a, T: Deserialize<'a>>( data: &'a [u8], format: MessageFormat, ) -> Result<Option<T>, String>

Deserializes the given byte slice into the specified type.

§Arguments
  • data - The byte slice containing the serialized data.
  • format - The format of the serialized data (MessageFormat::Json or MessageFormat::Cbor).
§Returns

A Result containing the deserialized data as an Option<T> on success, or an error message as a String on failure.

§Examples
use websocket_toolkit::messages::{MessageHandler, MessageFormat};

let serialized = b"\"Hello, WebSocket!\"";
let deserialized: Option<String> = MessageHandler::deserialize(serialized, MessageFormat::Json).unwrap();
assert_eq!(deserialized, Some("Hello, WebSocket!".to_string()));

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V