Skip to content

Commit

Permalink
Don't include serde in default features + enable serde impls in tests
Browse files Browse the repository at this point in the history
  • Loading branch information
cynecx committed Aug 22, 2024
1 parent dddff9b commit bba5ef7
Show file tree
Hide file tree
Showing 10 changed files with 69 additions and 28 deletions.
4 changes: 2 additions & 2 deletions conformance/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ authors.workspace = true
[dependencies]
bytes = "1"
env_logger = { version = "0.11", default-features = false }
prost = { path = "../prost" }
prost-types = { path = "../prost-types", features = ["any-v2"] }
prost = { path = "../prost", features = ["serde", "serde-json"] }
prost-types = { path = "../prost-types", features = ["serde", "any-v2"] }
protobuf = { path = "../protobuf" }
tests = { path = "../tests" }
47 changes: 29 additions & 18 deletions prost-build/src/code_generator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -384,9 +384,11 @@ impl<'a> CodeGenerator<'a> {
}

fn append_serde(&mut self) {
push_indent(self.buf, self.depth);
self.buf.push_str("#[prost(serde)]");
self.buf.push('\n');
if self.config.enable_serde {
push_indent(self.buf, self.depth);
self.buf.push_str("#[prost(serde)]");
self.buf.push('\n');
}
}

fn append_enum_attributes(&mut self, fq_message_name: &str) {
Expand Down Expand Up @@ -512,12 +514,14 @@ impl<'a> CodeGenerator<'a> {
let rust_field_name = &field.rust_name();
let proto_field_name = field.descriptor.name();

if !ident::is_stable_ident_for_json(
let field_name_is_stable_for_json = ident::is_stable_ident_for_json(
proto_field_name,
IdentKind::MessageField {
field: rust_field_name,
},
) {
);

if self.config.enable_serde && !field_name_is_stable_for_json {
self.buf
.push_str(&format!(", json(proto_name = \"{}\")", proto_field_name));
}
Expand Down Expand Up @@ -583,12 +587,14 @@ impl<'a> CodeGenerator<'a> {
let rust_field_name = &field.rust_name();
let proto_field_name = field.descriptor.name();

let json_attr = if !ident::is_stable_ident_for_json(
let field_name_is_stable_for_json = ident::is_stable_ident_for_json(
proto_field_name,
IdentKind::MessageField {
field: rust_field_name,
},
) {
);

let json_attr = if self.config.enable_serde && !field_name_is_stable_for_json {
format!(", json(proto_name = \"{}\")", proto_field_name)
} else {
Default::default()
Expand Down Expand Up @@ -685,12 +691,14 @@ impl<'a> CodeGenerator<'a> {
let ty_tag = self.field_type_tag(&field.descriptor);
let ty = self.resolve_type(&field.descriptor, fq_message_name);

let json_attr = if !ident::is_stable_ident_for_json(
let variant_name_is_stable_for_json = ident::is_stable_ident_for_json(
proto_field_name,
IdentKind::OneOfVariant {
variant: rust_variant_name,
},
) {
);

let json_attr = if self.config.enable_serde && !variant_name_is_stable_for_json {
format!(", json(proto_name = \"{}\")", proto_field_name)
} else {
Default::default()
Expand Down Expand Up @@ -809,15 +817,18 @@ impl<'a> CodeGenerator<'a> {

self.append_doc(&fq_proto_enum_name, Some(variant.proto_name));

let emit_proto_names = !variant.proto_aliases.is_empty()
|| !ident::is_stable_ident_for_json(
variant.proto_name,
IdentKind::EnumVariant {
ty: &enum_name,
variant: &variant.generated_variant_name,
},
);
if emit_proto_names {
let variant_name_is_stable_for_json = ident::is_stable_ident_for_json(
variant.proto_name,
IdentKind::EnumVariant {
ty: &enum_name,
variant: &variant.generated_variant_name,
},
);

let emit_proto_names =
!variant.proto_aliases.is_empty() || !variant_name_is_stable_for_json;

if self.config.enable_serde && emit_proto_names {
self.push_indent();

let names = iter::once(variant.proto_name)
Expand Down
10 changes: 10 additions & 0 deletions prost-build/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ pub struct Config {
pub(crate) prost_path: Option<String>,
#[cfg(feature = "format")]
pub(crate) fmt: bool,
pub(crate) enable_serde: bool,
}

impl Config {
Expand Down Expand Up @@ -1126,6 +1127,14 @@ impl Config {
*buf = with_generated;
}
}

/// Configures the code generator to also emit serde compatible serialization impls.
///
/// Defaults to `false`.
pub fn enable_serde(&mut self) -> &mut Self {
self.enable_serde = true;
self
}
}

/// Write a slice as the entire contents of a file.
Expand Down Expand Up @@ -1176,6 +1185,7 @@ impl default::Default for Config {
prost_path: None,
#[cfg(feature = "format")]
fmt: true,
enable_serde: false,
}
}
}
Expand Down
8 changes: 8 additions & 0 deletions prost-build/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -359,6 +359,7 @@ mod tests {
let tempdir = tempfile::tempdir().unwrap();

Config::new()
.enable_serde()
.service_generator(Box::new(ServiceTraitGenerator))
.out_dir(tempdir.path())
.compile_protos(&["src/fixtures/smoke_test/smoke_test.proto"], &["src"])
Expand All @@ -374,6 +375,7 @@ mod tests {
let gen = MockServiceGenerator::new(Rc::clone(&state));

Config::new()
.enable_serde()
.service_generator(Box::new(gen))
.include_file("_protos.rs")
.out_dir(tempdir.path())
Expand All @@ -398,6 +400,8 @@ mod tests {
let tempdir = tempfile::tempdir().unwrap();

let mut config = Config::new();
config.enable_serde();

config
.out_dir(tempdir.path())
// Add attributes to all messages and enums
Expand Down Expand Up @@ -453,6 +457,7 @@ mod tests {
let previously_empty_proto_path = tempdir.path().join(Path::new("google.protobuf.rs"));

Config::new()
.enable_serde()
.service_generator(Box::new(gen))
.include_file(include_file)
.out_dir(tempdir.path())
Expand Down Expand Up @@ -486,6 +491,7 @@ mod tests {
let tempdir = tempfile::tempdir().unwrap();

Config::new()
.enable_serde()
.out_dir(tempdir.path())
.boxed("Container.data.foo")
.boxed("Bar.qux")
Expand Down Expand Up @@ -527,6 +533,7 @@ mod tests {
let tempdir = tempfile::tempdir().unwrap();

Config::new()
.enable_serde()
.service_generator(Box::new(gen))
.include_file(include_file)
.out_dir(tempdir.path())
Expand Down Expand Up @@ -579,6 +586,7 @@ mod tests {

let mut buf = Vec::new();
Config::new()
.enable_serde()
.default_package_filename("_.default")
.write_includes(modules.iter().collect(), &mut buf, None, &file_names)
.unwrap();
Expand Down
2 changes: 1 addition & 1 deletion prost-types/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ rust-version.workspace = true
doctest = false

[features]
default = ["std", "serde", "any-v2"]
default = ["std"]
std = ["prost/std"]
serde = ["prost/serde"]
any-v2 = ["std", "serde", "prost/serde-json", "dep:serde", "dep:erased-serde"]
Expand Down
2 changes: 1 addition & 1 deletion prost/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ rust-version.workspace = true
bench = false

[features]
default = ["derive", "std", "serde", "serde-json"]
default = ["derive", "std"]
derive = ["dep:prost-derive"]
prost-derive = ["derive"] # deprecated, please use derive feature instead
no-recursion-limit = []
Expand Down
13 changes: 8 additions & 5 deletions protobuf/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,13 @@ fn main() -> Result<()> {
}

let conformance_proto_dir = src_dir.join("conformance");
prost_build::compile_protos(
&[conformance_proto_dir.join("conformance.proto")],
&[conformance_proto_dir],
)
.unwrap();
prost_build::Config::new()
.enable_serde()
.compile_protos(
&[conformance_proto_dir.join("conformance.proto")],
&[conformance_proto_dir],
)
.unwrap();

let proto_dir = src_dir.join("src");

Expand All @@ -51,6 +53,7 @@ fn main() -> Result<()> {
// values.
prost_build::Config::new()
.btree_map(["."])
.enable_serde()
.compile_protos(
&[
proto_dir.join("google/protobuf/test_messages_proto2.proto"),
Expand Down
2 changes: 1 addition & 1 deletion tests/single-include/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ publish = false
license = "MIT"

[dependencies]
prost = { path = "../../prost" }
prost = { path = "../../prost", features = ["serde"] }

[build-dependencies]
prost-build = { path = "../../prost-build" }
2 changes: 2 additions & 0 deletions tests/single-include/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@ use prost_build::Config;

fn main() {
Config::new()
.enable_serde()
.include_file("lib.rs")
.compile_protos(&["protos/search.proto"], &["protos"])
.unwrap();

Config::new()
.enable_serde()
.out_dir("src/outdir")
.include_file("mod.rs")
.compile_protos(&["protos/outdir.proto"], &["protos"])
Expand Down
7 changes: 7 additions & 0 deletions tests/src/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ fn main() {
// compare based on the Rust PartialEq implementations is difficult, due to presence of NaN
// values.
let mut config = prost_build::Config::new();
config.enable_serde();
config.btree_map(["."]);
// Tests for custom attributes
config.type_attribute("Foo.Bar_Baz.Foo_barBaz", "#[derive(Eq, PartialOrd, Ord)]");
Expand Down Expand Up @@ -128,12 +129,14 @@ fn main() {
.unwrap();

prost_build::Config::new()
.enable_serde()
.protoc_arg("--experimental_allow_proto3_optional")
.compile_protos(&[src.join("proto3_presence.proto")], includes)
.unwrap();

{
let mut config = prost_build::Config::new();
config.enable_serde();
config.disable_comments(["."]);

config
Expand All @@ -152,6 +155,7 @@ fn main() {
std::fs::create_dir_all(&out_path).unwrap();

prost_build::Config::new()
.enable_serde()
.bytes(["."])
.out_dir(out_path)
.include_file("wellknown_include.rs")
Expand All @@ -166,6 +170,7 @@ fn main() {
.unwrap();

prost_build::Config::new()
.enable_serde()
.enable_type_names()
.type_name_domain([".type_names.Foo"], "tests")
.compile_protos(&[src.join("type_names.proto")], includes)
Expand All @@ -184,6 +189,7 @@ fn main() {
fs::create_dir_all(&no_root_packages).expect("failed to create prefix directory");
let mut no_root_packages_config = prost_build::Config::new();
no_root_packages_config
.enable_serde()
.out_dir(&no_root_packages)
.default_package_filename("__.default")
.include_file("__.include.rs")
Expand All @@ -199,6 +205,7 @@ fn main() {
fs::create_dir_all(&no_root_packages_with_default).expect("failed to create prefix directory");
let mut no_root_packages_config = prost_build::Config::new();
no_root_packages_config
.enable_serde()
.out_dir(&no_root_packages_with_default)
.compile_protos(
&[src.join("no_root_packages/widget_factory.proto")],
Expand Down

0 comments on commit bba5ef7

Please sign in to comment.