1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
//! The `pbf` Rust crate provides functionalities to read and write Protocol Buffers (protobuf) messages.
//! This crate is a 0 dependency package that uses `no_std` and is intended to be used in
//! embedded systems and WASM applications. The crate is designed to be small and efficient,
//! with the cost of some more modern features. It is up to the user to create the necessary
//! data structures and implement the `ProtoRead` and `ProtoWrite` traits in order to use it
//! more effectively beyond base cases.
//!
//! ## Usage
//!
//! ```rust
//! use pbf::{ProtoRead, ProtoWrite, Protobuf, Field, Type};
//!
//! #[derive(Default)]
//! struct TestMessage {
//! a: i32,
//! b: String,
//! }
//! impl TestMessage {
//! fn new(a: i32, b: &str) -> Self {
//! TestMessage { a, b: b.to_owned() }
//! }
//! }
//! impl ProtoWrite for TestMessage {
//! fn write(&self, pb: &mut Protobuf) {
//! pb.write_varint_field::<u64>(1, self.a as u64);
//! pb.write_string_field(2, &self.b);
//! }
//! }
//! impl ProtoRead for TestMessage {
//! fn read(&mut self, tag: u64, pb: &mut Protobuf) {
//! println!("tag: {}", tag);
//! match tag {
//! 1 => self.a = pb.read_varint::<i32>(),
//! 2 => self.b = pb.read_string(),
//! _ => panic!("Invalid tag"),
//! }
//! }
//! }
//!
//! let mut pb = Protobuf::new();
//! let msg = TestMessage::new(1, "hello");
//! pb.write_fields(&msg);
//!
//! let bytes = pb.take();
//! let mut pb = Protobuf::from_input(bytes);
//!
//! let mut msg = TestMessage::default();
//! pb.read_fields(&mut msg, None);
//! assert_eq!(msg.a, 1);
//! assert_eq!(msg.b, "hello");
//! ```
//!
//! If you are using the `derive` feature, you can derive the `ProtoRead` and `ProtoWrite` traits for your struct.
//!
//! ```rust
//! use pbf::{ProtoRead, ProtoWrite, Protobuf, Field, Type};
//!
//! #[derive(ProtoRead, ProtoWrite, Default)]
//! struct TestMessage {
//! a: i32,
//! b: String,
//! }
//! ```
//!
//! `ProtoRead` and `ProtoWrite` trait derives support 5 attributes:
//!
//! - `pbf(tag = 1)` -> Set the tag # of the field.
//! - `pbf(fixed)` -> Set the type to `Fixed` (for 32-bit and 64-bit numbers).
//! - `pbf(signed)` -> Set the type to `Signed` (to handle protobuf "sint" values).
//! - `pbf(nested)` -> If a sub structure is present, set the type to `nested` so it can be properly derived.
//! - `pbf(ignore)` -> Ignore the field.
//!
//! Here is a more complex use case showcasing all the ways you can use derives:
//!
//! ```rust
//! use pbf::{self, BitCast, ProtoRead, ProtoWrite};
//!
//! #[derive(Debug, Default, Copy, Clone, PartialEq, BitCast)]
//! enum TestEnum {
//! #[default]
//! A = 1,
//! B = 2, // tag increments from the previous value so it will be 2
//! C = 3, // again, tag increments to 3
//! }
//!
//! #[derive(Debug, Default, PartialEq, ProtoRead, ProtoWrite)]
//! struct NestedStruct {
//! a: usize,
//! b: String,
//! }
//!
//! #[derive(Debug, Default, PartialEq, ProtoRead, ProtoWrite)]
//! struct TestStruct {
//! #[pbf(tag = 10, signed)]
//! a: i32,
//! #[pbf(ignore)]
//! b: bool,
//! c: Vec<u8>,
//! d: TestEnum,
//! #[pbf(tag = 20, fixed)]
//! e: u32,
//! #[pbf(nested)]
//! f: NestedStruct,
//! g: Option<f64>,
//! #[pbf(nested)]
//! h: Option<NestedStruct>,
//! #[pbf(signed)]
//! i: Option<Vec<i32>>,
//! }
//!
//! #[derive(Debug, Default, PartialEq, ProtoRead, ProtoWrite)]
//! pub enum Value {
//! /// String value
//! String(String),
//! /// Unsigned integer value
//! UInt(u64),
//! /// Signed integer 64-bit value
//! #[pbf(signed)]
//! SInt(i64),
//! /// 64-bit Floating point value
//! #[pbf(fixed)]
//! Double(f64),
//! /// Boolean value
//! #[pbf(tag = 12)]
//! Bool(bool),
//! /// Option case
//! Option(Option<i64>),
//! /// Value case
//! Enum(TestEnum),
//! /// Nested struct
//! #[pbf(nested)]
//! Nested(NestedStruct),
//! /// Null value
//! #[default]
//! Null,
//! }
//! ```
//!
//! If you are using the `derive` feature, you can also derive the `BitCast` trait for your enum.
//!
//! ```rust
//! use pbf::BitCast;
//!
//! #[derive(BitCast)]
//! enum TestEnum {
//! A = 1,
//! B = 2,
//! C = 3,
//! }
//! ```
extern crate pbf_core;
extern crate pbf_derive;
pub use *;
pub use *;