Skip to content

Commit

Permalink
Link with serde when with-serde feature is enabled
Browse files Browse the repository at this point in the history
  • Loading branch information
bacek authored and stepancheg committed Sep 30, 2018
1 parent 98ddf79 commit 98b30f0
Show file tree
Hide file tree
Showing 10 changed files with 36 additions and 18 deletions.
7 changes: 4 additions & 3 deletions protobuf-codegen-pure-test/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ bench = false
default-features = []
proto3 = []
with-bytes = ["bytes", "protobuf/with-bytes", "protobuf-test-common/with-bytes"]
with-serde = ["serde", "serde_derive", "serde_json", "protobuf/with-serde", "protobuf-test-common/with-serde"]

[build-dependencies]
protobuf-codegen-pure = { path = "../protobuf-codegen-pure" }
Expand All @@ -23,9 +24,9 @@ env_logger = "0.5.*"
[dependencies]
protobuf-test-common = { path = "../protobuf-test-common" }
protobuf-serde = { path = "../protobuf-serde" }
serde = "1.0"
serde_derive = "1.0"
serde_json = "1.0"
serde = { version = "1.0", optional = true }
serde_derive = { version = "1.0", optional = true }
serde_json = { version = "1.0", optional = true }
bytes = { version = "0.4", optional = true }

[dependencies.protobuf]
Expand Down
5 changes: 4 additions & 1 deletion protobuf-codegen-pure-test/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,13 @@ extern crate protobuf_serde;
#[cfg(feature = "with-bytes")]
extern crate bytes;

#[cfg(feature = "with-serde")]
extern crate serde;
extern crate serde_json;
#[cfg(feature = "with-serde")]
#[macro_use]
extern crate serde_derive;
#[cfg(feature = "with-serde")]
extern crate serde_json;

mod v2;
mod v3;
Expand Down
2 changes: 1 addition & 1 deletion protobuf-codegen/src/enums.rs
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ impl<'a> EnumGen<'a> {
);
}
if self.customize.serde_derive.unwrap_or(false) {
derive.extend(&["Serialize", "Deserialize"]);
w.write_line("#[cfg_attr(feature = \"with-serde\", derive(Serialize, Deserialize))]");
}
w.derive(&derive);
let ref type_name = self.type_name;
Expand Down
8 changes: 4 additions & 4 deletions protobuf-codegen/src/message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -364,10 +364,10 @@ impl<'a> MessageGen<'a> {
if self.lite_runtime {
derive.push("Debug");
}
w.derive(&derive);
if self.serde_derive_enabled() {
derive.extend(&["Serialize", "Deserialize"]);
w.write_line("#[cfg_attr(feature = \"with-serde\", derive(Serialize, Deserialize))]");
}
w.derive(&derive);
w.pub_struct(&self.type_name, |w| {
if !self.fields_except_oneof().is_empty() {
w.comment("message fields");
Expand All @@ -388,11 +388,11 @@ impl<'a> MessageGen<'a> {
w.comment("special fields");

if self.serde_derive_enabled() {
w.write_line("#[serde(skip)]");
w.write_line("#[cfg_attr(feature = \"with-serde\", serde(skip))]");
}
w.pub_field_decl("unknown_fields", "::protobuf::UnknownFields");
if self.serde_derive_enabled() {
w.write_line("#[serde(skip)]");
w.write_line("#[cfg_attr(feature = \"with-serde\", serde(skip))]");
}
w.field_decl("cached_size", "::protobuf::CachedSize");
});
Expand Down
2 changes: 1 addition & 1 deletion protobuf-codegen/src/oneof.rs
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ impl<'a> OneofGen<'a> {
derive.push("Debug");
}
if self.customize.serde_derive.unwrap_or(false) {
derive.extend(&["Serialize", "Deserialize"]);
w.write_line("#[cfg_attr(feature = \"with-serde\", derive(Serialize, Deserialize))]");
}
w.derive(&derive);
w.pub_enum(&self.type_name.to_string(), |w| {
Expand Down
7 changes: 4 additions & 3 deletions protobuf-test-common/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,16 @@ bench = false
default-features = []
proto3 = []
with-bytes = ["bytes", "protobuf/with-bytes"]
with-serde = ["serde", "protobuf/with-serde"]

[dependencies]
glob = "0.2"
log = "0.*"
env_logger = "0.5.*"
tempfile = "3.0"
serde = "1.0"
serde_derive = "1.0"
serde_json = "1.0"
serde = { version = "1.0", optional = true }
serde_derive = { version = "1.0", optional = true }
serde_json = { version = "1.0", optional = true }
bytes = { version = "0.4", optional = true }

[dependencies.protobuf]
Expand Down
7 changes: 4 additions & 3 deletions protobuf-test/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ bench = false
default-features = []
proto3 = []
with-bytes = ["bytes", "protobuf/with-bytes", "protobuf-test-common/with-bytes"]
with-serde = ["serde", "serde_derive", "serde_json", "protobuf/with-serde", "protobuf-test-common/with-serde"]

[build-dependencies]
protoc = { path = "../protoc" }
Expand All @@ -24,9 +25,9 @@ env_logger = "0.5.*"
[dependencies]
protobuf-test-common = { path = "../protobuf-test-common" }
protobuf-serde = { path = "../protobuf-serde" }
serde = "1.0"
serde_derive = "1.0"
serde_json = "1.0"
serde = { version = "1.0", optional = true }
serde_derive = { version = "1.0", optional = true }
serde_json = { version = "1.0", optional = true }
bytes = { version = "0.4", optional = true }

[dependencies.protobuf]
Expand Down
6 changes: 5 additions & 1 deletion protobuf-test/src/common/v2/test_serde_derive.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
use super::test_serde_derive_pb::*;
#![cfg(feature = "with-serde")]

use serde_json;

use super::test_serde_derive_pb::*;

use std::collections::HashMap;

#[test]
Expand Down
5 changes: 4 additions & 1 deletion protobuf-test/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,13 @@ extern crate protobuf_serde;
#[cfg(feature = "with-bytes")]
extern crate bytes;

#[cfg(feature = "with-serde")]
extern crate serde;
extern crate serde_json;
#[cfg(feature = "with-serde")]
#[macro_use]
extern crate serde_derive;
#[cfg(feature = "with-serde")]
extern crate serde_json;

mod v2;

Expand Down
5 changes: 5 additions & 0 deletions protobuf/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,12 @@

#[cfg(feature = "bytes")]
extern crate bytes;
#[cfg(feature = "with-serde")]
extern crate serde;
#[macro_use]
#[cfg(feature = "with-serde")]

extern crate serde_derive;
pub use unknown::UnknownFields;
pub use unknown::UnknownFieldsIter;
pub use unknown::UnknownValue;
Expand Down

0 comments on commit 98b30f0

Please sign in to comment.