Expand description
§Rustwire (encoded protobuf utils)
This crate provides a set of utilities for working with Protocol Buffers (protobuf) in Rust. It offers functions and types to manipulate encoded buffer messages efficiently.
§Features
- Extract fields from an encoded protocol buffer message by tag number.
- Replace fields in an encoded protocol buffer message.
- Create headers for protocol buffer fields.
- Support for various wire types: varint, 64-bit, length-delimited, and 32-bit.
§Installation
Add the following to your Cargo.toml
file:
[dependencies]
rustwire = "0.1.0"
§Usage
Here’s a quick example of how to use the protobuf-utils
crate:
use rustwire::{extract_field_by_tag, replace_field_with, create_header, Variant};
// Extract a field from an encoded message
let encoded_message = b"\x08\x01\x12\x07\x74\x65\x73\x74\x69\x6e\x67";
let tag_number = 2;
match extract_field_by_tag(encoded_message, tag_number) {
Some(field_value) => println!("Field value: {:?}", field_value),
None => println!("Field not found"),
}
// Replace a field in an encoded message
let mut encoded_message = b"\x08\x01\x12\x07\x74\x65\x73\x74\x69\x6e\x67".to_vec();
let tag_number = 2;
let replace_with = b"Hello";
match replace_field_with(&mut encoded_message, tag_number, replace_with) {
Some(old_value) => println!("Replaced field value: {:?}", old_value),
None => println!("Field not found or error occurred"),
}
// Create a header for a field
let tag_number = 1;
let variant = Variant::LengthDelimited;
let encoded_message = b"Hello, world!";
let header = create_header(tag_number, variant.into(), encoded_message);
let encoded_field = [&header[..], encoded_message].concat();
println!("Encoded field: {:?}", encoded_field);
For more detailed information and examples, please refer to the individual function and type documentation.
§Contributing
Contributions are welcome! If you find any issues or have suggestions for improvement, please open an issue or submit a pull request on the GitHub repository.
§License
This crate is licensed under the MIT License.
Enums§
- Variant
- Represents the wire type variant of a field in a protocol buffer message.
Functions§
- create_
header - Creates the header for a field in a protocol buffer message.
- encode_
double - Encodes a double-precision floating-point number (
f64
) into its binary representation. - encode_
float - Encodes a single-precision floating-point number (
f32
) into its binary representation. - encode_
varint - Encodes a 64-bit unsigned integer (
u64
) into its varint representation. - extract_
field_ by_ tag - Extracts a field with the given tag number from an encoded protobuf message.
- extract_
multiple_ fields_ by_ tag - Extracts multiple fields with the given tag numbers from an encoded protobuf message.
- replace_
field_ with - Replaces a field with the specified tag number in the encoded message with the given replacement data.