wire-framed

wire-framed is a library for encoding and decoding messages using a custom binary protocol. It prioritizes ease of use and performance.

It revolves around the [Message] type that can hold multiple frames of data (represented with the [bytes::Bytes] type), and 4 main traits: [FromFrame], [IntoFrame], [FromMessage] and [IntoMessage].

Each frame should be a self-contained piece of data that can be decoded without any context.

Usage with frames

```rust use wire_framed::prelude::*;

[derive(Debug, Encoding, Decoding, PartialEq, Eq)]

pub struct Foo { pub id: u32, pub name: String, pub description: String, pub created_at: u64, }

fn sendtosocket(_frame: Bytes) -> Result<(), std::io::Error> { Ok(()) }

fn send() -> Result<(), std::io::Error> { let foo = Foo { id: 1, name: "John".tostring(), description: "John is a legend".tostring(), created_at: 1234567890, };

let frame = foo.into_frame();
send_to_socket(frame)

}

fn recvfromsocket() -> Bytes {

Bytes::from_static(&[

0x00, 0x00, 0x00, 0x01, // id

0x00, 0x00, 0x00, 0x04, // name length

0x4a, 0x6f, 0x68, 0x6e, // name

0x00, 0x00, 0x00, 0x10, // description length

0x4a, 0x6f, 0x68, 0x6e, 0x20, 0x69, 0x73, 0x20, 0x61, 0x20, 0x6c, 0x65, 0x67, 0x65, 0x6e, 0x64, // description

0x00, 0x00, 0x00, 0x00, 0x49, 0x96, 0x02, 0xd2, // created_at

])

}

fn recv() -> Result<(), std::io::Error> { let bytes = recvfromsocket(); let foo = Foo::from_frame(bytes)?; //! // process foo

Ok(())

}

fn main() -> Result<(), std::io::Error> {

send()?;

recv()?;

#

Ok(())

}

```

Usage with messages

```rust use wire_framed::prelude::*;

[derive(Debug, Encoding, Decoding, PartialEq, Eq)]

pub struct Foo { pub id: u32, pub name: String, pub description: String, }

fn main() -> Result<(), std::io::Error> { let foo = Foo { id: 1, name: "John".tostring(), description: "John is a legend".tostring(), };

let msg = Message::builder()
    .frame(foo.into_frame())
    .frame(foo.into_frame())
    .build();

println!("Message: {:?}", msg);
Ok(())

}