-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add basic example and fix Getting Started section of README
- Loading branch information
Showing
7 changed files
with
137 additions
and
28 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,12 @@ | ||
[package] | ||
name = "basic" | ||
version = "0.1.0" | ||
edition = "2021" | ||
|
||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html | ||
|
||
[dependencies] | ||
micropb = { version = "0.1", path = "../../micropb", features = ["container-heapless"] } | ||
|
||
[build-dependencies] | ||
micropb-gen = { version = "0.1", path = "../../micropb-gen" } |
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 @@ | ||
fn main() { | ||
let mut gen = micropb_gen::Generator::new(); | ||
// Compile example.proto into a Rust module | ||
gen.compile_protos( | ||
&["example.proto"], | ||
std::env::var("OUT_DIR").unwrap() + "/example.rs", | ||
) | ||
.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,17 @@ | ||
syntax = "proto3"; | ||
|
||
message Example { | ||
int32 f_int32 = 1; | ||
int64 f_int64 = 2; | ||
uint32 f_uint32 = 3; | ||
uint64 f_uint64 = 4; | ||
sint32 f_sint32 = 5; | ||
sint64 f_sint64 = 6; | ||
bool f_bool = 7; | ||
fixed32 f_fixed32 = 8; | ||
fixed64 f_fixed64 = 9; | ||
sfixed32 f_sfixed32 = 10; | ||
sfixed64 f_sfixed64 = 11; | ||
float f_float = 12; | ||
double f_double = 13; | ||
} |
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,36 @@ | ||
use micropb::{MessageDecode, MessageEncode, PbDecoder, PbEncoder}; | ||
|
||
mod example { | ||
#![allow(clippy::all)] | ||
#![allow(nonstandard_style, unused, irrefutable_let_patterns)] | ||
// Let's assume that Example is the only message define in the .proto file that has been | ||
// converted into a Rust struct | ||
include!(concat!(env!("OUT_DIR"), "/example.rs")); | ||
} | ||
|
||
fn main() { | ||
let example = example::Example { | ||
f_int32: 12, | ||
f_bool: true, | ||
f_float: 0.234, | ||
..Default::default() | ||
}; | ||
|
||
// Use heapless::Vec as the output stream and build an encoder around it | ||
let mut encoder = PbEncoder::new(micropb::heapless::Vec::<u8, 32>::new()); | ||
|
||
// Compute the size of the `Example` on the wire | ||
let _size = example.compute_size(); | ||
// Encode the `Example` to the data stream | ||
example.encode(&mut encoder).expect("Vec over capacity"); | ||
|
||
let data = encoder.into_writer(); | ||
// Construct new decoder from byte slice | ||
let mut decoder = PbDecoder::new(data.as_slice()); | ||
|
||
// Decode a new instance of `Example` into a new struct | ||
let mut new = example::Example::default(); | ||
new.decode(&mut decoder, data.len()) | ||
.expect("decoding failed"); | ||
assert_eq!(example, new); | ||
} |
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