6 releases (1 stable)

1.0.0 Mar 31, 2020
1.0.0-rc.3 Mar 25, 2020
1.0.0-rc.2 Mar 13, 2020
1.0.0-rc.1 Feb 7, 2020
0.13.0-rc.2 Dec 4, 2019

#20 in #exonum

Download history 38/week @ 2024-08-23 36/week @ 2024-08-30 38/week @ 2024-09-06 48/week @ 2024-09-13 71/week @ 2024-09-20 44/week @ 2024-09-27 15/week @ 2024-10-04 30/week @ 2024-10-11 29/week @ 2024-10-18 23/week @ 2024-10-25 49/week @ 2024-11-01 10/week @ 2024-11-08 27/week @ 2024-11-15 42/week @ 2024-11-22 59/week @ 2024-11-29 69/week @ 2024-12-06

197 downloads per month
Used in 14 crates (10 directly)

Apache-2.0

32KB
497 lines

High-level Protobuf conversion library for Exonum

Travis Build Status License: Apache-2.0 rust 1.42.0+ required

exonum-proto provides a high-level interface for interacting with code generated by protoc-rust crate.

The central part of this crate is ProtobufConvert trait.

The main purpose of this trait is to allow users to create a map between their types and the types generated from .proto descriptions, while providing a mechanism for additional validation of Protobuf data.

Most of the time you do not have to implement this trait because most of the use cases are covered by #[derive(ProtobufConvert)] from the exonum-derive crate.

A typical example of such mapping with validation is manual implementation of this trait for exonum_crypto::Hash. exonum_crypto::Hash is a fixed sized array of bytes but Protobuf does not allow us to express this constraint since only dynamically sized arrays are supported.

If you would like to use Hash as a part of your Protobuf struct, you would have to write a conversion function from Protobuf proto::Hash(which is dynamically sized array of bytes) toexonum_crypto::Hash and call it every time when you want to use exonum_crypto::Hash in your application.

The provided ProtobufConvert implementation for Hash allows you to embed this field into your struct and generate ProtobufConvert for it using #[derive(ProtobufConvert)], which will validate your structure based on the validation function for Hash.

Consult the crate docs for more details.

Examples

Sample Protobuf roundtrip:

use exonum_proto::ProtobufConvert;
use bit_vec::BitVec;

let bit_vector = BitVec::from_bytes(&[0b_1010_0000, 0b_0001_0010]);
let bit_vector_pb = bit_vector.to_pb();
let deserialized_bit_vector: BitVec = ProtobufConvert::from_pb(pb_bv).unwrap();
assert_eq!(bit_vector, deserialized_bit_vector);

Usage

Include exonum-proto as a dependency in your Cargo.toml:

[dependencies]
exonum-proto = "1.0.0"

License

exonum-proto is licensed under the Apache License (Version 2.0). See LICENSE for details.

Dependencies

~5–6.5MB
~125K SLoC