Skip to content

Commit

Permalink
Add new end-to-end example
Browse files Browse the repository at this point in the history
  • Loading branch information
jgarzik committed Feb 18, 2024
1 parent 0d2dbd8 commit 7f71d6a
Show file tree
Hide file tree
Showing 6 changed files with 125 additions and 0 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ members = [
"protobuf-codegen",
"protobuf-examples/customize-serde",
"protobuf-examples/dynamic",
"protobuf-examples/end-to-end",
"protobuf-examples/pure-vs-protoc",
"protobuf-examples/vs-prost",
"protobuf-examples/issue-614",
Expand Down
13 changes: 13 additions & 0 deletions protobuf-examples/end-to-end/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
[package]
name = "protobuf-examples-end-to-end"
version = "0.0.1"
authors = ["Jeff Garzik"]
edition = "2021"
publish = false

[dependencies]
protobuf = { path = "../../protobuf" }

[build-dependencies]
protobuf = { path = "../../protobuf" }
protobuf-codegen = { path = "../../protobuf-codegen" }
38 changes: 38 additions & 0 deletions protobuf-examples/end-to-end/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# End-to-End Example

A fork-and-go example of using protobufs.

## Motivation

Simple and obvious examples for using `rust-protobuf` module were
lacking, so this was created.

## Dependencies

protoc (Protobuf Compiler)

[Installation instructions](https://grpc.io/docs/protoc-installation)

### Installing Protoc on Ubuntu (and similar)

`sudo apt install protobuf-compiler`

## Look here

Key files to read are:

* src/protos/example.proto
* src/main.rs
* build.rs

## Using

Standard rust package:
```
$ cargo build
$ cargo run
```

## Contributions

Contributions are welcome. File an issue or PR.
7 changes: 7 additions & 0 deletions protobuf-examples/end-to-end/build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
fn main() {
protobuf_codegen::Codegen::new()
.cargo_out_dir("protos")
.include("src")
.input("src/protos/example.proto")
.run_from_script();
}
46 changes: 46 additions & 0 deletions protobuf-examples/end-to-end/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
use protobuf::{EnumOrUnknown, Message};

include!(concat!(env!("OUT_DIR"), "/protos/mod.rs"));

use example::{get_response, GetRequest, GetResponse};

fn main() {
// Encode example request
let mut out_msg = GetRequest::new();
out_msg.name = "John Smith".to_string();
out_msg.age = 25;
out_msg.features.push("one".to_string());
out_msg.features.push("two".to_string());
println!("Message request:\nout_msg {:#?}", out_msg);

let out_bytes: Vec<u8> = out_msg.write_to_bytes().unwrap();
println!("Message request in bytes:\nout_bytes {:?}", out_bytes);

// Decode example request
let in_msg = GetRequest::parse_from_bytes(&out_bytes).unwrap();

assert_eq!(in_msg.name, out_msg.name);
assert_eq!(in_msg.age, out_msg.age);
assert_eq!(in_msg.features, out_msg.features);

//////////////////////////////////

// Encode example response
let mut out_resp = GetResponse::new();
out_resp.status = EnumOrUnknown::new(get_response::Status::OK);
out_resp.address = "1243 main street".to_string();
out_resp.city = "anytown".to_string();
out_resp.zipcode = 54321;
println!("\nMessage response:\nout_msg {:#?}", out_resp);

let out_bytes: Vec<u8> = out_resp.write_to_bytes().unwrap();
println!("Message response in bytes:\nout_bytes {:?}", out_bytes);

// Decode example response
let in_resp = GetResponse::parse_from_bytes(&out_bytes).unwrap();

assert_eq!(in_resp.status, out_resp.status);
assert_eq!(in_resp.address, out_resp.address);
assert_eq!(in_resp.city, out_resp.city);
assert_eq!(in_resp.zipcode, out_resp.zipcode);
}
20 changes: 20 additions & 0 deletions protobuf-examples/end-to-end/src/protos/example.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
syntax = "proto3";

message GetRequest {
string name = 1;
int32 age = 2;
repeated string features = 3;
}

message GetResponse {
enum Status {
OK = 0;
ERR = 1;
NOT_FOUND = 2;
}
Status status = 1;
string address = 2;
string city = 3;
int32 zipcode = 4;
}

0 comments on commit 7f71d6a

Please sign in to comment.