#[derive(ProtobufConvert)]
{
// Attributes available to this derive:
#[protobuf_convert]
}
Expand description
ProtobufConvert derive macro.
Attributes:
§Required
#[protobuf_convert(source = "path")]
#[derive(Clone, Debug, ProtobufConvert)]
#[protobuf_convert(source = "proto::Message")]
pub struct Message {
/// Message author id.
pub author: u32,
/// Message text.
pub text: String,
}
let msg = Message::new();
let serialized_msg = msg.to_pb();
let deserialized_msg = ProtobufConvert::from_pb(serialized_msg).unwrap();
assert_eq!(msg, deserialized_msg);
Corresponding proto file:
message Message {
// Message author id..
uint32 author = 1;
// Message text.
string text = 2;
}
This macro can also be applied to enums. In proto files enums are represented
by oneof
field. You can specify oneof
field name, default is “kind”.
Corresponding proto file must contain only this oneof field. Possible enum
variants are zero-field and one-field variants.
Another enum attribute is impl_from_trait
. If you specify it then From
and TryFrom
traits for enum variants will be generated. Note that this will not work if enum has
variants with the same field types.
#[derive(Debug, Clone, ProtobufConvert)]
#[protobuf_convert(source = "proto::Message", oneof_field = "message")]
pub enum Message {
/// Plain message.
Plain(String),
/// Encoded message.
Encoded(String),
}
Corresponding proto file:
message Message {
oneof message {
// Plain message.
string plain = 1;
// Encoded message.
string encoded = 2;
}
}
Path is the name of the corresponding protobuf generated struct.
#[protobuf_convert(source = "path", serde_pb_convert)]
Implement serde::{Serialize, Deserialize}
using structs that were generated with
protobuf.
For example, it should be used if you want json representation of your struct
to be compatible with protobuf representation (including proper nesting of fields).
For example, struct with crypto::Hash
with this
(de)serializer will be represented as
StructName {
"hash": {
"data": [1, 2, ...]
},
// ...
}
// With default (de)serializer.
StructName {
"hash": "12af..." // HEX
// ...
}