Crates.io | protobuf_codec |
lib.rs | protobuf_codec |
version | 0.2.8 |
source | src |
created_at | 2018-05-12 11:45:06.978556 |
updated_at | 2019-10-06 02:02:23.732943 |
description | Encoders and decoders for Protocol Buffers based on bytecodec crate |
homepage | https://github.com/sile/protobuf_codec |
repository | https://github.com/sile/protobuf_codec |
max_upload_size | |
id | 65034 |
size | 152,345 |
A Rust implementation of encoders and decoders for Protocol Buffers based on bytecodec crate.
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));
}