Expand description
Runtime protobuf reflection via prost-reflect.
This crate provides a thin facade over prost_reflect for dynamic
protobuf operations: building a DescriptorPool from a
prost_types::FileDescriptorSet and constructing DynamicMessage
instances at runtime without generated Rust types.
§Quick start
use oxiproto_reflect::{pool_from_fds_bytes, dynamic_message};
// `fds_bytes` is the raw bytes of a `FileDescriptorSet` proto.
let pool = pool_from_fds_bytes(fds_bytes)?;
let msg = dynamic_message(&pool, "my.package.MyMessage")?;
println!("fields: {:?}", msg.descriptor().fields().collect::<Vec<_>>());§Debug and Display for DynamicMessage
DynamicMessage implements both std::fmt::Debug and
std::fmt::Display (protobuf text format). The following example
verifies both traits work correctly through this crate’s re-exports.
use oxiproto_reflect::{pool_from_fds, DynamicMessage};
use prost_types::{
FileDescriptorSet, FileDescriptorProto, DescriptorProto, FieldDescriptorProto,
};
use prost_types::field_descriptor_proto::{Label, Type};
let fds = FileDescriptorSet {
file: vec![FileDescriptorProto {
name: Some("test.proto".to_string()),
syntax: Some("proto3".to_string()),
message_type: vec![DescriptorProto {
name: Some("Ping".to_string()),
field: vec![FieldDescriptorProto {
name: Some("value".to_string()),
number: Some(1),
label: Some(Label::Optional as i32),
r#type: Some(Type::Int32 as i32),
json_name: Some("value".to_string()),
..Default::default()
}],
..Default::default()
}],
..Default::default()
}],
};
let pool = pool_from_fds(fds).unwrap();
let msg_desc = pool.get_message_by_name("Ping").unwrap();
let msg = DynamicMessage::new(msg_desc);
// Debug format is always available.
let debug_str = format!("{msg:?}");
assert!(!debug_str.is_empty());
// Display uses the protobuf text format; an empty message formats to "".
let display_str = format!("{msg}");
assert_eq!(display_str, "");Re-exports§
pub use dynamic::clear_field;pub use dynamic::get_field_by_name;pub use dynamic::has_field;pub use dynamic::set_field_by_name;pub use dynamic::unknown_fields;pub use native::Cardinality as NativeCardinality;pub use native::DescriptorPool as NativeDescriptorPool;pub use native::DynamicMessage as NativeDynamicMessage;pub use native::EnumDescriptor as NativeEnumDescriptor;pub use native::EnumValueDescriptor as NativeEnumValueDescriptor;pub use native::FieldDescriptor as NativeFieldDescriptor;pub use native::FileDescriptor as NativeFileDescriptor;pub use native::Kind as NativeKind;pub use native::MapKey as NativeMapKey;pub use native::MessageDescriptor as NativeMessageDescriptor;pub use native::MethodDescriptor as NativeMethodDescriptor;pub use native::NativeJsonError;pub use native::NativeTextError;pub use native::OneofDescriptor as NativeOneofDescriptor;pub use native::ServiceDescriptor as NativeServiceDescriptor;pub use native::Value as NativeValue;
Modules§
- dynamic
- Free-function convenience helpers for
DynamicMessagefield access. - native
- Native, pure-Rust protobuf reflection.
Structs§
- Descriptor
Pool - A
DescriptorPoolis a collection of related descriptors. Typically it will be created from aFileDescriptorSetoutput by the protobuf compiler (seeDescriptorPool::from_file_descriptor_set) but it may also be built up by adding files individually. - Dynamic
Message DynamicMessageprovides encoding, decoding and reflection of a protobuf message.- Enum
Descriptor - A protobuf enum type.
- Field
Descriptor - A protobuf message definition.
- File
Descriptor - A single source file containing protobuf messages and services.
- Message
Descriptor - A protobuf message definition.
- Method
Descriptor - A method definition for a
ServiceDescriptor. - Service
Descriptor - A protobuf service definition.
- Unknown
Field - An unknown field found when deserializing a protobuf message.
Enums§
- Reflect
Error - Errors produced by reflection operations.
- Reflect
Value - Re-export of
prost_reflect::Valueunder a distinct alias to avoid name conflicts withprost_types::Value. A dynamically-typed protobuf value.
Traits§
- Reflect
Message - Re-export of the
prost_reflect::ReflectMessagetrait so callers can usemsg.descriptor()without a separateprost_reflectdependency. Trait for message types that support reflection.
Functions§
- all_
messages - Iterate over all message descriptors registered in the pool.
- all_
services - Iterate over all service descriptors registered in the pool.
- dynamic_
message - Construct an empty
DynamicMessagefor the named message inpool. - get_
enum_ by_ name - Look up an enum descriptor by its fully-qualified name.
- get_
service_ by_ name - Look up a service descriptor by its fully-qualified name.
- pool_
from_ fds - Build a
DescriptorPooldirectly from aFileDescriptorSet. - pool_
from_ fds_ bytes - Build a
DescriptorPoolfrom the raw bytes of a serializedprost_types::FileDescriptorSet.
Derive Macros§
- Reflect
Message - Re-export of the
prost_reflect::ReflectMessagetrait so callers can usemsg.descriptor()without a separateprost_reflectdependency. A derive macro for theReflectMessagetrait.