-
Notifications
You must be signed in to change notification settings - Fork 15.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add an example crate with a few small tests using the codegen crate.
PiperOrigin-RevId: 678734677
- Loading branch information
1 parent
a51f98c
commit 3b62052
Showing
9 changed files
with
145 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
[package] | ||
name = "protobuf-codegen-example" | ||
version = "0.1.0" | ||
edition = "2021" | ||
|
||
[dependencies] | ||
protobuf = { path = "../protobuf" } | ||
|
||
[build-dependencies] | ||
protobuf-codegen = { path = "../protobuf_codegen" } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
use protobuf_codegen::CodeGen; | ||
use std::env; | ||
|
||
fn main() { | ||
let mut codegen = CodeGen::new(); | ||
codegen | ||
.protoc_path(env::var("PROTOC").expect("PROTOC should be set to the path to protoc")) | ||
.protoc_gen_upb_minitable_path(env::var("PROTOC_GEN_UPB_MINITABLE").expect( | ||
"PROTOC_GEN_UPB_MINITABLE should be set to the path to protoc-gen-upb_minitable", | ||
)) | ||
.inputs(["foo.proto", "bar/bar.proto"]) | ||
.include("proto"); | ||
codegen.compile().unwrap(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
edition = "2023"; | ||
|
||
package proto_example; | ||
|
||
message Bar { | ||
int32 int = 1; | ||
repeated int32 numbers = 2; | ||
string name = 3; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
edition = "2023"; | ||
|
||
package proto_example; | ||
|
||
import "bar/bar.proto"; | ||
|
||
message Foo { | ||
int32 int = 1; | ||
repeated int32 numbers = 2; | ||
string name = 3; | ||
Bar bar = 4; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
#[path = "protos/foo.u.pb.rs"] | ||
mod protos; | ||
|
||
use protobuf::proto; | ||
|
||
use protos::Foo; | ||
|
||
fn main() { | ||
let foo = proto!(Foo { name: "foo", bar: __ { name: "bar" } }); | ||
dbg!(foo); | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
|
||
#[test] | ||
fn set_strings() { | ||
let foo = proto!(Foo { name: "foo", bar: __ { name: "bar" } }); | ||
|
||
assert_eq!(foo.name(), "foo"); | ||
assert_eq!(foo.bar().name(), "bar"); | ||
} | ||
|
||
#[test] | ||
fn set_ints() { | ||
let foo = proto!(Foo { int: 42, bar: __ { numbers: [1, 2, 3] } }); | ||
|
||
assert_eq!(foo.int(), 42); | ||
let nums: Vec<_> = foo.bar().numbers().iter().collect(); | ||
assert_eq!(nums, vec![1, 2, 3]); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters