From 1aa56fec3d2db80d5d21cff6dbc74e3735fdc3bc Mon Sep 17 00:00:00 2001 From: Protobuf Team Bot Date: Tue, 10 Dec 2024 08:21:50 -0800 Subject: [PATCH] Internal change PiperOrigin-RevId: 704718010 --- .../protobuf_codegen/src/lib.rs | 12 +++++++++++- .../protobuf_example/proto/foo.proto | 4 ++-- .../protobuf_example/src/main.rs | 19 ++++++++----------- 3 files changed, 21 insertions(+), 14 deletions(-) diff --git a/rust/release_crates/protobuf_codegen/src/lib.rs b/rust/release_crates/protobuf_codegen/src/lib.rs index 307a9c3e4bd6..25f19417583c 100644 --- a/rust/release_crates/protobuf_codegen/src/lib.rs +++ b/rust/release_crates/protobuf_codegen/src/lib.rs @@ -18,7 +18,7 @@ impl CodeGen { pub fn new() -> Self { Self { inputs: Vec::new(), - output_dir: std::env::current_dir().unwrap().join("src").join("protobuf_generated"), + output_dir: Path::join(std::env::var("OUT_DIR"), "protobuf_generated"), protoc_path: None, protoc_gen_upb_minitable_path: None, includes: Vec::new(), @@ -111,6 +111,16 @@ impl CodeGen { println!("{}", std::str::from_utf8(&output.stdout).unwrap()); eprintln!("{}", std::str::from_utf8(&output.stderr).unwrap()); assert!(output.status.success()); + + std::fs::write( + output_dir.join("mod.rs"), + r#" + #[path = "foo.u.pb.rs"] + mod foo; + "#, + ) + .map_err(|_| format!("failed to write mod.rs"))?; + self.compile_only() } diff --git a/rust/release_crates/protobuf_example/proto/foo.proto b/rust/release_crates/protobuf_example/proto/foo.proto index 92bc3952f9a9..bb9a3da8029b 100644 --- a/rust/release_crates/protobuf_example/proto/foo.proto +++ b/rust/release_crates/protobuf_example/proto/foo.proto @@ -2,11 +2,11 @@ edition = "2023"; package proto_example; -import "bar/bar.proto"; +// import "bar/bar.proto"; message Foo { int32 int = 1; repeated int32 numbers = 2; string name = 3; - Bar bar = 4; + // Bar bar = 4; } diff --git a/rust/release_crates/protobuf_example/src/main.rs b/rust/release_crates/protobuf_example/src/main.rs index 5010dddf5fab..a2f99dbcb27d 100644 --- a/rust/release_crates/protobuf_example/src/main.rs +++ b/rust/release_crates/protobuf_example/src/main.rs @@ -1,12 +1,14 @@ -#[path = "protobuf_generated/foo.u.pb.rs"] -mod protos; +// #[path = "protobuf_generated/foo.u.pb.rs"] +// mod protos; + +include!(concat!(env!("OUT_DIR"), "/protobuf_generated/mod.rs")); use protobuf::proto; -use protos::Foo; +use foo::Foo; fn main() { - let foo = proto!(Foo { name: "foo", bar: __ { name: "bar" } }); + let foo = proto!(Foo { name: "foo" }); dbg!(foo); } @@ -16,18 +18,13 @@ mod tests { #[test] // allow_core_test fn set_strings() { - let foo = proto!(Foo { name: "foo", bar: __ { name: "bar" } }); - + let foo = proto!(Foo { name: "foo" }); assert_eq!(foo.name(), "foo"); - assert_eq!(foo.bar().name(), "bar"); } #[test] // allow_core_test fn set_ints() { - let foo = proto!(Foo { int: 42, bar: __ { numbers: [1, 2, 3] } }); - + let foo = proto!(Foo { int: 42 }); assert_eq!(foo.int(), 42); - let nums: Vec<_> = foo.bar().numbers().iter().collect(); - assert_eq!(nums, vec![1, 2, 3]); } }