Skip to content

Commit 90cfea0

Browse files
authored
move rust proto generation into protos (#191)
1 parent 9a53092 commit 90cfea0

File tree

11 files changed

+86
-53
lines changed

11 files changed

+86
-53
lines changed

Cargo.lock

+23-5
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

+11
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,21 @@ resolver = "2"
33

44
members = [
55
"rust/helloworld_tonic",
6+
"protos/example_service"
67
]
78

89
[workspace.package]
910
edition = "2021"
1011
rust-version = "1.79.0"
1112
readme = "README.md"
1213

14+
15+
[workspace.dependencies]
16+
example_service = { path = "protos/example_service" }
17+
prost = { version = "0.12.6" }
18+
prost-types = { version = "0.12.6", default-features = false }
19+
tonic = { version = "0.11.0", features = ["transport"] }
20+
tonic-build = "0.11.0"
21+
tokio = { version = "1.38", default-features = false, features = ["macros", "net", "rt-multi-thread", "signal"] }
22+
protoc-gen-tonic = "0.4.0"
23+
protoc-gen-prost = "0.3.1"

MODULE.bazel

+1
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ crate.from_cargo(
4444
cargo_lockfile = "//:Cargo.lock",
4545
manifests = [
4646
"//:Cargo.toml",
47+
"//protos/example_service:Cargo.toml",
4748
"//rust/helloworld_tonic:Cargo.toml",
4849
],
4950
)

protos/example_service/Cargo.toml

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
[package]
2+
name = "example_service"
3+
version = "0.1.0"
4+
edition.workspace = true
5+
rust-version.workspace = true
6+
7+
8+
[lib]
9+
name = "proto_bindings"
10+
path = "lib.rs"
11+
12+
13+
[dependencies]
14+
tonic = { workspace = true }
15+
prost = { workspace = true }
16+
prost-types = { workspace = true }
17+
tonic-build = { workspace = true }
18+
protoc-gen-prost = { workspace = true }
19+
protoc-gen-tonic = { workspace = true }
20+
21+
[build-dependencies]
22+
tonic-build = { workspace = true }

protos/example_service/build.rs

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
fn main() -> Result<(), Box<dyn std::error::Error>> {
2+
tonic_build::compile_protos("helloworld.proto")?;
3+
Ok(())
4+
}

protos/example_service/lib.rs

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
pub mod hello_world {
2+
tonic::include_proto!("example_service");
3+
}

rust/helloworld_tonic/BUILD.bazel

+15-14
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,30 @@
11
load("@crates//:defs.bzl", "all_crate_deps")
2-
load("@rules_rust//cargo:defs.bzl", "cargo_build_script")
2+
3+
# load("@rules_rust//cargo:defs.bzl", "cargo_build_script")
34
load("@rules_rust//rust:defs.bzl", "rust_binary")
45

5-
cargo_build_script(
6-
name = "build_script",
7-
srcs = ["build.rs"],
8-
build_script_env = {
9-
"PROTO_PATH": "$(execpath //protos/example_service:proto_files)",
10-
},
11-
data = ["//protos/example_service:proto_files"],
12-
deps = ["@crates//:tonic-build"],
13-
)
6+
#cargo_build_script(
7+
# name = "build_script",
8+
# srcs = ["build.rs"],
9+
# build_script_env = {
10+
# "PROTO_PATH": "$(execpath //protos/example_service:proto_files)",
11+
# },
12+
# data = ["//protos/example_service:proto_files"],
13+
# deps = ["@crates//:tonic-build"],
14+
#)
1415

1516
rust_binary(
1617
name = "helloworld_tonic_server",
1718
srcs = ["src/server.rs"],
1819
crate_name = "helloworld_tonic",
19-
deps = all_crate_deps() + [":build_script"],
20-
# deps = all_crate_deps() + ["//protos/example_service:hello_proto_rust"],
20+
# deps = all_crate_deps() + [":build_script"],
21+
deps = all_crate_deps() + ["//protos/example_service:hello_proto_rust"],
2122
)
2223

2324
rust_binary(
2425
name = "helloworld_tonic_client",
2526
srcs = ["src/client.rs"],
2627
crate_name = "helloworld_tonic",
27-
deps = all_crate_deps() + [":build_script"],
28-
# deps = all_crate_deps() + ["//protos/example_service:hello_proto_rust"],
28+
# deps = all_crate_deps() + [":build_script"],
29+
deps = all_crate_deps() + ["//protos/example_service:hello_proto_rust"],
2930
)

rust/helloworld_tonic/Cargo.toml

+3-13
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ name = "helloworld_tonic"
33
version = "0.1.0"
44
edition.workspace = true
55
rust-version.workspace = true
6-
readme.workspace = true
76

87
[[bin]] # Bin to run the HelloWorld gRPC server
98
name = "helloworld_server"
@@ -14,15 +13,6 @@ name = "helloworld_client"
1413
path = "src/client.rs"
1514

1615
[dependencies]
17-
tonic = "0.11"
18-
prost = "0.12"
19-
tokio = { version = "1.0", features = ["macros", "rt-multi-thread"] }
20-
21-
[dev-dependencies]
22-
tonic-build = "0.11"
23-
prost-types = { version = "0.12.6", default-features = false }
24-
protoc-gen-prost ="0.3.1"
25-
protoc-gen-tonic ="0.4.0"
26-
27-
[build-dependencies]
28-
tonic-build = "0.11"
16+
example_service = { workspace = true }
17+
tonic = { workspace = true }
18+
tokio = { workspace = true }

rust/helloworld_tonic/build.rs

-9
This file was deleted.

rust/helloworld_tonic/src/client.rs

+2-6
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,5 @@
1-
use hello_world::greeter_client::GreeterClient;
2-
use hello_world::HelloRequest;
3-
4-
pub mod hello_world {
5-
tonic::include_proto!("example_service");
6-
}
1+
use hello_proto::example_service::greeter_client::GreeterClient;
2+
use hello_proto::example_service::HelloRequest;
73

84
#[tokio::main]
95
async fn main() -> Result<(), Box<dyn std::error::Error>> {

rust/helloworld_tonic/src/server.rs

+2-6
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,7 @@
11
use tonic::{transport::Server, Request, Response, Status};
22

3-
use hello_world::greeter_server::{Greeter, GreeterServer};
4-
use hello_world::{HelloReply, HelloRequest};
5-
6-
pub mod hello_world {
7-
tonic::include_proto!("example_service"); // The string specified here must match the proto package name
8-
}
3+
use hello_proto::example_service::greeter_server::{Greeter, GreeterServer};
4+
use hello_proto::example_service::{HelloReply, HelloRequest};
95

106
#[derive(Debug, Default)]
117
pub struct MyGreeter {}

0 commit comments

Comments
 (0)