-
Notifications
You must be signed in to change notification settings - Fork 245
Extract ProtobufConvert derive to separate crate #1501
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Extract ProtobufConvert derive to separate crate #1501
Conversation
Co-Authored-By: Oleksandr Anyshchenko <oleksandr.anyshchenko@xdev.re>
Co-Authored-By: Igor Aleksanov <popzxc@yandex.ru>
Co-Authored-By: Oleksandr Anyshchenko <oleksandr.anyshchenko@xdev.re>
…tract_proto_sep_crate
Co-Authored-By: Oleksandr Anyshchenko <oleksandr.anyshchenko@xdev.re>
exonum/src/proto/mod.rs
Outdated
@@ -65,8 +65,7 @@ impl ProtobufConvert for ValidatorId { | |||
fn from_pb(pb: Self::ProtoStruct) -> Result<Self, Error> { | |||
ensure!( | |||
pb <= u32::from(u16::max_value()), | |||
"{} is out of range for valid ValidatorId", | |||
pb | |||
"u32 is out of range for valid ValidatorId" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hm?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Merge artifact, sorry
Codecov Report
@@ Coverage Diff @@
## dynamic_services #1501 +/- ##
====================================================
- Coverage 93.4% 93.39% -0.01%
====================================================
Files 198 198
Lines 28381 28408 +27
====================================================
+ Hits 26510 26533 +23
- Misses 1871 1875 +4
Continue to review full report at Codecov.
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So we can replace
[exonum(pb = "")]
by the [protobuf_convert(source = "")]
or even [protobuf_convert(source = foo::path)]
@@ -44,7 +45,7 @@ pub enum Error { | |||
} | |||
|
|||
/// Transfer `amount` of the currency from one wallet to another. | |||
#[derive(Clone, Debug, ProtobufConvert)] | |||
#[derive(Clone, Debug, ProtobufConvert, BinaryValue, ObjectHash)] | |||
#[exonum(pb = "proto::Transfer", serde_pb_convert)] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we should avoid of using exonum
for attribute name here, and also is there any need to have serde_pb_convert
attribute that looks very strange and unclear.
exonum/src/lib.rs
Outdated
@@ -55,6 +57,7 @@ extern crate log; | |||
extern crate serde_derive; | |||
#[macro_use] | |||
extern crate serde_json; | |||
extern crate exonum_proto; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Needless ?
CHANGELOG.md
Outdated
|
||
#### exonum-proto-derive | ||
|
||
- Introduced new crate `exonum-proto-derive`. Derive macro `ProtobufConvert` is |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
- Introduced new crate `exonum-proto-derive`. Derive macro `ProtobufConvert` is | |
- Introduced a new crate `exonum-proto-derive`. Derive macro `ProtobufConvert` is |
@@ -20,56 +20,58 @@ | |||
|
|||
extern crate proc_macro; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Needless ?
@@ -473,14 +429,13 @@ impl ToTokens for ProtobufConvert { | |||
}; | |||
|
|||
let expanded = quote! { | |||
mod #mod_name { | |||
mod #mod_name { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why this padding was added?
CHANGELOG.md
Outdated
@@ -39,8 +39,17 @@ The project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html) | |||
|
|||
#### exonum-proto | |||
|
|||
- Introduced new crate `exonum-proto`. Trait `ProtobufConvert` is moved to | |||
`exonum-proto` crate. (#1496) | |||
- Introduced a new crate `exonum-proto`. Trait `ProtobufConvert` changed to is moved |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
typo
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Comments as of cb81870.
/// 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). |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit / off-topic: it's not clear what "compatible" means in this context. E.g., bytes
should be base64-encoded according to Protobuf 3 spec, but the example below shows that it is serialized as an array.
components/proto-derive/src/lib.rs
Outdated
/// (de)serializer will be represented as | ||
/// StructName { | ||
/// "hash": { | ||
/// data: [1, 2, ...] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit: "data"
should be quoted?
components/proto-derive/src/lib.rs
Outdated
|
||
#[derive(Debug, FromMeta, PartialEq, Eq)] | ||
#[darling(default)] | ||
struct CratePath(syn::Path); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this struct
necessary?
components/proto-derive/src/lib.rs
Outdated
/// // ... | ||
/// } | ||
/// ``` | ||
#[proc_macro_attribute] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Am I correct that protobuf_convert
is not a derive macro? Why? It derives a specific trait, so a derive macro would be more appropriate. IMO, a proc macro attribute implies a deeper level of transform of the attached TokenStream
.
@@ -20,6 +20,7 @@ exonum-derive = { version = "0.12.0", path = "../../../components/derive" } | |||
exonum-merkledb = { version = "0.12.0", path = "../../../components/merkledb" } | |||
exonum-crypto = { version = "0.12.0", path = "../../../components/crypto"} | |||
exonum-proto = { version = "0.12.0", path = "../../../components/proto" } | |||
exonum-proto-derive = { version = "0.12.0", path = "../../../components/proto-derive"} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could it be possible to bundle exonum-proto-derive
with exonum-proto
as in structopt
and recent versions of serde
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Bumping the request. This is not a serious concern; still, it improves UX.
components/proto-derive/src/lib.rs
Outdated
use quote::ToTokens; | ||
use syn::{Attribute, NestedMeta}; | ||
|
||
/// ProtobufConvert attribute macros. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit: why plural (macros)? Also, an example or two would be great, especially for enum
s.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks mergeable as of ee261f0.
components/proto-derive/src/lib.rs
Outdated
/// #[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize, ProtobufConvert)] | ||
/// #[protobuf_convert(source = "schema::runtime::ConfigChange", oneof_field = "message")] | ||
/// pub enum ConfigChange { | ||
/// /// New consensus config. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit: bogus indentation (3 instead of 4).
components/proto-derive/src/lib.rs
Outdated
/// Corresponding proto file: | ||
/// ```text | ||
/// message Wallet { | ||
/// // Public key of the wallet owner. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit: bogus indentation (1).
components/proto-derive/src/lib.rs
Outdated
/// ``` | ||
/// | ||
/// Corresponding proto file: | ||
/// ```test |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
text
? Also, maybe use proto
for syntax highlighting?
components/proto-derive/src/lib.rs
Outdated
/// Corresponding proto file: | ||
/// ```test | ||
/// message ConfigChange { | ||
/// oneof message { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit: Bogus indentation (1).
/// ``` | ||
/// | ||
/// 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 "message". |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do I understand correctly that:
- The protobuf message must contain only this
oneof
field group (additional fields are not allowed)? - The Rust enum should have variants with a single unnamed field? (Or are zero-field variants allowed too?)
It could be worth specifying these details.
@@ -20,6 +20,7 @@ exonum-derive = { version = "0.12.0", path = "../../../components/derive" } | |||
exonum-merkledb = { version = "0.12.0", path = "../../../components/merkledb" } | |||
exonum-crypto = { version = "0.12.0", path = "../../../components/crypto"} | |||
exonum-proto = { version = "0.12.0", path = "../../../components/proto" } | |||
exonum-proto-derive = { version = "0.12.0", path = "../../../components/proto-derive"} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Bumping the request. This is not a serious concern; still, it improves UX.
ProtobufConvert
is moved to separate crate.BinaryValue
andObjectHash
toexonum-derive
.