vino-codec

Crates.iovino-codec
lib.rsvino-codec
version0.9.0
sourcesrc
created_at2021-08-05 20:35:04.892122
updated_at2022-03-01 18:43:27.93736
descriptionJSON, Messagepack, & Raw serde value encoding/decoding library
homepagehttps://docs.vino.dev
repositoryhttps://github.com/vinodotdev/vino-sdk
max_upload_size
id432175
size14,047
Jarrod Overson (jsoverson)

documentation

https://docs.vino.dev

README

Maintenance

The Vino Codec crate contains the serialization and deserialization functions and structures for communicating in and out of Vino Components.

JSON

Serializes to a serde_json::Value which can be printed as a JSON string.

json::serialize

use vino_codec::{json, Error};
use serde::{Serialize, Deserialize};

pub fn main() -> Result<(), Error> {

  #[derive(Serialize, Deserialize)]
  struct Point {
    x: i32,
    y: i32,
  }

  let point = Point { x: 200, y: 193 };

  let value = json::serialize(&point)?;
  println!("{:?}", value);

  assert_eq!(value, r#"{"x":200,"y":193}"#);
  Ok(())
}

json::deserialize

use vino_codec::{json, Error};
use serde::{Serialize, Deserialize};

pub fn main() -> Result<(), Error> {
  #[derive(Serialize, Deserialize, Debug, PartialEq)]
  struct Point {
    x: i32,
    y: i32,
  }

  let json = r#"{"x":200,"y":193}"#;

  let instance: Point = json::deserialize(&json)?;

  assert_eq!(instance, Point { x: 200, y: 193 });
  Ok(())
}

MessagePack

Serializes to a MessagePack [Vec].

messagepack::serialize

use vino_codec::{messagepack, Error};
use serde::{Serialize, Deserialize};

pub fn main() -> Result<(), Error> {
  #[derive(Serialize, Deserialize)]
  struct Point {
    x: i32,
    y: i32,
  }

  let point = Point { x: 200, y: 193 };

  let value = messagepack::serialize(&point)?;
  println!("{:?}", value);

  let expected: Vec<u8> = vec![130, 161, 120, 204, 200, 161, 121, 204, 193];
  assert_eq!(value, expected);
  Ok(())
}

messagepack::deserialize

use vino_codec::{messagepack, Error};
use serde::{Serialize, Deserialize};

pub fn main() -> Result<(), Error> {
  #[derive(Serialize, Deserialize, Debug, PartialEq)]
  struct Point {
    x: i32,
    y: i32,
  }

  let slice = vec![146, 204, 200, 204, 193];

  let instance: Point = messagepack::deserialize(&slice)?;

  assert_eq!(instance, Point { x: 200, y: 193 });
  Ok(())
}

Raw

The [raw] module uses [serde_value] as an intermediary format to pass around.

use vino_codec::{raw, Error};
use serde::{Serialize, Deserialize};

pub fn main() -> Result<(), Error> {
  #[derive(Serialize, Deserialize, Debug, PartialEq)]
  struct Point {
    x: i32,
    y: i32,
  }

  let point = Point { x: 200, y: 193 };

  let value = raw::serialize(&point)?;
  let instance: Point = raw::deserialize(value)?;

  assert_eq!(instance, Point { x: 200, y: 193 });
  Ok(())
}
Commit count: 111

cargo fmt