protobuf_codec

Crates.ioprotobuf_codec
lib.rsprotobuf_codec
version0.2.8
sourcesrc
created_at2018-05-12 11:45:06.978556
updated_at2019-10-06 02:02:23.732943
descriptionEncoders and decoders for Protocol Buffers based on bytecodec crate
homepagehttps://github.com/sile/protobuf_codec
repositoryhttps://github.com/sile/protobuf_codec
max_upload_size
id65034
size152,345
Takeru Ohta (sile)

documentation

README

protobuf_codec

protobuf_codec Documentation Build Status Code Coverage License: MIT

A Rust implementation of encoders and decoders for Protocol Buffers based on bytecodec crate.

Documentation

Examples

An encoder/decoder for SearchRequest message defined in the Language Guide.

extern crate bytecodec;
extern crate protobuf_codec;

use bytecodec::EncodeExt;
use bytecodec::io::{IoDecodeExt, IoEncodeExt};
use protobuf_codec::field::{Fields, FieldDecoder, FieldEncoder, MaybeDefault};
use protobuf_codec::field::num::{F1, F2, F3};
use protobuf_codec::message::{MessageDecoder, MessageEncoder};
use protobuf_codec::scalar::{Int32Decoder, Int32Encoder, StringDecoder, StringEncoder};

// syntax = "proto3";
//
// message SearchRequest {
//   string query = 1;
//   int32 page_number = 2;
//   int32 result_per_page = 3;
// }
type SearchRequestEncoder = MessageEncoder<
    Fields<(
        MaybeDefault<FieldEncoder<F1, StringEncoder>>,
        MaybeDefault<FieldEncoder<F2, Int32Encoder>>,
        MaybeDefault<FieldEncoder<F3, Int32Encoder>>,
    )>,
>;
type SearchRequestDecoder = MessageDecoder<
    Fields<(
        MaybeDefault<FieldDecoder<F1, StringDecoder>>,
        MaybeDefault<FieldDecoder<F2, Int32Decoder>>,
        MaybeDefault<FieldDecoder<F3, Int32Decoder>>,
    )>,
>;

fn main() {
    let mut buf = Vec::new();
    let mut encoder = SearchRequestEncoder::with_item(("foo".to_owned(), 3, 10)).unwrap();
    encoder.encode_all(&mut buf).unwrap();
    assert_eq!(buf, [10, 3, 102, 111, 111, 16, 3, 24, 10]);

    let mut decoder = SearchRequestDecoder::default();
    let message = decoder.decode_exact(&buf[..]).unwrap();
    assert_eq!(message, ("foo".to_owned(), 3, 10));
}

References

Commit count: 143

cargo fmt