Skip to content

Commit

Permalink
Internal change
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 704718010
  • Loading branch information
protobuf-github-bot authored and copybara-github committed Dec 10, 2024
1 parent 874e03b commit 1aa56fe
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 14 deletions.
12 changes: 11 additions & 1 deletion rust/release_crates/protobuf_codegen/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down Expand Up @@ -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()
}

Expand Down
4 changes: 2 additions & 2 deletions rust/release_crates/protobuf_example/proto/foo.proto
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
19 changes: 8 additions & 11 deletions rust/release_crates/protobuf_example/src/main.rs
Original file line number Diff line number Diff line change
@@ -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);
}

Expand All @@ -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]);
}
}

0 comments on commit 1aa56fe

Please sign in to comment.